python-3.x 自动清除扫雷器中的空格

68bkxrlz  于 2023-02-17  发布在  Python
关注(0)|答案(1)|浏览(96)

我试图在pygame代码扫雷,我有一个自动清除空格的问题,由于递归错误,RecursionError:最大递归深度超过了比较,它说它被重复993次以上(这个数量不会改变,无论多少随机生成的位置,我创建).我已经尝试做我自己的解决方案,并得到这个问题,然后我用一个解决方案,我发现在互联网上,仍然得到同样的错误,我不知道该怎么办

import pygame
import random

pygame.init()

size = 800
rows = 8
window = pygame.display.set_mode((size, size))
pygame.display.set_caption("MineSweeper")
mines_needed = 10
num_colour = {
    0: (255,255,255), 1:(3,37,126), 2: (0,137,0), 3: (255,0,0), 4: (0,0,255), 5: (128,0,0), 6: (0,128,128), 7: (0,0,0), 8: (128,128,128)
}
font = pygame.font.SysFont('arial', 30)

def board():
    global board_array
    board_array = []
    window.fill((255, 255, 255))
    distance = size//rows #100
    x = 0
    y = 0
    for i in range(rows):
        pygame.draw.line(window, (0, 0, 0), (x, 0), (x, size))
        pygame.draw.line(window, (0, 0, 0), (0, y), (size, y))
        x += distance
        y += distance
        elements = []
        for j in range(rows):
            elements.append(0)
        board_array.append(elements)
def create_mines():
    mines_created = 0
    while mines_created < mines_needed:
        randomr = random.randint(-1,7)
        randomc = random.randint(-1,7)
        if board_array[randomr][randomc] == -1:
            pass
        else:
            board_array[randomr][randomc] = -1
            mines_created += 1
def draw():
    number = 0
    for x in range(8):
        for y in range(8):
            if x == 0 and y == 0:
                number = 0
                if board_array[x + 1][y] == -1:
                    number += 1
                if board_array[x + 1][y + 1] == -1:
                    number += 1
                if board_array[x][y + 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if x == 7 and y == 0:
                number = 0
                if board_array[x - 1][y] == -1:
                    number += 1
                if board_array[x - 1][y + 1] == -1:
                    number += 1
                if board_array[x][y + 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if x == 0 and y == 7:
                number = 0
                if board_array[x + 1][y] == -1:
                    number += 1
                if board_array[x + 1][y - 1] == -1:
                    number += 1
                if board_array[x][y - 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if x == 7 and y == 7:
                number = 0
                if board_array[x - 1][y] == -1:
                    number += 1
                if board_array[x - 1][y - 1] == -1:
                    number += 1
                if board_array[x][y - 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if x == 0 and (y != 0 and y != 7):
                number = 0
                if board_array[x][y - 1] == -1:
                    number += 1
                if board_array[x + 1][y - 1] == -1:
                    number += 1
                if board_array[x + 1][y] == -1:
                    number += 1
                if board_array[x + 1][y + 1] == -1:
                    number += 1
                if board_array[x][y + 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if x == 7 and (y != 0 and y != 7):
                number = 0
                if board_array[x][y - 1] == -1:
                    number += 1
                if board_array[x - 1][y - 1] == -1:
                    number += 1
                if board_array[x - 1][y] == -1:
                    number += 1
                if board_array[x - 1][y + 1] == -1:
                    number += 1
                if board_array[x][y + 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if y == 0 and (x != 0 and x != 7):
                number = 0
                if board_array[x + 1][y] == -1:
                    number += 1
                if board_array[x - 1][y] == -1:
                    number += 1
                if board_array[x][y + 1] == -1:
                    number += 1
                if board_array[x + 1][y + 1] == -1:
                    number += 1
                if board_array[x - 1][y + 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if y == 7 and (x != 0 and x != 7):
                number = 0
                if board_array[x + 1][y] == -1:
                    number += 1
                if board_array[x - 1][y] == -1:
                    number += 1
                if board_array[x][y - 1] == -1:
                    number += 1
                if board_array[x + 1][y - 1] == -1:
                    number += 1
                if board_array[x - 1][y - 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number
            if (x != 7 and x != 0) and (y != 7 and y != 0):
                number = 0
                if board_array[x][y + 1] == -1:
                    number += 1
                if board_array[x][y - 1] == -1:
                    number += 1
                if board_array[x + 1][y] == -1:
                    number += 1
                if board_array[x - 1][y] == -1:
                    number += 1
                if board_array[x - 1][y - 1] == -1:
                    number += 1
                if board_array[x + 1][y - 1] == -1:
                    number += 1
                if board_array[x - 1][y + 1] == -1:
                    number += 1
                if board_array[x + 1][y + 1] == -1:
                    number += 1
            if board_array[x][y] != -1:
                board_array[x][y] = number

def click(x,y):
    dug = set()
    dug.add((x,y))
    centre_x = (x * 100) + 50
    centre_y = (y * 100) + 50
    if board_array[x][y] != -1:
        number_text = font.render(str(board_array[x][y]), True, num_colour[board_array[x][y]])
        window.blit(number_text, (centre_x - 10, centre_y - 10))
    elif board_array[x][y] != 0:
        pygame.draw.circle(window, (0, 0, 0), (centre_x, centre_y), 10)
    if board_array[x][y] == 0:
        pygame.draw.rect(window, (128,128,128), (x*100+2, y*100+2, 97, 97))
        for i in range(max(0,x-1), min(6,x+1)+1):
            for j in range(max(0, y-1), min(6, y+1)+1):
                if (x,y) in dug:
                    pass
                click(x,y)

def draw_flag():
    pass

board()
create_mines()
draw()
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                x,y = pygame.mouse.get_pos()
                x /= 100
                y /= 100
                click(int(x),int(y))

        if event.type == pygame.QUIT:
            run = False
    print(board_array)
    pygame.display.update()

我知道我的代码可以得到更好的优化,但我试图做到这一点,从互联网的帮助最小,并计划回到它一旦我完成了游戏

jmp7cifd

jmp7cifd1#

我不确定您到底要做什么,但是您将原始参数传递给click中的递归调用,因此无休止地运行。

if (x,y) in dug:
    pass
click(x,y)

1.将(x,y)替换为(i,j)

  1. pass不执行任何操作-您可能希望continue跳过递归单击-而是用途:
if (i,j) not in dug:
    click(i,j)

看起来这仍然会失败,因为您没有在递归调用中使用dug集,所以您需要将其传递到递归click并从其取回,以跳过您已经完成处理的点。

相关问题