Basically, I want an event where if I click a cell and the value of the cell shows it has zero mines around it in a one cell radius, it automatically checks if there are more cells that also dont have any mines around it and also reveal themselves within the same turn. (Not too familiar with minesweeper but I think thats also how the real game works)
I am trying to use recursion, but it's my first time and I have no idea if I am doing it correct and for some reason it just ends up not showing any numericals over the cell at all to show how many mines are nearby. I know there is something wrong with my if/for statement but I can't pinpoint exactly what.
I am trying to use recursion, but it's my first time and I have no idea if I am doing it correct and for some reason it just ends up not showing any numericals over the cell at all to show how many mines are nearby. I know there is something wrong with my if/for statement but I can't pinpoint exactly what.
Here is my cell class:
from tkinter import Button
import random
import settings
class Cell:
all = [] # list of all cells
# x and y are the coordinates of the cell
def __init__(self, x, y, is_mine=False):
self.is_mine = is_mine
self.cell_button_object = None
self.x = x
self.y = y
Cell.all.append(self)
# creates a button object for the cell and binds the left and right click events
def create_button_object(self, location):
btn = Button(location, width = 12, height = 4)
btn.bind('<Button-1>', self.left_click)
btn.bind('<Button-3>', self.right_click)
self.cell_button_object = btn
#
def left_click(self, event):
# if the cell is a mine, show the mine
if self.is_mine:
self.show_mine()
# (Doesnt work but if it did) reveals zero mine value cells around the initailly clicked zero mine value cell
elif self.surrounded_cell_mines == 0:
def zeroCell(self):
for cell in self.surrounded_cells:
if cell.surrounded_cells_mines == 0:
cell.show_cell()
zeroCell(self)
else:
# reveals how many mines around cell
self.show_cell()
# returns the cell object based on the x and y coordinates
def get_cell_by_axis(self,x,y):
for cell in Cell.all:
if cell.x == x and cell.y == y:
return cell
@property
# returns a list of all the cells around the cell in a one cell radius
def surrounded_cells(self):
cells = [
self.get_cell_by_axis(self.x - 1, self.y - 1),
self.get_cell_by_axis(self.x -1, self.y),
self.get_cell_by_axis(self.x -1, self.y +1),
self.get_cell_by_axis(self.x, self.y -1),
self.get_cell_by_axis(self.x +1, self.y -1),
self.get_cell_by_axis(self.x +1,self.y),
self.get_cell_by_axis(self.x+1,self.y+1),
self.get_cell_by_axis(self.x, self.y+1)
]
cells = [cell for cell in cells if cell is not None]
return cells
@property
# returns the number of mines around the cell thorugh conter
def surrounded_cells_mines(self):
counter = 0
for cell in self.surrounded_cells:
if cell.is_mine:
counter +=1
return counter
# shows the number of mines around the cell
def show_cell(self):
self.cell_button_object.configure(text=self.surrounded_cells_mines)
def show_mine(self):
# change the color of the button to red if mine
self.cell_button_object.configure(bg = "red")
# right click event
def right_click(self, event):
print('right click')
# randomizes the mines in the game
@staticmethod
def randomize_mines():
picked_cells = random.sample(Cell.all, settings.mines_count)
for picked_cell in picked_cells:
picked_cell.is_mine = True
def __repr__(self):
return f"Cell({self.x}, {self.y})"
do this :
and this :