コード例 #1
0
ファイル: test_pieces.py プロジェクト: AngelVI13/pyviz
def test_queen_moves():
    board = Board()

    # Check move generation when queen is on A1
    queen = Queen(Square.from_notation("a1"), board)

    moves = queen.get_moves()
    assert len(moves) == 21
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("h1") in moves
    assert Square.from_notation("a8") in moves
    assert Square.from_notation("h8") in moves

    # Check move generation when queen is on D4
    queen = Queen(Square.from_notation("d4"), board)

    moves = queen.get_moves()
    assert len(moves) == 27
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("a4") in moves
    assert Square.from_notation("h4") in moves
    assert Square.from_notation("d8") in moves
    assert Square.from_notation("d1") in moves
    assert Square.from_notation("b2") in moves
    assert Square.from_notation("h8") in moves
    assert Square.from_notation("a7") in moves
    assert Square.from_notation("g1") in moves

    # Check move generation when queen is on D4
    # and there is a blockin piece on F4
    queen = Queen(Square.from_notation("d4"), board)
    blocking_bishop1 = Bishop(Square.from_notation("c4"), board)
    blocking_bishop2 = Bishop(Square.from_notation("c3"), board)

    board.add_piece(blocking_bishop1)
    board.add_piece(blocking_bishop2)

    moves = queen.get_moves()
    assert len(moves) == 21
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("h4") in moves
    assert Square.from_notation("e4") in moves
    assert Square.from_notation("d1") in moves
    assert Square.from_notation("d8") in moves
    assert Square.from_notation("f4") in moves
    assert Square.from_notation("b6") in moves
    assert Square.from_notation("g1") in moves
    assert Square.from_notation("a7") in moves
    assert Square.from_notation("a1") not in moves
    assert Square.from_notation("a4") not in moves
コード例 #2
0
ファイル: test_pieces.py プロジェクト: AngelVI13/pyviz
def test_king_moves():
    board = Board()

    # Check move generation when king is on A1
    king = King(Square.from_notation("a1"), board)

    moves = king.get_moves()
    assert len(moves) == 3
    assert all(isinstance(move, Square) for move in moves)
    assert Square(file=0, rank=1) in moves
    assert Square.from_notation("b1") in moves
    assert Square.from_notation("b1") in moves

    # Check move generation when king is on D4
    king = King(Square.from_notation("d4"), board)

    moves = king.get_moves()
    assert len(moves) == 8
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("c3") in moves
    assert Square.from_notation("c4") in moves
    assert Square.from_notation("c5") in moves
    assert Square.from_notation("d3") in moves
    assert Square.from_notation("d5") in moves
    assert Square.from_notation("e3") in moves
    assert Square.from_notation("e4") in moves
    assert Square.from_notation("e5") in moves

    # Check move generation when king is on D4
    # and there is a blockin piece on C5
    king = King(Square.from_notation("d4"), board)
    blocking_bishop = Bishop(Square.from_notation("c5"), board)

    board.add_piece(blocking_bishop)

    moves = king.get_moves()
    assert len(moves) == 7
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("c3") in moves
    assert Square.from_notation("c4") in moves
    assert Square.from_notation("c5") not in moves
    assert Square.from_notation("d3") in moves
    assert Square.from_notation("d5") in moves
    assert Square.from_notation("e3") in moves
    assert Square.from_notation("e4") in moves
    assert Square.from_notation("e5") in moves
コード例 #3
0
ファイル: test_pieces.py プロジェクト: AngelVI13/pyviz
def test_knight_moves():
    board = Board()

    # Check move generation when knight is on A1
    knight = Knight(Square.from_notation("a1"), board)

    moves = knight.get_moves()
    assert len(moves) == 2
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("b3") in moves
    assert Square.from_notation("c2") in moves

    # Check move generation when knight is on D4
    knight = Knight(Square.from_notation("d4"), board)

    moves = knight.get_moves()
    assert len(moves) == 8
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("b3") in moves
    assert Square.from_notation("b5") in moves
    assert Square.from_notation("c6") in moves
    assert Square.from_notation("c2") in moves
    assert Square.from_notation("e2") in moves
    assert Square.from_notation("e6") in moves
    assert Square.from_notation("f5") in moves
    assert Square.from_notation("f3") in moves

    # Check move generation when knight is on D4
    # and there is a blockin piece on C2
    knight = Knight(Square.from_notation("d4"), board)
    blocking_bishop = Bishop(Square.from_notation("c2"), board)

    board.add_piece(blocking_bishop)

    moves = knight.get_moves()
    assert len(moves) == 7
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("b3") in moves
    assert Square.from_notation("b5") in moves
    assert Square.from_notation("c6") in moves
    assert Square.from_notation("c2") not in moves
    assert Square.from_notation("e2") in moves
    assert Square.from_notation("e6") in moves
    assert Square.from_notation("f5") in moves
    assert Square.from_notation("f3") in moves
コード例 #4
0
ファイル: test_board.py プロジェクト: AngelVI13/pyviz
def test_get_piece_that_reaches_square():
    board = Board()

    bishop = Bishop(Square.from_notation("a1"), board)
    knight = Knight(Square.from_notation("b1"), board)

    board.add_piece(bishop)
    board.add_piece(knight)

    square = Square.from_notation("a3")
    assert isinstance(board.get_piece_that_reaches_square(square), Knight)

    square = Square.from_notation("b2")
    assert isinstance(board.get_piece_that_reaches_square(square), Bishop)

    with raises(ValueError):
        square = Square.from_notation("e2")
        _ = board.get_piece_that_reaches_square(square)
コード例 #5
0
ファイル: test_board.py プロジェクト: AngelVI13/pyviz
def test_get_singular_squares():
    board = Board()

    bishop = Bishop(Square.from_notation("a1"), board)
    knight = Knight(Square.from_notation("b1"), board)

    board.add_piece(bishop)
    board.add_piece(knight)

    squares = board.get_singular_squares()
    assert isinstance(squares, list)
    assert all(isinstance(square, Square) for square in squares)
    assert len(squares) == 8
    assert Square.from_notation("b2") in squares
    assert Square.from_notation("h8") in squares
    assert Square.from_notation("c3") not in squares
    assert Square.from_notation("a3") in squares
    assert Square.from_notation("d2") in squares
コード例 #6
0
ファイル: test_pieces.py プロジェクト: AngelVI13/pyviz
def test_rook_moves():
    board = Board()

    # Check move generation when rook is on A1
    rook = Rook(Square.from_notation("a1"), board)

    moves = rook.get_moves()
    assert len(moves) == 14
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("h1") in moves
    assert Square.from_notation("a8") in moves

    # Check move generation when rook is on D4
    rook = Rook(Square.from_notation("d4"), board)

    moves = rook.get_moves()
    assert len(moves) == 14
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("a4") in moves
    assert Square.from_notation("h4") in moves
    assert Square.from_notation("d8") in moves
    assert Square.from_notation("d1") in moves

    # Check move generation when rook is on D4
    # and there is a blockin piece on F4
    rook = Rook(Square.from_notation("d4"), board)
    blocking_bishop = Bishop(Square.from_notation("f4"), board)

    board.add_piece(blocking_bishop)

    moves = rook.get_moves()
    assert len(moves) == 11
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("a4") in moves
    assert Square.from_notation("e4") in moves
    assert Square.from_notation("d1") in moves
    assert Square.from_notation("d8") in moves
    assert Square.from_notation("f4") not in moves
コード例 #7
0
ファイル: test_pieces.py プロジェクト: AngelVI13/pyviz
def test_bishop_moves():
    board = Board()

    # Check move generation when bishop is on A1
    bishop = Bishop(Square.from_notation("a1"), board)

    moves = bishop.get_moves()
    assert len(moves) == 7
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("b2") in moves
    assert Square.from_notation("h8") in moves

    # Check move generation when bishop is on D4
    bishop = Bishop(Square.from_notation("d4"), board)

    moves = bishop.get_moves()
    assert len(moves) == 13
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("b2") in moves
    assert Square.from_notation("h8") in moves
    assert Square.from_notation("a7") in moves
    assert Square.from_notation("g1") in moves

    # Check move generation when bishop is on E4
    # and there is a blockin piece on F5
    bishop = Bishop(Square.from_notation("e4"), board)
    blocking_bishop = Bishop(Square.from_notation("f5"), board)

    board.add_piece(blocking_bishop)

    moves = bishop.get_moves()
    assert len(moves) == 10
    assert all(isinstance(move, Square) for move in moves)
    assert Square.from_notation("b1") in moves
    assert Square.from_notation("h1") in moves
    assert Square.from_notation("a8") in moves
    assert Square.from_notation("h7") not in moves
コード例 #8
0
ファイル: game_state.py プロジェクト: AngelVI13/pyviz
class GameState:
    QUESTIONS_PER_LEVEL = 5
    MAX_LEVEL = 5
    # for every level add the corresponding piece to the board
    LEVELS = {1: Bishop, 2: Knight, 3: Rook, 4: King, 5: Queen}

    def __init__(self):
        self.board = Board()
        self.current_state = States.PRE_GAME
        self.level = 0
        self.score = 0

    def setup_pre_game(self):
        """Reset board and set 2 initial pieces"""

        # reset game info
        self.current_state = States.PLAY
        self.level = 0
        self.board.reset()

        # add initial 2 pieces to the board (knight & bishop)
        all_squares = list(range(64))

        for piece in (Bishop, Knight):
            square = choice(all_squares)
            all_squares.remove(square)
            piece_object = piece(Square.from_index(square), self.board)
            self.board.add_piece(piece_object)

    def print_piece_info(self):
        for piece in self.board.pieces:
            print(
                f"You have a {piece.__class__.__name__} on {piece.square.notation}"
            )

    def generate_new_square(self):
        return choice(self.board.get_singular_squares())

    def generate_new_piece(self, level: int) -> Piece:
        all_squares = list(range(64))
        for piece in self.board.pieces:
            all_squares.remove(piece.square.index)

        square_index = choice(all_squares)
        square = Square.from_index(square_index)

        new_piece_class = self.LEVELS.get(level)
        new_piece = new_piece_class(square, self.board)
        return new_piece

    def update_game(self, guessed_piece: Piece, new_square: Square):
        self.score += 1

        # change position of the piece
        for piece in self.board.pieces:
            if piece is guessed_piece:
                piece.square = new_square

        new_level = self.score // self.QUESTIONS_PER_LEVEL
        if new_level > self.level:
            self.level = new_level

            print(f"------------------")
            print(f"---> LEVEL UP <---")
            print(f"------------------")

            new_piece = self.generate_new_piece(self.level)
            self.board.add_piece(new_piece)
            print(
                f"You got a NEW {new_piece.__class__.__name__} on {new_piece.square.notation}"
            )