So, I wrote a minesweeper in python using a class, the random and the numpy modules. By choosing the difficulty, it sets up the dimension of the board and creates it, with the help of numpy. It, then, randomly sets up mines across the board, also according to the difficulty chosen.
When you make a play, a specific function checks the 8 cells around the point you chose to step and returns the amount of mines present there.
After some fixing, I ran the code for the final time in hopes that it would be working just fine… what I got in return were different values for the same position after making different plays.
I tried resorting to Chat, but it didn’t work out the way I wanted and the issue was still there.
I checked every function and the path the program was taking at every play, but I was still unable to find where it was making this mistake.
Since the code is quite big for this site, here’s the OnlineGDB link instead:
https://onlinegdb.com/JxHI-lRdP
A quick preview:
def setupMines(self, mines: int = 15) -> None:
if len(self.board) <= 5: raise ValueError('Board too small')
self.mines = set()
while len(self.mines) < mines:
rand_row = randint(0, self.__dimension - 1)
rand_col = randint(0, self.__dimension - 1)
self.mines.add((rand_row, rand_col))
for row, col in self.mines:
self.board[row][col] = 1
def makePlay(self, row: int, col: int) -> None:
self.row = row - 1
self.column = col - 1
if self.board[row - 1][col - 1] == 1:
for i in 'You stepped on a mine!nEnding program...':
print(i, end='')
wait(0.07)
exit(0)
elif self.board[row - 1][col - 1] == 0:
self.board[row - 1][col - 1] = -1
self.printBoard()
self.EndGame()
def checkPlay(self) -> int:
tip = 0
aux_row = -1
for _ in range(3):
aux_column = -1
for _ in range(3):
if (self.row + aux_row, self.column + aux_column) in self.mines: tip += 1
aux_column += 1
aux_row += 1
return tip