Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-vol.2中简单的基于文本的网格游戏_Python_Python 3.x - Fatal编程技术网

Python-vol.2中简单的基于文本的网格游戏

Python-vol.2中简单的基于文本的网格游戏,python,python-3.x,Python,Python 3.x,所以,我和你们中的一些人帮助了我。我已经知道了如何做动作,也知道了如何防止玩家穿过墙壁。我的移动和主要功能如下所示: def move(grid,row,col,nRow,nCol): grid[nRow][nCol]='O' grid[row][col]=' ' if grid[nRow][nCol]==grid[row][col]: grid[nRow][nCol]='O' def main(): grid,row,col=initializ

所以,我和你们中的一些人帮助了我。我已经知道了如何做动作,也知道了如何防止玩家穿过墙壁。我的移动和主要功能如下所示:

def move(grid,row,col,nRow,nCol):
    grid[nRow][nCol]='O'
    grid[row][col]=' '
    if grid[nRow][nCol]==grid[row][col]:
        grid[nRow][nCol]='O'

def main():
    grid,row,col=initialize()
    while True:
        display(grid)
        dir=int(input("Where you wanna go?: "))
        if dir==2:
            nRow=row+1
            nCol=col
            if grid[nRow][nCol]=='#':
                nRow=row
                nCol=col
                print("There's a wall!")
            move(grid,row,col,nRow,nCol)
        row=nRow
        col=nCol

main()
问题是我的主函数中有墙条件,但我想把它移到另一个位置。然而,如果我这样做,玩家将只是通过墙壁移动。那么,如何在保留功能的同时将墙条件移动到移动中

我试着这样做:

def move(grid,row,col,nRow,nCol):
    if grid[nRow][nCol]=='#':
        nRow=row
        nCol=col
        print("There's a wall!")
    grid[nRow][nCol]='O'
    grid[row][col]=' '
    if grid[nRow][nCol]==grid[row][col]:
        grid[nRow][nCol]='O'



def main():

    grid,row,col=initialize()
    while True:
        display(grid)

        dir=int(input("Where you wanna go?: "))
        if dir==2:
            nRow=row+1
            nCol=col
            move(grid,row,col,nRow,nCol)

那么,你正在检查你的主要功能中是否有一堵墙;您对执行此“检查内部移动”功能感觉如何?您认为您的移动调用是否包含所有必要的参数

编辑

我理解您的代码不起作用的原因:move是一个方法,在这个意义上,它对其参数执行某些操作,但不返回任何内容

由于网格是可变的,您可以就地更改其值,但row、col、nRow、nCol的情况并非如此:它们的值可以在移动范围内更改,但这些更改在main中不可见。这意味着无效移动的移动拒绝在main中不可见,后者将仅具有nRow和nCol的试用值,而不是更新的值

避免这种情况的一种方法就是返回nRow和nCol:

并让主要负责人意识到这一点:

def main():
    (...)
        nRow, nCol = move(grid,row,col,nRow,nCol)
    row=nRow
    col=nCol
这样,move可以更新main中nRow和nCol的值

另一个解决方案,正如abarnet在年的评论中所给出的,是在有墙的情况下,此举将引发一个例外:

class MoveException(BaseException):
     """exception to be raised when move would hit a wall"""
     pass

def move(grid, row, col, nRow, nCol):
    if grid[nRow][nCol]=='#':
        # illegal move!
        raise MoveException
    else:
        # move can be done, updating grid content
        grid[nRow][nCol] = '0'  # moving character
        grid[row][col] = ' '    # cleaning grid

def main()
    grid,row,col=initialize()
    while True:
        display(grid)

        dir=int(input("Where you wanna go?: "))
        if dir==2:
            nRow=row+1
            nCol=col
        # specify other possible movements here

        # now trying to move
        try:
            move(grid,row,col,nRow,nCol)
        except MoveException:
            # illegal move! 
            # We do not update character's position.
            # This could also be the place to print some error message
            # but I'm lazy and will let you add something here ;)
            pass                    
        else:
            # move could be performed, updating character's position
            row, col = nRow, nCol
您对这个“尝试并处理问题”解决方案感觉如何?我觉得它更具可读性,异常的出现可以传递多个方法层,这使它们非常有用

顺便说一句,在我看来,您的move方法遗漏了一个条件块,否则会使它更干净一些:

def move(grid,row,col,nRow,nCol):
    if grid[nRow][nCol]=='#':
        # this block is executed if there is a wall in the desired position
        nRow=row
        nCol=col
        print("There's a wall!")
    else:  
        # this block is only executed if there is no wall in the desired position
        grid[nRow][nCol]='O'  # moving character's character
        grid[row][col]=' '    # emptying previous position

请修正你的缩进。这正是我想要做的。我想将墙检查移动到move函数,这样我的主函数就不会有太多不必要的东西。我只是不知道怎么做,我不知道我错过了什么。墙壁检查条件在主要情况下就像一个符咒,但如果我尝试在move中实现它,它完全忽略了它的作用。你可以用你的move内容更新你的问题,以便我们可以查看你的try?更新,也许会有帮助
def move(grid,row,col,nRow,nCol):
    if grid[nRow][nCol]=='#':
        # this block is executed if there is a wall in the desired position
        nRow=row
        nCol=col
        print("There's a wall!")
    else:  
        # this block is only executed if there is no wall in the desired position
        grid[nRow][nCol]='O'  # moving character's character
        grid[row][col]=' '    # emptying previous position