Auto clear blank spaces in minesweeper

55 Views Asked by At

I'm trying to code minesweeper in pygame, I am having a problem with auto-clearing the blank spaces due to a recursion error, RecursionError: maximum recursion depth exceeded in comparison, it says it is being repeated 993 more times (this amount doesn't change no matter how many randomly generated positions I create). I've tried doing my own solution and got this problem then I used a solution I found on the internet and still got the same error I'm not sure what to do

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()

I know my code could be better optimised but I tried to do this with minimal help from the internet and plan on going back to it once I've completed the game

1

There are 1 best solutions below

1
Tzahi T On

I'm not sure exactly what you are trying to do, but you are passing the original arguments to the recursive call in click, thus running endlessly.

if (x,y) in dug:
    pass
click(x,y)
  1. Replace (x,y) with (i,j).
  2. pass does nothing - you probably wanted continue to jump over the recursive click - instead, use:
if (i,j) not in dug:
    click(i,j)

It looks like this will still fail as you don't use the dug set in the recursive calls, so you need to pass it to and get it back from the recursive click to skip points you've finished handling.