From c2385405552a506da3debf4e913b17055486abb5 Mon Sep 17 00:00:00 2001 From: AshMLondon <98660851+AshMLondon@users.noreply.github.com> Date: Sat, 19 Mar 2022 16:01:06 +0000 Subject: [PATCH 1/2] Close window on finish Use tk.destroy() function to close down the window at end of a game when you say you don't want to continue. The quit() function was leaving window open and causing error next time program called. --- minesweeper.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/minesweeper.py b/minesweeper.py index 4c32893..5358d79 100644 --- a/minesweeper.py +++ b/minesweeper.py @@ -130,7 +130,9 @@ def gameOver(self, won): if res: self.restart() else: - self.tk.quit() + self.tk.destroy() #shut down the window + #self.tk.quit() + def updateTimer(self): ts = "00:00:00" From d33fc6b82cf2f149937c67ccfce02738c20658bc Mon Sep 17 00:00:00 2001 From: AshMLondon <98660851+AshMLondon@users.noreply.github.com> Date: Sat, 19 Mar 2022 16:52:05 +0000 Subject: [PATCH 2/2] Set specific number of mines not random Add flag to choose to set specific number of mines. If set, will generate correct size list of mines as part of setup. --- README.md | 2 +- minesweeper.py | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d6045dc..ed9d7a0 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,6 @@ Contents: To Do: ---------- -- Have specific number of mines, rather than random +- ~~Have specific number of mines, rather than random~~ - Highscore table - Adjustable grid and mine count via UI diff --git a/minesweeper.py b/minesweeper.py index 5358d79..8796195 100644 --- a/minesweeper.py +++ b/minesweeper.py @@ -12,6 +12,9 @@ SIZE_X = 10 SIZE_Y = 10 +FIXED_MINE_NUMBER = True #if True set a fixed number of mines, rather than a random outcome +MINES_TO_SET = 9 + STATE_DEFAULT = 0 STATE_CLICKED = 1 STATE_FLAGGED = 2 @@ -61,6 +64,12 @@ def setup(self): self.correctFlagCount = 0 self.clickedCount = 0 self.startTime = None + + if FIXED_MINE_NUMBER: + #if fixed number, generate random list of mines + #for simplicity generate set of random numbers going up to x*y size + #will then compare with mine number, counting across row then row by row + mine_list = random.sample(range(SIZE_X*SIZE_Y-1),MINES_TO_SET) # create buttons self.tiles = dict({}) @@ -75,11 +84,19 @@ def setup(self): # tile image changeable for debug reasons: gfx = self.images["plain"] - - # currently random amount of mines - if random.uniform(0.0, 1.0) < 0.1: - isMine = True - self.mines += 1 + + if FIXED_MINE_NUMBER: + #fixed amount of mines + if x*SIZE_X+y in mine_list: + #count which square number we're on and see if in mine list + isMine = True + self.mines += 1 + + else: + # currently random amount of mines + if random.uniform(0.0, 1.0) < 0.1: + isMine = True + self.mines += 1 tile = { "id": id,