对于一个简单的迷宫游戏,如何缩短代码?

2024-10-04 03:16:34 发布

您现在位置:Python中文网/ 问答频道 /正文

你好。我是一个代码初学者,基本上我用python做了一个简单的迷宫游戏。 它是3x3,所以我制作了9个字典变量,详细说明了每个图块(北、南、东、西)可能的移动 我的代码由嵌套的if elif和一个main while循环组成。你知道吗

if level == 1: #level of difficulty.
    i = 0
    maplist1 = {'directions1':'south'}
    maplist2 = {'directions1':'south','directions2':'east'}
    maplist3 = {'directions1':'west'}
    maplist4 = {'directions1':'north','directions2':'east','directions3':'south'}
....
....
    current_position = 1 #local variable within the first if statement

下面是代码块的一个片段,它将被重复,几乎没有变化(字面上重复)。你知道吗

    while i == 0: #to continuously loop the program
        if current_position == 1:
            directions = maplist1['directions1']
            direction = input(f"What direction do you want to go: {directions} ").title() #choose the possible directions to go to.
            if direction == "S" or direction == "South":
                current_position = 4
                print(f"You are in cell {current_position}.")
            else:
                print("Cannot go in that direction.")
        if current_position == 2:
            directions = maplist2['directions1']
            directions2 = maplist2['directions2']
            direction = input(f"What direction do you want to go: {directions} {directions2} ").title()
            if direction == "S" or direction == "South":
                current_position = 5
                print(f"You are in cell {current_position}.")
            elif direction == "E" or direction == "East":
                current_position = 3
                print(f"You are in cell {current_position}.")
            else:
                print("Cannot go in that direction.")
        if current_position == 3:
            directions = maplist3['directions1']
            direction = input(f"What direction do you want to go: {directions} ").title()
            if direction == "W" or direction == "West":
                current_position = 2
                print(f"You are in cell {current_position}.")
            else:
                print("Cannot go in that direction.")
        if current_position == 4:
            directions = maplist4['directions1']
            directions2 = maplist4['directions2']
            directions3 = maplist4['directions3']
            direction = input(f"What direction do you want to go: {directions} {directions2} {directions3} ").title()
            if direction == "N" or direction == "North":
                current_position = 1
                print(f"You are in cell {current_position}.")
            elif direction == "S" or direction == "South":
                current_position = 7
                print(f"You are in cell {current_position}.")
            elif direction == "E" or direction == "East":
                current_position = 5
                print(f"You are in cell {current_position}.")
            else:
                print("Cannot go in that direction.")
.......
......
.......
        if current_position == 9:
            print("Congratulations, you finished the game.")
            i += 1 #to stop the loop

我的问题是如何使它更简单,更紧凑?你知道吗

  • 每次移动都必须记住当前位置
  • 我知道如何使用def,但很不确定如何实现它在我的迷宫游戏。你知道吗

这个游戏有3个关卡,基本上是3x3,4x4和5x5。这就是为什么我想知道如何缩短/压缩代码的原因。你知道吗

我不需要你给我你的代码,但一些指导将是伟大的如何进行,因为我现在感到迷茫。你知道吗


Tags: ortoinyougoifpositioncell
3条回答

利用规则而不是硬编码任何东西。你知道吗

如果你有一个3x3迷宫,你的(x,y)索引是类似的

0,0   1,0    2,0
0,1   1,1    2,1
0,2   1,2    2,2

您可以将其存储为列表列表:

field = [ ["one","two","three"],["four","five","six"],["seven","eight","nine"] ]
pos = (0,0)
p = field[pos[0]][pos[1]] #  would be "one"

# rules for movement based on actual position:
move_right = pos[0] < (3-1)
move_left = pos[0] > 0
move_up = pos[1] > 0
move_down = pos[1] < (3-1)

可以使用函数对其进行编码:

# p is a coordinate (0,0) etc tuple of x and y

# as functions
def can_move_right(p, dim=3):
    return p[0] < dim-1

def can_move_left(p,dim=3):
    return p[0] > 0

def can_move_up(p, dim=3):
    return p[1] > 0

def can_move_down(p,dim=3):
    return p[1] < dim-1

def move_up(p):
    if can_move_up(p):
        return (p[0],p[1]-1)
    # implicitly returns None if not possible

def move_down(p):
    if can_move_down(p):
        return (p[0],p[1]+1)

# etc

new_pos = move_up( (1,1) )  # => (1,0)
new_pos = move_down( (1,1) )  # => (1,2)

new_pos = move_up( (0,0) )  # None - need to handle it
if not new_pos:
    print("Wrong move")

要调整nx n,只需给出其他dim规则是相同的。你知道吗

尝试创建一个迷宫式的数据结构。一个典型的想法是创建一个二维单元阵列,每个单元都有一个北/西/南/东墙。你知道吗

让我们创建一个类,只是为了更好地理解。我希望你忽略除can_move()方法之外的所有内容。你知道吗

class Cell:
    def __init__(self, walls):
        self.walls = walls

    def __str__(self):
        """Allows us to call print() on a Cell!"""
        return '\n'.join(self.draw())

    def _draw_wall(self, wall, s):
        return s if wall in self.walls else ' ' * len(s)

    def draw(self, inner='   '):
        """Draw top, mid, and bottom parts of the cell."""
        n = self._draw_wall('N', '___')
        w = self._draw_wall('W', '|')
        e = self._draw_wall('E', '|')
        s = self._draw_wall('S', '___')
        top = ' ' + n + ' '
        mid = w + inner + e
        bot = w + s + e
        return top, mid, bot

    def can_move(self, direction):
        return direction not in self.walls

让我们做一些细胞,看看它们是什么样子:

>>> Cell('NWSE')
 ___ 
|   |
|___|

>>> Cell('NWS')
 ___ 
|    
|___ 

现在,一个迷宫是由一个二维的细胞列表组成的。这里有一个小迷宫:

maze = Maze(width=3, height=3, rows=[
    [Cell('NWE'),  Cell('NW'),   Cell('NE')],
    [Cell('WE'),   Cell('WE'),   Cell('WE')],
    [Cell('WS'),   Cell('SE'),   Cell('WSE')]
])

看起来是这样的:

>>> maze
 ___  ___  ___
| x ||        |
|   ||        |

|   ||   ||   |
|   ||   ||   |

|        ||   |
|___  ___||___|

但是等等!我们还没有定义什么是Maze。再一次,忽略所有与绘图相关的内容,将注意力集中在move_direction()。你知道吗

class Maze:
    def __init__(self, width, height, rows):
        self.width = width
        self.height = height
        self.rows = rows
        self.position = (0, 0)

    def __str__(self):
        return '\n'.join(self.draw_row(i) for i, _ in enumerate(self.rows))

    def _inner(self, i, j):
        return ' x ' if (i, j) == self.position else '   '

    def draw_row(self, i):
        triples = [
            cell.draw(self._inner(i, j)) for j, cell in enumerate(self.rows[i])
        ]
        return '\n'.join([
            ''.join(t for t, _, _ in triples),
            ''.join(m for _, m, _ in triples),
            ''.join(b for _, _, b in triples)])

    def cell_at_position(self, i, j):
        return self.rows[i][j]

    def move_direction(self, direction):
        curr_cell = self.cell_at_position(*self.position)
        if not curr_cell.can_move(direction):
            print("Can't go in that direction!")
            return
        deltas = {'N': (-1, 0), 'W': (0, -1), 'S': (1, 0), 'E': (0, 1)}
        y, x = self.position
        dy, dx = deltas[direction]
        self.position = y + dy, x + dx

要使用它,只需创建一个简单的小循环:

while True:
    print(maze)
    direction = input('What direction? ')
    maze.move_direction(direction)

看着奇迹发生!你知道吗

你可以试试here。你知道吗

不要把迷宫想象成一维,即当前的位置(1,2,3,9),你应该把它想象成二维矩阵空间((0,0),(0,1))。你知道吗

我们不应该把基本迷宫硬编码为3,我们应该考虑nxnmaze,因为现在你已经实现了3x3的逻辑,现在你应该可以把它扩展到nxn

你用你的地图来处理可能的方向,我们应该写边界条件的逻辑,就像一个检查每一步可能的方向的方法

def possible_path(current_x, current_y, matrix_size=3):
    # this returns like directions possible like current_x + 1 < matrix_size
    # then the east direction is possible.
   pass

方向处理应该是这样的

 def position_handler(row, col, direction):
     # can write the logic for mapping the direction to the movement in X and Y,
     # if direction == 'NORTH' 
     # or can get the config from a map like,
     # move = movement.get(direction)
     # say NORTH config is =>movement= {'NORTH': {'x': 0, 'y':1}}
     # and now move accordingly i.e 
     # row += move.get('x')
     # col += move.get('y')
     pass

相关问题 更多 >