Пример #1
0
def test_an_passant(pawn):  # noqa: D103
    grid = pawn.grid

    p2_pos = pawn.position + Position(0, -1)
    grid.add_piece(pieces.Pawn(p2_pos, pawn.color))

    grid[p2_pos].previous_positions = [None]
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=True,
                                                          right=True)

    grid[p2_pos].color = pawn.color.inverted()
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=True,
                                                          right=True)

    grid[p2_pos].previous_positions = []
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=True,
                                                          right=True)

    p3_pos = pawn.position + Position(0, 1)
    attacked_pos = p3_pos + Position(pawn.get_direction(pawn).value, 0)
    grid.add_piece(pieces.Pawn(p3_pos, pawn.color.inverted()))
    grid[p3_pos].previous_positions = [None]

    other = grid[p3_pos]
    assert grid.move(pawn.position, attacked_pos)
    assert grid.captured == [other]
Пример #2
0
def test_attack_range(pawn):  # noqa: D103
    dir_val = pawn.get_direction(pawn).value
    grid = pawn.grid

    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=True,
                                                          right=True)

    p2_pos = pawn.position + Position(dir_val, -1)
    grid.add_piece(pieces.Pawn(p2_pos, pawn.color))
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=False,
                                                          right=True)

    p3_pos = pawn.position + Position(dir_val, 1)
    grid.add_piece(pieces.Pawn(p3_pos, pawn.color))
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=False,
                                                          right=False)

    grid[p2_pos].color = grid[p2_pos].color.inverted()
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=True,
                                                          right=False)

    grid[p3_pos].color = grid[p3_pos].color.inverted()
    assert pawn.attack_range == get_expected_attack_range(pawn,
                                                          left=True,
                                                          right=True)
Пример #3
0
def test_move_in_check():  # noqa: D103
    grid = Grid()
    r, f = 1, 1
    king = piece_types.King(Position(r, f - 1))
    rook = piece_types.Rook(Position(r, f + 1), color=Color.Black)
    p1 = piece_types.Pawn(Position(r - 1, f))
    p2 = piece_types.Pawn(Position(r - 1, f + 2))

    for piece in (king, rook, p1, p2):
        grid.add_piece(piece)

    assert grid.own_king_in_check(king)

    assert not grid.move(p2.position, p2.position + Position(1, 0))

    assert grid.move(p1.position, p1.position + Position(1, 0))
    assert not grid.own_king_in_check(king)

    p1.position += Position(-1, 0)

    print(king, p2, rook)
    assert grid.own_king_in_check(king)
    assert grid.move(p2.position, rook.position)

    assert not grid.own_king_in_check(king)
Пример #4
0
def test_knight_ranges(color, coords, knight):  # noqa: D103
    knight.position = Position(*coords)
    knight.color = color

    assert sorted(knight.move_range) == sorted(knight.attack_range)

    move_diffs = [(1, 2), (1, -2), (-1, 2), (-1, -2)]
    move_diffs += list(map(reversed, move_diffs))
    move_diffs = map(lambda x: Position(*x), move_diffs)
    moves = map(lambda x: x + knight.position, move_diffs)
    moves = list(filter(lambda x: x.is_valid(), moves))
    assert sorted(knight.move_range) == sorted(moves)

    pos = knight.position + Position(1, 2)
    pawn = pieces.Pawn(pos, color=color)
    knight.grid.add_piece(pawn)

    try:
        moves.remove(pawn.position)
    except ValueError:
        pass
    assert sorted(knight.move_range) == sorted(moves)

    pawn.color = pawn.color.inverted()
    if pawn.position.is_valid():
        moves.append(pawn.position)
    assert sorted(knight.move_range) == sorted(moves)
Пример #5
0
def test_castle_piece_between(color, file_letter, grid_castle):  # noqa: D103
    rank = 1 if color is Color.White else 8

    side = Side.Queenside if file_letter in "bcd" else Side.Kingside

    pawn = piece_types.Pawn(Position.get_pos(f"{file_letter}{rank}"),
                            color=color.White)
    grid_castle.add_piece(pawn)

    assert not grid_castle.castle(color, side)
Пример #6
0
def test_pawn_range_obstruction(pawn):  # noqa: D103
    grid = pawn.grid
    assert pawn.move_range == get_expected_move_range(pawn, two_positions=True)

    p2 = pieces.Pawn(pawn.position + Position(1, 0), pawn.color)
    grid.add_piece(p2)
    assert pawn.move_range == []

    assert grid.move(p2.position, p2.position + Position(1, 0))
    assert pawn.move_range == get_expected_move_range(pawn, False)

    assert grid.move(p2.position, p2.position + Position(1, 0))
    assert pawn.move_range == get_expected_move_range(pawn, True)

    pawn.move(pawn.position + Position(1, 0))
    assert pawn.move_range == get_expected_move_range(pawn, False)

    p3 = pieces.Pawn(pawn.position + Position(2, 0), pawn.color.inverted())
    grid.add_piece(p3)
    assert pawn.move_range == get_expected_move_range(pawn, False)

    assert grid.move(p3.position, p3.position + Position(-1, 0))
    assert pawn.move_range == []
Пример #7
0
def test_revert_move(grid):  # noqa: D103
    grids = []
    p1 = piece_types.Pawn(Position.get_pos("a2"))
    p2 = piece_types.Pawn(Position.get_pos("b2"))
    grid.add_piece(p1)
    grid.add_piece(p2)

    assert not grid.revert_move()

    grid.move(p1.position, p1.position + Position(2, 0))
    grids.append(deepcopy(grid))
    grid.move(p2.position, p2.position + Position(1, 0))
    grids.append(deepcopy(grid))
    grid.move(p1.position, p1.position + Position(1, 0))
    grids.append(deepcopy(grid))
    grid.move(p2.position, p2.position + Position(1, 0))

    assert grids[-1] != grid

    while grids:
        snapshot = grids.pop()
        assert grid.revert_move()
        assert grid == snapshot
Пример #8
0
def pawn():  # noqa: D103
    grid = Grid()
    pawn = pieces.Pawn(Position(1, 1))
    grid.add_piece(pawn)
    return pawn