简体   繁体   English

在没有功能的python中实现扫雷时出现的问题

[英]Problem in implementing minesweeper reveal in python without functions

I am working on making the game minesweeper for a project, but have encountered a problem with the reveal part of the game (If if enter the coordinates of any cell that has 0 mines surrounding it, then the game is supposed to keep revealing neighboring tiles unless a non-zero cell value is reached).我正在为一个项目制作游戏扫雷,但在游戏的显示部分遇到了问题(如果输入任何周围有 0 个地雷的单元格的坐标,那么游戏应该继续显示相邻的瓷砖除非达到非零单元格值)。 I have also searched the internet for this but cannot find any appropriate solution (I am not allowed to use functions as it is not a part of this year's curriculum).我也为此在互联网上搜索过,但找不到任何合适的解决方案(我不允许使用函数,因为它不是今年课程的一部分)。 Can somebody pls provide any insight as to how I can implement this in python.有人可以提供有关我如何在 python 中实现此功能的任何见解。

PS For reference you can have a look at this code PS作为参考,您可以查看此代码

def neighbours(r, col):
 
global mine_values
global numbers
global vis

# If the cell already not visited
if [r,col] not in vis:

    # Mark the cell visited
    vis.append([r,col])

    # If the cell is zero-valued
    if numbers[r][col] == 0:

        # Display it to the user
        mine_values[r][col] = numbers[r][col]

        # Recursive calls for the neighbouring cells
        if r > 0:
            neighbours(r-1, col)
        if r < n-1:
            neighbours(r+1, col)
        if col > 0:
            neighbours(r, col-1)
        if col < n-1:
            neighbours(r, col+1)    
        if r > 0 and col > 0:
            neighbours(r-1, col-1)
        if r > 0 and col < n-1:
            neighbours(r-1, col+1)
        if r < n-1 and col > 0:
            neighbours(r+1, col-1)
        if r < n-1 and col < n-1:
            neighbours(r+1, col+1)  
             
    # If the cell is not zero-valued            
    if numbers[r][col] != 0:
            mine_values[r][col] = numbers[r][col]

Link - https://www.askpython.com/python/examples/create-minesweeper-using-python链接 - https://www.askpython.com/python/examples/create-minesweeper-using-python

This should work:这应该有效:

    #before main loop
    shifts = [
        (-1,-1),
        (-1, 1),
        (-1, 0),
        ( 1,-1),
        ( 1, 1),
        ( 1, 0),
        ( 0,-1),
        ( 0, 1),
    ]
    #main loop
    to_reveal = []
    if (x,y) not in vis:
        to_reveal.append((x,y))

    while to_reveal != []:
        cell = to_reveal.pop()
        vis.append(cell)
        if board[cell[1]][cell[0]] == 0:
            for shift in shifts:
                if (cell[0] + shift[0],cell[1] + shift[1]) not in vis and 0 <= (cell[0] + shift[0]) <= 9 and 0 <= (cell[1] + shift[1]) <= 9:
                    to_reveal.append((cell[0] + shift[0],cell[1] + shift[1]))

What this code does, is after the player picks a tile to uncover, that tile is added to a list.这段代码的作用是在玩家选择要发现的瓷砖后,将该瓷砖添加到列表中。 Then it if is a 0 add all neighbors to the list, ect.然后如果是 0,则将所有邻居添加到列表中,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM