def test_flagging(self): """ Tests whether the flagging method updates correctly. """ board = Board(rows=10, cols=10, number_of_mines=0) square = board.get_square(0, 0) assert not square.flagged square.flag_square() assert square.flagged
def test_setup_board(self): """ Tests if the apppropriate dimensions have been set. Also checks to make sure no squares have been clicked. """ board = Board(rows=10, cols=10, number_of_mines=0) rows, cols = board.get_dimensions() assert (rows, cols) == (10, 10) for row in range(rows): for col in range(cols): assert not board.get_square(row, col).clicked
def test_hit_mine_first(self): """ Tests if manually setting mines works. Tests if hitting a mine on the first square removes the mine from the game. """ board = Board(rows=10, cols=10, number_of_mines=1) more_mines = [(0, 0), (0, 1), (0, 2), (1, 1)] board.set_mines(more_mines) assert not board.get_square(0, 1).clicked assert board.get_square(0, 1).mine board.click(0, 1) assert board.get_square(0, 1).clicked assert not board.get_square(0, 1).mine assert board.game_state == GameState.ONGOING
def test_hit_mine_second(self): """ Tests if manually setting mines works. Tests if hitting a mine on the first square causes you to lose the game. """ board = Board(rows=10, cols=10, number_of_mines=1) more_mines = [(0, 0), (0, 1), (0, 2), (1, 1)] board.set_mines(more_mines) assert not board.get_square(0, 0).clicked assert board.get_square(0, 0).mine board.click(0, 1) assert board.game_state == GameState.ONGOING board.click(0, 0) assert board.game_state == GameState.LOSE
def autoplay(self): t0 = time.time() while self.game_count < 100000: round = Board(rows=10, cols=10) while round.game_state in [GameState.ongoing, GameState.start]: # guess = self.choose_next(round) # original guess random cell strategy guess = self.choose_bestnext( round) # new guess best cell strategy round.click(guess[0], guess[1]) if round.game_state == GameState.win: self.win_count += 1 self.game_count += 1 t1 = time.time() time_taken = int(t1 - t0) print("\nNumber of games won: " + str(self.win_count) + " out of " + str(self.game_count) + " games.") print("Total time to complete the " + str(self.game_count) + " attempts: " + str(time_taken) + " seconds!") print("Average win rate: " + str(((self.win_count / self.game_count) * 100)) + "%\n")
def play(): """ Playable game of Minesweeper. """ num_rows, num_cols = dimensions() num_mines = number_of_mines(int(num_rows), int(num_cols)) welcome() board = Board(rows=int(num_rows), cols=int(num_cols), number_of_mines=int(num_mines)) while board.game_state in [GameState.ONGOING, GameState.START]: board.print_board(board.print_square) try: inp = input("> ") line = "".join(inp.split()) if line[0] == "f": point = list(map(int, line[1:].split(","))) board.get_square(point[0], point[1]).flag_square() else: point = list(map(int, line.split(","))) board.click(point[0], point[1]) except (IndexError, ValueError): oops() instructions() except KeyboardInterrupt: try: sys.exit(0) except SystemExit: os._exit(0) if board.game_state == GameState.LOSE: print("\n\nOh no! Better luck next time! You hit a mine.\n") print("Here's the solution!\n") else: print("\n\nYou didn't hit any of the mines! You win!\n") board.print_board(board.print_solution)
class Game: def __init__(self): self.board = Board(rows=10, cols=10) def play(self): self.welcome() while self.board.game_state in [GameState.ongoing, GameState.start]: self.board.pr_wrapper(self.board.pr_hook) try: inp = input("> ") line = "".join(inp.split()) if line[0] == "f": point = tuple(map(int, line[1:].split(","))) self.board.flagSq(point[0], point[1]) else: point = tuple(map(int, line.split(","))) self.board.click(point[0], point[1]) except (IndexError, ValueError): self.help() except KeyboardInterrupt: try: sys.exit(0) except SystemExit: os._exit(0) if self.board.game_state == GameState.lose: print("\n\nOh no! Better luck next time! You hit a mine.\n") print("Here's the solution!\n") else: print("\n\nYou didn't hit any of the mines! You win!\n") self.board.pr_wrapper(self.board.pr_endhook) def welcome(self): print("\nLet's play Minesweeper in Terminal!") self.help() def help(self): print("\nUse the following format to enter coordinates!") print("> <row>, <column>") print("> eg. 1, 1") print( "\nIf you'd like to flag a coordinate, use the following format!") print("If you'd like to UNflag, please enter that same coordinate.") print("> f <row>, <column>") print("> f 1,1") def play_again(self): ask = input('Would you like to go again? (y/n): ') return ask.lower() == 'y'
def test_click_square(self): """ Tests if a square's CLICK property is updating correctly. Also checks that neighboring squares with 0 mine neighbors are also clicked. """ board = Board(rows=10, cols=10, number_of_mines=0) assert not board.get_square(0, 0).clicked assert not board.get_square(9, 9).clicked board.click(0, 0) assert board.get_square(0, 0).clicked assert board.get_square(9, 9).clicked
def __init__(self): self.board = Board(rows=10, cols=10)
def play_round(self): board = Board(rows=10, cols=10) current_round = Round(board) if current_round.play() == GameState.WIN: self.win_count.value += 1 self.game_count.value += 1