示例#1
0
def test_queen_moves():
    """
    Places 16 Queens at random positions and plays 100 games with
    random moves to test the mechanics.
    """
    num_games = 100
    num_moves = 300
    for i in range(num_games):
        print("game", i+1)
        board = clean_board()
        poss = np.random.randint(8, size=[16, 2])
        board = clean_board()
        for j in range(8):
            board[poss[j, 0], poss[j, 1]] = Queen(
                (poss[j, 0], poss[j, 1]), "white")
        for j in range(8, 16):
            board[poss[j, 0], poss[j, 1]] = Queen(
                (poss[j, 0], poss[j, 1]), "black")
        queen_board = Board(board)
        game = GameEngine(queen_board, player1="computer", player2="computer")
        for _ in range(num_moves):
            moves = game.all_moves()
            if len(moves) == 0:
                break
            rand_int = np.random.randint(len(moves))
            game.choose_move(moves[rand_int])
示例#2
0
    def _start_pos(self):
        """
        Default Chess Board positioning
        """
        # white pieces
        self.board = clean_board()
        for j in range(8):
            self.board[1, j] = Pawn((1, j), "white")
        self.board[0, 0] = Rook((0, 0), "white")
        self.board[0, 7] = Rook((0, 7), "white")
        self.board[0, 1] = Knight((0, 1), "white")
        self.board[0, 6] = Knight((0, 6), "white")
        self.board[0, 2] = Bishop((0, 2), "white")
        self.board[0, 5] = Bishop((0, 5), "white")
        self.board[0, 4] = King((0, 4), "white")
        self.board[0, 3] = Queen((0, 3), "white")

        # black pieces
        for j in range(8):
            self.board[6, j] = Pawn((6, j), "black")
        self.board[7, 0] = Rook((7, 0), "black")
        self.board[7, 7] = Rook((7, 7), "black")
        self.board[7, 1] = Knight((7, 1), "black")
        self.board[7, 6] = Knight((7, 6), "black")
        self.board[7, 2] = Bishop((7, 2), "black")
        self.board[7, 5] = Bishop((7, 5), "black")
        self.board[7, 4] = King((7, 4), "black")
        self.board[7, 3] = Queen((7, 3), "black")
示例#3
0
def test_stalemate_nomoves():
    """
    Two pawns blocking each other, no more moves so stalemate check
    """
    board = clean_board()
    board[3, 4] = Pawn((3, 4), "white")
    board[5, 4] = Pawn((5, 4), "black")
    pawn_board = Board(board)
    game = GameEngine(pawn_board, player1="computer", player2="computer")
    with pytest.raises(SystemExit):
        for _ in range(2):
            moves = game.all_moves()
            if len(moves) > 0:
                game.choose_move(moves[0])
            else:
                game.choose_move([])
示例#4
0
def test_stalemate_3moves():
    """
    Checks if the stalemate function works when the same state is repeated 3 times
    """
    board = clean_board()
    board[3, 4] = Knight((3, 4), "white")
    board[5, 4] = Knight((5, 4), "black")
    knight_board = Board(board)
    game = GameEngine(knight_board, player1="computer", player2="computer")
    with pytest.raises(SystemExit):
        white_move1 = ((3, 4), (1, 3), 'base')
        white_move2 = ((1, 3), (3, 4), 'base')
        black_move1 = ((5, 4), (3, 3), 'base')
        black_move2 = ((3, 3), (5, 4), 'base')
        for _ in range(9):
            game.choose_move(white_move1)
            game.choose_move(black_move1)
            game.choose_move(white_move2)
            game.choose_move(black_move2)
示例#5
0
def test_enpasse():
    """
    checks if white and black pawns can do enpasse
    """
    board = clean_board()
    board[3, 2] = Pawn((3, 2), "white")
    board[6, 1] = Pawn((6, 1), "black")
    board[3, 4] = Pawn((3, 4), "black")
    board[1, 5] = Pawn((1, 5), "white")
    pawn_board = Board(board)
    game = GameEngine(pawn_board, player1="computer", player2="computer")
    moves = game.all_moves()
    game.choose_move(moves[1])
    moves = game.all_moves()
    game.choose_move(moves[1])
    moves = game.all_moves()
    game.choose_move(moves[0])
    moves = game.all_moves()
    game.choose_move(moves[2])
    moves = game.all_moves()
    game.choose_move(moves[1])
示例#6
0
def test_bishop_moves():
    """
    Places 8 Bishops along two rows and plays 100 games with
    random moves to test the mechanics.
    """
    num_games = 100
    num_moves = 300
    for i in range(num_games):
        print("game", i + 1)
        board = clean_board()
        for j in range(8):
            board[1, j] = Bishop((1, j), "white")
        for j in range(8):
            board[6, j] = Bishop((6, j), "black")
        bishop_board = Board(board)
        game = GameEngine(bishop_board, player1="computer", player2="computer")
        for _ in range(num_moves):
            moves = game.all_moves()
            if len(moves) == 0:
                break
            rand_int = np.random.randint(len(moves))
            game.choose_move(moves[rand_int])