Exemple #1
0
def test_not_on_board_error():
    err_msg = 'To_coordinates not legal board coordidnates'
    try:
        raise NotOnBoardError(Coords(x='z', y='10'), err_msg)
    except NotOnBoardError as err:
        assert err.coords == Coords(x='z', y='10')
        assert err.message == err_msg
Exemple #2
0
def test_same_from_and_to_coords_raises_exception():
    game = Draughts({
        '66': Counter(Color.BLACK),
    })

    with pytest.raises(IllegalMoveError, match=game.SAME_SQUARE):
        game.move(Coords(x=6, y=6), Coords(x=6, y=6))
Exemple #3
0
def test_move_raises_error_if_capture_possible_for_piece():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
    })

    with pytest.raises(IllegalMoveError, match=game.CAPTURE_POSSIBLE):
        game.move(Coords(x=6, y=6), Coords(x=7, y=5))
Exemple #4
0
def test_draughts_piece_can_move():
    game = Draughts()
    game.playing_color = Color.WHITE
    assert game.board[5][3] is None

    game.move(Coords(x=4, y=2), Coords(x=5, y=3))
    assert game.board[4][2] is None
    assert game.board[5][3] == Counter(Color.WHITE)
Exemple #5
0
def test_illegal_capture_raises_exception():
    game = Draughts({
        '00': Counter(Color.WHITE),
    })
    game.playing_color = Color.WHITE

    with pytest.raises(IllegalMoveError, match=game.ILLEGAL_CAPTURE):
        game.move(Coords(x=0, y=0), Coords(x=2, y=2))
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
def test_crowned_black_piece_can_capture_backwards():
    game = Draughts({
        '55': Counter(Color.BLACK),
        '66': Counter(Color.WHITE),
    })
    game.board[5][5].crowned = True

    game.move(Coords(x=5, y=5), Coords(x=7, y=7))
    assert game.board[6][6] is None
    assert game.board[7][7] == Counter(Color.BLACK)
Exemple #9
0
 def _legal_en_passant(self, to_coords):
     if not self.last_move_pawn:
         return False
     if self.playing_piece == Pawn(Color.WHITE) and to_coords.y == 5:
         return Coords(to_coords.x,
                       to_coords.y - 1) == self.last_move_pawn.coords
     if self.playing_piece == Pawn(Color.BLACK) and to_coords.y == 2:
         return Coords(to_coords.x,
                       to_coords.y + 1) == self.last_move_pawn.coords
     return False
Exemple #10
0
def test_draughts_piece_can_capture():
    game = Draughts({
        '00': Counter(Color.WHITE),
        '11': Counter(Color.BLACK),
    })
    game.playing_color = Color.WHITE

    game.move(Coords(x=0, y=0), Coords(x=2, y=2))
    assert game.board[1][1] is None
    assert game.board[2][2] == Counter(Color.WHITE)
Exemple #11
0
def test_move_raises_error_if_capture_possible_for_crowned_piece():
    game = Draughts({
        '77': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '64': Counter(Color.BLACK)
    })
    game.board[6][4].crowned = True

    with pytest.raises(IllegalMoveError, match=game.CAPTURE_POSSIBLE):
        game.move(Coords(x=7, y=7), Coords(x=6, y=6))
Exemple #12
0
def test_black_can_capture_two_pieces_in_multiple_direction():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '53': Counter(Color.WHITE),
    })

    game.move(Coords(x=6, y=6), Coords(x=6, y=2))
    assert game.board[5][5] is None
    assert game.board[5][3] is None
    assert game.board[6][2] == Counter(Color.BLACK)
Exemple #13
0
def test_crowned_white_piece_can_capture_backwards():
    game = Draughts({
        '22': Counter(Color.WHITE),
        '11': Counter(Color.BLACK),
    })
    game.playing_color = Color.WHITE
    game.board[2][2].crowned = True

    game.move(Coords(x=2, y=2), Coords(x=0, y=0))
    assert game.board[1][1] is None
    assert game.board[0][0] == Counter(Color.WHITE)
Exemple #14
0
def test_can_capture_two_pieces_in_multiple_direction():
    game = Draughts({
        '00': Counter(Color.WHITE),
        '11': Counter(Color.BLACK),
        '13': Counter(Color.BLACK),
    })
    game.playing_color = Color.WHITE

    game.move(Coords(x=0, y=0), Coords(x=0, y=4))
    assert game.board[1][1] is None
    assert game.board[1][3] is None
    assert game.board[0][4] == Counter(Color.WHITE)
Exemple #15
0
def test_can_capture_two_pieces_in_straight_line():
    game = Draughts({
        '00': Counter(Color.WHITE),
        '11': Counter(Color.BLACK),
        '33': Counter(Color.BLACK),
    })
    game.playing_color = Color.WHITE

    game.move(Coords(x=0, y=0), Coords(x=4, y=4))
    assert game.board[1][1] is None
    assert game.board[3][3] is None
    assert game.board[4][4] == Counter(Color.WHITE)
Exemple #16
0
def test_crowned_piece_can_take_two_pieces_and_return_to_same_y_index():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '35': Counter(Color.WHITE),
    })
    game.board[6][6].crowned = True

    game.move(Coords(x=6, y=6), Coords(x=2, y=6))
    assert game.board[5][5] is None
    assert game.board[3][5] is None
    assert game.board[2][6] == Counter(Color.BLACK)
Exemple #17
0
def test_one_piece_capture_forced_to_make_two_move_capture_if_possible():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '33': Counter(Color.WHITE),
    })

    game.move(Coords(x=6, y=6), Coords(x=4, y=4))
    assert game.board[6][6] is None
    assert game.board[5][5] is None
    assert game.board[4][4] is None
    assert game.board[3][3] is None
    assert game.board[2][2] == Counter(Color.BLACK)
Exemple #18
0
def test_can_capture_three_pieces_in_straight_line():
    game = Draughts({
        '77': Counter(Color.BLACK),
        '66': Counter(Color.WHITE),
        '44': Counter(Color.WHITE),
        '22': Counter(Color.WHITE),
    })

    game.move(Coords(x=7, y=7), Coords(x=1, y=1))
    assert game.board[6][6] is None
    assert game.board[4][4] is None
    assert game.board[2][2] is None
    assert game.board[1][1] == Counter(Color.BLACK)
Exemple #19
0
def test_black_can_capture_three_pieces_in_multiple_directions_s_shape():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '53': Counter(Color.WHITE),
        '51': Counter(Color.WHITE),
    })

    game.move(Coords(x=6, y=6), Coords(x=4, y=0))
    assert game.board[5][5] is None
    assert game.board[5][3] is None
    assert game.board[5][1] is None
    assert game.board[4][0] == Counter(Color.BLACK)
Exemple #20
0
def test_white_can_capture_three_pieces_in_multiple_directions_l_shape():
    game = Draughts({
        '60': Counter(Color.WHITE),
        '51': Counter(Color.BLACK),
        '33': Counter(Color.BLACK),
        '35': Counter(Color.BLACK),
    })
    game.playing_color = Color.WHITE

    game.move(Coords(x=6, y=0), Coords(x=4, y=6))
    assert game.board[5][1] is None
    assert game.board[3][3] is None
    assert game.board[3][5] is None
    assert game.board[4][6] == Counter(Color.WHITE)
Exemple #21
0
def test_two_extra_captures_forced_if_possible():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '33': Counter(Color.WHITE),
        '11': Counter(Color.WHITE),
    })

    game.move(Coords(x=6, y=6), Coords(x=4, y=4))
    assert game.board[6][6] is None
    assert game.board[5][5] is None
    assert game.board[3][3] is None
    assert game.board[1][1] is None
    assert game.board[0][0] == Counter(Color.BLACK)
Exemple #22
0
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
Exemple #23
0
def test_two_piece_capture_force_to_make_three_move_capture_if_possible():
    game = Draughts({
        '66': Counter(Color.BLACK),
        '55': Counter(Color.WHITE),
        '33': Counter(Color.WHITE),
        '11': Counter(Color.WHITE),
    })

    game.move(Coords(x=6, y=6), Coords(x=2, y=2))
    assert game.board[6][6] is None
    assert game.board[5][5] is None
    assert game.board[3][3] is None
    assert game.board[2][2] is None
    assert game.board[1][1] is None
    assert game.board[0][0] == Counter(Color.BLACK)
Exemple #24
0
 def _empty_square_coords(self):
     empty_square_coords = []
     for x_idx, row in enumerate(self.board):
         for y_idx, disc in enumerate(row):
             if not disc:
                 empty_square_coords.append(Coords(x_idx, y_idx))
     return empty_square_coords
Exemple #25
0
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
Exemple #26
0
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
Exemple #27
0
 def _castle_coords(self):
     if self._white_king_row():
         if self._queen_side():
             castle_coords = (Coords(x=3, y=0), Coords(x=2, y=0))
         if self._king_side():
             castle_coords = (Coords(x=5, y=0), Coords(x=6, y=0))
     if self._black_king_row():
         if self._queen_side():
             castle_coords = (Coords(x=3, y=7), Coords(x=2, y=7))
         if self._king_side():
             castle_coords = (Coords(x=5, y=7), Coords(x=6, y=7))
     return castle_coords
Exemple #28
0
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
Exemple #29
0
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
Exemple #30
0
def test_counters_can_be_crowned():
    game = Draughts({
        '16': Counter(Color.WHITE),
        '11': Counter(Color.BLACK),
    })
    black_piece = game.board[1][1]
    white_piece = game.board[1][6]

    assert str(black_piece) == '\u26C2'
    assert not black_piece.crowned

    game.move(Coords(x=1, y=1), Coords(x=0, y=0))
    assert black_piece.crowned
    assert str(black_piece) == '\u26C3'

    assert str(white_piece) == '\u26C0'
    assert not white_piece.crowned

    game.move(Coords(x=1, y=6), Coords(x=0, y=7))
    assert white_piece.crowned
    assert str(white_piece) == '\u26C1'