I am currently making a minesweeper game with Python but I have some problems withe the mines repartition:
To choose the places of the different mines, I use the Python random
module to generate x and y coordinates. Firstly I chose to place all the mines before the player first plays but the problem was that the first play was generally a mine or a cas with mines as neighbours. So I decided to place the mines after the player first plays to be sure that the first cas revealed will not have mines as neighbours but the problem is that the mines's repartition is not very interesting for a mines game. How can I make it better?
Here is my function that places mines in the Board class after player first plays where pos is coords of first cas revealed:
def place_mines(self, pos):
posed_mines = 0
while posed_mines < self.mines_number:
x = random.randrange(0, COLUMN_NUMBER-1)
y = random.randrange(0, LINE_NUMBER-1)
if not self.Matrix[y][x].is_mined and (x < pos[0]-1 or x > pos[0]+1) and (y < pos[1]-1 or y > pos[1]+1):
self.Matrix[y][x].mine()
posed_mines += 1
And the result in game:
Here is the function that places all the mines before the player first plays:
def place_mines(self):
posed_mines = 0
while posed_mines < self.mines_number:
x = random.randrange(0, COLUMN_NUMBER-1)
y = random.randrange(0, LINE_NUMBER-1)
if not self.Matrix[y][x].is_mined:
self.Matrix[y][x].mine()
posed_mines += 1
And the result in game :
My question is not a duplicate because on the question linked, the mine placing is not done after player first play