MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / play

Function play

projects/minesweeper/main.py:171–208  ·  view source on GitHub ↗
(dim_size=10, num_bombs=10)

Source from the content-addressed store, hash-verified

169
170# play the game
171def play(dim_size=10, num_bombs=10):
172 # Step 1: create the board and plant the bombs
173 board = Board(dim_size, num_bombs)
174
175 # Step 2: show the user the board and ask for where they want to dig
176 # Step 3a: if location is a bomb, show game over message
177 # Step 3b: if location is not a bomb, dig recursively until each square is at least
178 # next to a bomb
179 # Step 4: repeat steps 2 and 3a/b until there are no more places to dig -> VICTORY!
180 safe = True
181
182 while len(board.dug) < board.dim_size**2 - num_bombs:
183 print(board)
184 # 0,0 or 0, 0 or 0, 0
185 user_input = re.split(
186 ",(\\s)*", input("Where would you like to dig? Input as row,col: ")
187 ) # '0, 3'
188 row, col = int(user_input[0]), int(user_input[-1])
189 if row < 0 or row >= board.dim_size or col < 0 or col >= dim_size:
190 print("Invalid location. Try again.")
191 continue
192
193 # if it's valid, we dig
194 safe = board.dig(row, col)
195 if not safe:
196 # dug a bomb ahhhhhhh
197 break # (game over rip)
198
199 # 2 ways to end loop, lets check which one
200 if safe:
201 print("CONGRATULATIONS!!!! YOU ARE VICTORIOUS!")
202 else:
203 print("SORRY GAME OVER :(")
204 # let's reveal the whole board!
205 board.dug = [
206 (r, c) for r in range(board.dim_size) for c in range(board.dim_size)
207 ]
208 print(board)
209
210
211if __name__ == "__main__": # good practice :)

Callers 1

main.pyFile · 0.70

Calls 2

digMethod · 0.95
BoardClass · 0.70

Tested by

no test coverage detected