Exemple #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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'
Exemple #20
0
    def _new_board_setup():
        x_axis_nums = cycle([0, 2, 4, 6, 1, 3, 5, 7])
        y_axis_nums = [0, 1, 2, 5, 6, 7]
        pieces = {}

        for y_axis_num in y_axis_nums:
            color = Color.WHITE if y_axis_num < 3 else Color.BLACK
            for _ in range(4):
                coords = f'{next(x_axis_nums)}{y_axis_num}'
                pieces[coords] = Counter(color)

        return pieces
Exemple #21
0
    def _capture_coords(self, direction, from_coords):
        capture_coords = NEXT_ADJACENT_COORD[direction](from_coords)
        to_coords = NEXT_ADJACENT_COORD[direction](capture_coords)

        for coords in (capture_coords, to_coords):
            if not self.coords_on_board(coords):
                return None

        capture_square = self.board[capture_coords.x][capture_coords.y]
        move_to_square = self.board[to_coords.x][to_coords.y]

        if capture_square == Counter(self.opponent_color) and move_to_square is None:
            return to_coords
        return None
Exemple #22
0
import pytest

from src.games.game import Coords
from src.game_enums import Color
from src.game_pieces.draughts_counter import Counter

white_counter = Counter(Color.WHITE)
black_counter = Counter(Color.BLACK)


@pytest.mark.parametrize(
    'coords, rt_val',
    [
        (Coords(x=0, y=2), True),
        (Coords(x=2, y=2), True),
        (Coords(x=1, y=0), False),  # can't move horizontally
        (Coords(x=1, y=2), False),  # can't move vertically
        (Coords(x=0, y=0), False),  # can't move backwards
        (Coords(x=3, y=3), False)  # can't move more than one space
    ])
def test_white_counter_legal_move(coords, rt_val):
    white_counter.coords = Coords(x=1, y=1)
    assert white_counter.legal_move(coords) == rt_val


@pytest.mark.parametrize(
    'coords, rt_val',
    [
        (Coords(x=7, y=5), True),
        (Coords(x=5, y=5), True),
        (Coords(x=7, y=6), False),  # can't move horizontally