예제 #1
0
def test_king_valid_start_moves():
    board = Board()
    king = King(4, 4, 'w')
    king.valid_moves(board)
    actual = king.move_list
    expected = [[3, 4], [5, 4], [4, 3], [4, 5], [5, 3], [5, 5], [3, 3], [3, 5]]
    assert actual == expected
예제 #2
0
def test_king_attack():
    board = Board()
    king = King(4, 4, 'w')
    board.board[4][4] = king
    board.board[3][4] = Pawn(3, 4, 'b')
    king.valid_moves(board)
    actual = king.attack_list
    expected = [[3, 4]]
    assert actual == expected
예제 #3
0
def test_white_king_castling():
    board = Board()
    king = King(7, 4, 'b')
    board.board[7][4] = king
    board.board[7][7] = Rook(7, 7, 'w')
    board.board[7][0] = Rook(7, 0, 'w')
    king.valid_moves(board)
    actual = king.move_list
    expected = [[6, 4], [7, 3], [7, 5], [6, 3], [6, 5], [7, 6], [7, 2]]
    assert actual == expected
예제 #4
0
def test_black_king_castling():
    board = Board()
    king = King(0, 4, 'b')
    board.board[0][4] = king
    board.board[0][7] = Rook(0, 7, 'b')
    board.board[0][0] = Rook(0, 0, 'b')
    king.valid_moves(board)
    actual = king.move_list
    expected = [[1, 4], [0, 3], [0, 5], [1, 3], [1, 5], [0, 6], [0, 2]]
    assert actual == expected
예제 #5
0
def test_checkmate_false():
    board = Board()
    king = King(7, 0, 'w')
    board.board[7][0] = king
    print(board.board[7][0])
    rook = Rook(5, 0, 'b')
    board.board[5][0] = rook
    king.valid_moves(board)
    actual = board.checkmate_status()
    expected = False
    assert actual == expected
예제 #6
0
def test_white_king_cannot_castling():
    board = Board()
    king = King(7, 4, 'w')
    board.board[7][4] = king
    board.board[7][7] = Rook(7, 7, 'w')
    board.board[7][6] = Knight(7, 6, 'w')
    board.board[7][3] = Bishop(7, 3, 'w')
    board.board[7][0] = Rook(7, 0, 'w')
    king.valid_moves(board)
    actual = king.move_list
    expected = [[6, 4], [7, 5], [6, 3], [6, 5]]
    assert actual == expected
예제 #7
0
def test_psuedo_move():
    board = Board()
    king = King(7, 0, 'w')
    board.board[7][0] = king
    queen = Queen(6, 0, 'b')
    board.board[6][0] = queen
    rook = Rook(5, 0, 'b')
    board.board[5][0] = rook
    queen.valid_moves(board)
    rook.valid_moves(board)
    king.valid_moves(board)

    actual = king.psuedo_move([6, 0], board)
    print('rook attacks', rook.attack_list)
    expected = False
    assert actual == expected
예제 #8
0
def test_checkmate_true():
    board = Board()
    king = King(7, 0, 'w')
    board.board[7][0] = king
    queen = Queen(6, 0, 'b')
    board.board[6][0] = queen
    rook = Rook(5, 0, 'b')
    board.board[5][0] = rook
    queen.valid_moves(board)
    rook.valid_moves(board)

    print('king position', queen.attack_list)
    king.valid_moves(board)
    actual = board.checkmate_status()
    print('king attacks', king.attack_list)
    print('rook attacks', rook.attack_list)
    expected = True
    assert actual == expected
예제 #9
0
파일: board.py 프로젝트: pranavmodx/ChessX
    def is_controlled_sq(self, req_pos, turn):
        '''
        Checks whether a square is controlled by a piece.
        'turn' means controlled by piece of which colour/turn
        '''

        # Along knight routes (L)
        knight = Knight(0)
        knight.set_pos(req_pos)
        # print(self.get_sq_notation(req_pos))
        # print()
        for move in knight.valid_moves():
            # print(self.get_sq_notation(move), end=': ')
            piece = self.fetch_piece(move)
            # print(piece)
            if piece and type(piece).__name__ == 'Knight' and piece.colour == turn:
                # self.pos_of_piece_causing_check = move
                # print('yeah')
                del knight
                return True

        # Along diagonals
        bishop = Bishop(0)
        bishop.set_pos(req_pos)
        for move in bishop.valid_moves():
            piece = self.fetch_piece(move)

            if piece and \
            (type(piece).__name__ == 'Bishop' or
            type(piece).__name__ == 'Queen') and \
            piece.colour == turn and \
            not piece.is_path_obstructed(self, req_pos, move):
                # print('yes')
                # self.pos_of_piece_causing_check = move
                del bishop
                # print('Obstruct')
                return True

        # Along ranks and files
        rook = Rook(0)
        rook.set_pos(req_pos)
        for move in rook.valid_moves():
            # print(self.get_sq_notation(move), end=": ")
            piece = self.fetch_piece(move)
            # print(piece)

            if piece and (type(piece).__name__ == 'Rook' or
            type(piece).__name__ == 'Queen') and \
            piece.colour == turn and \
            not piece.is_path_obstructed(self, req_pos, move):
                # self.pos_of_piece_causing_check = move
                # print('here')
                del rook
                return True

        # King opposition
        king = King()
        king.set_pos(req_pos)
        for move in king.valid_moves():
            piece = self.fetch_piece(move)
            if piece and type(piece).__name__ == 'King' and piece.colour == turn:
                if not self.is_controlled_sq(move, self.turn):
                    # print('here')
                    # self.pos_of_piece_causing_check = move
                    del king
                    return True

        # Pawns
        if turn == 'White':
            pawn = Pawn(colour='Black')
        else:
            pawn = Pawn()
        pawn.set_pos(req_pos)
        for move in pawn.valid_moves(self.is_flipped):
            # If directly in front/back
            if move[0] - req_pos[0] == 0:
                continue
            piece = self.fetch_piece(move)
            if piece and type(piece).__name__ == 'Pawn' and piece.colour == turn:
                # self.pos_of_piece_causing_check = move
                del pawn
                return True

        del knight, bishop, rook, king, pawn

        # print('Nope')
        return False