def test_can_place_piece_and_trap_an_opponent_disc(): game = Othello() assert game.board[5][3] is None assert game.board[4][3].color == Color.WHITE game.move(to_coords=Coords(x=5, y=3)) assert game.board[5][3].color == Color.BLACK assert game.board[4][3].color == Color.BLACK
def test_player_can_move_detected_correctly(): game = Othello(restore_positions={ '01': Disc(Color.BLACK), '11': Disc(Color.WHITE), '22': Disc(Color.BLACK) }) black_can_move = game._scan_board_for_trapped_discs(Coords(x=0, y=0)) assert black_can_move
def test_winner_declared(): game = Othello( restore_positions={ '43': Disc(Color.WHITE), '33': Disc(Color.BLACK), '77': Disc(Color.WHITE), }) assert not game.winner game.move(to_coords=Coords(x=5, y=3)) assert game.winner == Color.BLACK
def test_game_ends_if_both_players_cant_move(): game = Othello( restore_positions={ '43': Disc(Color.WHITE), '33': Disc(Color.BLACK), '77': Disc(Color.WHITE), }) assert game.playing_color == Color.BLACK game.move(to_coords=Coords(x=5, y=3)) assert game.winner == Color.BLACK
def test_drawer_declared(): game = Othello( restore_positions={ '00': Disc(Color.WHITE), '70': Disc(Color.WHITE), '77': Disc(Color.WHITE), '33': Disc(Color.BLACK), '34': Disc(Color.WHITE) }) game.move(to_coords=Coords(x=3, y=5)) assert game.winner == Color.NONE
def test_othello_board_setup(): game = Othello() expected_board_setup = set([(Color.WHITE, Coords(x=3, y=4)), (Color.WHITE, Coords(x=4, y=3)), (Color.BLACK, Coords(x=3, y=3)), (Color.BLACK, Coords(x=4, y=4))]) actual_board_setup = set((disc.color, disc.coords) for row in game.board for disc in row if disc) assert actual_board_setup == expected_board_setup
def test_illegal_disc_placement_raises_exception(): game = Othello() assert game.playing_color == Color.BLACK with pytest.raises(IllegalMoveError, match=game.SQUARE_TAKEN): game.move(to_coords=Coords(x=4, y=4)) with pytest.raises(IllegalMoveError, match=game.ILLEGAL_MOVE): game.move(to_coords=Coords(x=5, y=4)) # doesn't trap opponent discs with pytest.raises(IllegalMoveError, match=game.ILLEGAL_MOVE): game.move(to_coords=Coords(x=7, y=7)) # not next to any discs assert game.playing_color == Color.BLACK # same color to play
def test_player_color_swaps_for_each_turn(): game = Othello() assert game.playing_color == Color.BLACK game.move(to_coords=Coords(x=5, y=3)) assert game.playing_color == Color.WHITE game.move(to_coords=Coords(x=5, y=4)) assert game.playing_color == Color.BLACK
def test_can_trap_multiple_discs(): game = Othello( restore_positions={ '42': Disc(Color.WHITE), '43': Disc(Color.WHITE), '44': Disc(Color.BLACK), '51': Disc(Color.WHITE), '61': Disc(Color.WHITE), '71': Disc(Color.BLACK), }) assert game.disc_count(Color.BLACK) == 2 game.move(to_coords=Coords(x=4, y=1)) assert game.disc_count(Color.BLACK) == 7
def othello(): return play_game(Othello(), move_piece_game=False)
class TerminalGame(): RED = '\033[91m' BLUE = '\033[94m' END = '\033[0m' NO_MESSAGE = '\n\n' INVALID_ARG_ERROR = f'''{RED} Invalid Args provided.....\n Usage: python {sys.argv[0]} GAME_TYPE\n Game type options: (C)hess, (D)raughts, (O)thello {END}''' QUIT_MSG = f'\n{RED}Got too much for you, did it?!?{END}\n' GAME_OPTIONS = {'C': Chess(), 'D': Draughts(), 'O': Othello()} X_COORD_MAP = { 'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7 } Y_COORD_MAP = { '1': 0, '2': 1, '3': 2, '4': 3, '5': 4, '6': 5, '7': 6, '8': 7 } def __init__(self): self.game = self._parse_args_to_fetch_game() self.display_message = self.NO_MESSAGE self.player = None def play(self): """Main game loop. Continue until winner declared.""" while not self.game.winner: self.player = self.game.playing_color.value self._display_board_to_terminal() self._make_move() self._display_board_and_winner() def _parse_args_to_fetch_game(self): try: game_type = sys.argv[1].title()[0] return self.GAME_OPTIONS[game_type] except (KeyError, IndexError): print(self.INVALID_ARG_ERROR) sys.exit() def _display_board_and_winner(self): self.display_message = f'\n{self.BLUE}{self.player} wins!!! Thanks for playing.....{self.END}\n' self._display_board_to_terminal() def _make_move(self): try: move_coords = input( f'{self.player} to move. Choose wisely.....\n --> ') self.display_message = self.NO_MESSAGE if len(move_coords) > 2: move_coords = self._split_and_map_coords(move_coords) self.game.move(*move_coords) else: place_piece_coords = self._map_coords(move_coords) self.game.move(to_coords=place_piece_coords) except IllegalMoveError as error: self.display_message = f'\n{self.RED}{error.message}{self.END}\n' def _split_and_map_coords(self, move_coords): try: from_coords, to_coords = move_coords.split() return self._map_coords(from_coords), self._map_coords(to_coords) except ValueError: raise IllegalMoveError(self.game.input_error_msg) def _map_coords(self, input_coords): try: input_x, input_y = input_coords x_coord = self.X_COORD_MAP[str(input_x).lower()] y_coord = self.Y_COORD_MAP[str(input_y)] return f'{x_coord}{y_coord}' except (ValueError, KeyError): raise IllegalMoveError(self.game.input_error_msg) def _align_board_for_display(self): transposed_board = [list(row) for row in zip(*self.game.board)] return list(reversed(transposed_board)) def _display_board_to_terminal(self): self._clear_screen() board = self._align_board_for_display() board.append(self.game.x_axis()) print( tabulate(board, tablefmt="fancy_grid", showindex=self.game.y_axis())) print(self.display_message) def _clear_screen(self): print('\n' * 50)