예제 #1
0
def king_castling_check_test():
    print("========================================")
    print("Performing the king castling check test")
    print("========================================")
    board = Board()
    piece = King(Colour.WHITE)
    current_location = Location('e', 1)
    board.__add_piece__(piece, current_location)
    board.__add_piece__(Rook(Colour.WHITE), Location('a', 1))
    board.__add_piece__(Rook(Colour.WHITE), Location('h', 1))
    board.__add_piece__(Rook(Colour.BLACK), Location('e', 2))
    print(board)

    allowed_moves = piece.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(piece, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(piece.name, new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(piece.allowed_moves(current_location, board))
예제 #2
0
 def __init__(self):
     self.board = [[], [], [], [], [], [], [], []]
     self.board[0].append(Rook(0, 0, "black"))
     self.board[1].append(Knight(1, 0, "black"))
     self.board[2].append(Bishop(2, 0, "black"))
     self.board[3].append(Queen(3, 0, "black"))
     self.board[4].append(King(4, 0, "black"))
     self.board[5].append(Bishop(5, 0, "black"))
     self.board[6].append(Knight(6, 0, "black"))
     self.board[7].append(Rook(7, 0, "black"))
     for i in range(8):
         self.board[i].append(Pawn(i, 1, "black"))
     for i in range(4):
         for j in range(8):
             self.board[j].append(NoPiece(i + 2, j))
     for i in range(8):
         self.board[i].append(Pawn(i, 6, "white"))
     self.board[0].append(Rook(0, 7, "white"))
     self.board[1].append(Knight(1, 7, "white"))
     self.board[2].append(Bishop(2, 7, "white"))
     self.board[3].append(Queen(3, 7, "white"))
     self.board[4].append(King(4, 7, "white"))
     self.board[5].append(Bishop(5, 7, "white"))
     self.board[6].append(Knight(6, 7, "white"))
     self.board[7].append(Rook(7, 7, "white"))
예제 #3
0
def parse(matrix: List[List[str]]) -> List[List[Field]]:
    """
    Parses matrix of strings to matrix of fields and pieces on chessboard.
    :param matrix: matrix of strings containing string representations of pieces and fields.
    :return: matrix of fields and chess pieces representing chessboard.
    """
    chessboard = list()

    for i in range(len(matrix)):
        chessboard.append(list())
        for j in range(len(matrix[i])):
            chessboard[i].append({
                '--': Field(i, j),
                'wW': King(i, j, Color.WHITE),
                'bW': King(i, j, Color.BLACK),
                'wq': Queen(i, j, Color.WHITE),
                'bq': Queen(i, j, Color.BLACK),
                'wb': Bishop(i, j, Color.WHITE),
                'bb': Bishop(i, j, Color.BLACK),
                'wk': Knight(i, j, Color.WHITE),
                'bk': Knight(i, j, Color.BLACK),
                'wr': Rook(i, j, Color.WHITE),
                'br': Rook(i, j, Color.BLACK),
                'wp': Pawn(i, j, Color.WHITE),
                'bp': Pawn(i, j, Color.BLACK)
            }[matrix[i][j]])
    return chessboard
예제 #4
0
 def loadPieces(self, n, p):
     if (p == 'K'):
         self.gameTiles[n] = Tile(n, King("Black", n))
     elif (p == 'k'):
         self.gameTiles[n] = Tile(n, King("White", n))
     elif (p == 'Q'):
         self.gameTiles[n] = Tile(n, Queen("Black", n))
     elif (p == 'q'):
         self.gameTiles[n] = Tile(n, Queen("White", n))
     elif (p == 'N'):
         self.gameTiles[n] = Tile(n, Knight("Black", n))
     elif (p == 'n'):
         self.gameTiles[n] = Tile(n, Knight("White", n))
     elif (p == 'B'):
         self.gameTiles[n] = Tile(n, Bishop("Black", n))
     elif (p == 'b'):
         self.gameTiles[n] = Tile(n, Bishop("White", n))
     elif (p == 'R'):
         self.gameTiles[n] = Tile(n, Rook("Black", n))
     elif (p == 'r'):
         self.gameTiles[n] = Tile(n, Rook("White", n))
     elif (p == 'P'):
         self.gameTiles[n] = Tile(n, Pawn("Black", n))
     elif (p == 'p'):
         self.gameTiles[n] = Tile(n, Pawn("White", n))
     else:
         self.gameTiles[n] = Tile(n, NullPiece())
예제 #5
0
파일: board.py 프로젝트: HourGlss/Chess
    def custom_piece_placement(self):
        color = "white"
        p = Pawn(color)
        n = Knight(color)
        r = Rook(color)
        q = Queen(color)
        b = Bishop(color)
        k = King(color)

        self.board[7][0].add_piece(r)
        self.board[4][0].add_piece(k)
        self.board[0][0].add_piece(r)
        self.board[0][1].add_piece(p)

        color = "black"
        p = Pawn(color)
        n = Knight(color)
        r = Rook(color)
        q = Queen(color)
        b = Bishop(color)
        k = King(color)
        self.board[2][3].add_piece(p)
        self.board[5][6].add_piece(p)
        self.board[6][7].add_piece(q)
        self.board[1][5].add_piece(p)
예제 #6
0
파일: board.py 프로젝트: simon-frisk/Chess
def init_pieces(width):
    return [
        Rook(0, 0, PieceColor.BLACK, width),
        Knight(0, 1, PieceColor.BLACK, width),
        Bishop(0, 2, PieceColor.BLACK, width),
        Queen(0, 3, PieceColor.BLACK, width),
        King(0, 4, PieceColor.BLACK, width),
        Bishop(0, 5, PieceColor.BLACK, width),
        Knight(0, 6, PieceColor.BLACK, width),
        Rook(0, 7, PieceColor.BLACK, width),
        Pawn(1, 0, PieceColor.BLACK, width),
        Pawn(1, 1, PieceColor.BLACK, width),
        Pawn(1, 2, PieceColor.BLACK, width),
        Pawn(1, 3, PieceColor.BLACK, width),
        Pawn(1, 4, PieceColor.BLACK, width),
        Pawn(1, 5, PieceColor.BLACK, width),
        Pawn(1, 6, PieceColor.BLACK, width),
        Pawn(1, 7, PieceColor.BLACK, width),
        Pawn(6, 0, PieceColor.WHITE, width),
        Pawn(6, 1, PieceColor.WHITE, width),
        Pawn(6, 2, PieceColor.WHITE, width),
        Pawn(6, 3, PieceColor.WHITE, width),
        Pawn(6, 4, PieceColor.WHITE, width),
        Pawn(6, 5, PieceColor.WHITE, width),
        Pawn(6, 6, PieceColor.WHITE, width),
        Pawn(6, 7, PieceColor.WHITE, width),
        Rook(7, 0, PieceColor.WHITE, width),
        Knight(7, 1, PieceColor.WHITE, width),
        Bishop(7, 2, PieceColor.WHITE, width),
        Queen(7, 3, PieceColor.WHITE, width),
        King(7, 4, PieceColor.WHITE, width),
        Bishop(7, 5, PieceColor.WHITE, width),
        Knight(7, 6, PieceColor.WHITE, width),
        Rook(7, 7, PieceColor.WHITE, width),
    ]
예제 #7
0
 def setUp(self):
     self.king_white = King(ChessCoord('E', '6'), white)
     self.king_black = King(ChessCoord('G', '5'), black)
     self.bishop_black = Bishop(ChessCoord('B', '4'), black)
     self.rook_white = Bishop(ChessCoord('A', '2'), white)
     self.pieces = [self.king_black, self.king_white,
                    self.bishop_black, self.rook_white]
     self.move_inspect_result_no_piece = MoveInspectResult(True, False, [], None)
예제 #8
0
 def __init__(self):
     self.state = [
         [
             Lance(p1),
             Knight(p1),
             Silver(p1),
             Gold(p1),
             King(p1),
             Gold(p1),
             Silver(p1),
             Knight(p1),
             Lance(p1)
         ],
         [None,
          Rook(p1), None, None, None, None, None,
          Bishop(p1), None],
         [
             Pawn(p1),
             Pawn(p1),
             Pawn(p1),
             Pawn(p1),
             Pawn(p1),
             Pawn(p1),
             Pawn(p1),
             Pawn(p1),
             Pawn(p1)
         ],
         [None, None, None, None, None, None, None, None, None],
         [None, None, None, None, None, None, None, None, None],
         [None, None, None, None, None, None, None, None, None],
         [
             Pawn(p2),
             Pawn(p2),
             Pawn(p2),
             Pawn(p2),
             Pawn(p2),
             Pawn(p2),
             Pawn(p2),
             Pawn(p2),
             Pawn(p2)
         ],
         [None,
          Bishop(p2), None, None, None, None, None,
          Rook(p2), None],
         [
             Lance(p2),
             Knight(p2),
             Silver(p2),
             Gold(p2),
             King(p2),
             Gold(p2),
             Silver(p2),
             Knight(p2),
             Lance(p2)
         ],
     ]
예제 #9
0
    def test_castling_should_be_possible_for_white_king_h1_rook(self):
        e1_king = King(ChessCoord('E', '1'), white)
        h1_rook = Rook(ChessCoord('H', '1'), white)
        pieces = [e1_king, h1_rook]
        inspect_move_result = e1_king.inspect_move(pieces,
                                                        ChessCoord('G', '1'))

        self.failUnless(inspect_move_result ==
                        CastlingMoveInspectResult(True, False, True, False, h1_rook,
                                                  ChessCoord('F', '1')))
예제 #10
0
    def test_castling_should_not_be_possible_if_rook_has_moved(self):
        e1_king = King(ChessCoord('E', '1'), white)
        h1_rook = Rook(ChessCoord('H', '1'), white)
        h1_rook.update_coord(ChessCoord('H', '2'))
        pieces = [e1_king, h1_rook]
        inspect_move_result = e1_king.inspect_move(pieces, ChessCoord('G', '1'))

        self.failUnless(inspect_move_result ==
                        CastlingMoveInspectResult(False, False, True, False, None,
                                                  ChessCoord('F', '1')))
예제 #11
0
    def test_castling_not_possible_if_in_check(self):
        e1_king = King(ChessCoord('E', '1'), white)
        h1_rook = Rook(ChessCoord('H', '1'), white)
        e4_rook_black = Rook(ChessCoord('E', '4'), black)
        pieces = [e1_king, h1_rook, e4_rook_black]
        inspect_move_result = e1_king.inspect_move(pieces,
                                                   ChessCoord('G', '1'))

        self.failUnless(inspect_move_result ==
                        CastlingMoveInspectResult(False, False, True, False, h1_rook,
                                                  ChessCoord('F', '1')))
예제 #12
0
    def test_castling_invalid_enemy_piece_attacks_square_in_between(self):
        e1_king = King(ChessCoord('E', '1'), white)
        h1_rook = Rook(ChessCoord('H', '1'), white)
        f5_rook = Rook(ChessCoord('F', '5'), black)
        pieces = [e1_king, h1_rook, f5_rook]
        inspect_move_result = e1_king.inspect_move(pieces,
                                                   ChessCoord('G', '1'))

        self.failUnless(inspect_move_result ==
                        CastlingMoveInspectResult(False, False, True, True, h1_rook,
                                                  ChessCoord('F', '1')))
예제 #13
0
    def create_board(self):

        # A chess board is constitued of 64 tiles
        for tile in range(64):
            self.gameTiles[tile] = Tile(tile, NullPiece())

        # Placing the pieces on the board

        # Placing black pices

        # First Line
        self.gameTiles[0] = Tile(0, Rook("Black", 0))
        self.gameTiles[1] = Tile(1, Knight("Black", 1))
        self.gameTiles[2] = Tile(2, Bishop("Black", 2))
        self.gameTiles[3] = Tile(3, Queen("Black", 3))
        self.gameTiles[4] = Tile(4, King("Black", 4))
        self.gameTiles[5] = Tile(5, Bishop("Black", 5))
        self.gameTiles[6] = Tile(6, Knight("Black", 6))
        self.gameTiles[7] = Tile(7, Rook("Black", 7))
        self.gameTiles[8] = Tile(8, Pawn("Black", 8))
        self.gameTiles[9] = Tile(9, Pawn("Black", 9))

        # Second Line
        self.gameTiles[10] = Tile(10, Pawn("Black", 10))
        self.gameTiles[11] = Tile(11, Pawn("Black", 11))
        self.gameTiles[12] = Tile(12, Pawn("Black", 12))
        self.gameTiles[13] = Tile(13, Pawn("Black", 13))
        self.gameTiles[14] = Tile(14, Pawn("Black", 14))
        self.gameTiles[15] = Tile(15, Pawn("Black", 15))

        # Placing the white pieces

        # First line
        self.gameTiles[48] = Tile(48, Pawn("White", 48))
        self.gameTiles[49] = Tile(49, Pawn("White", 49))
        self.gameTiles[50] = Tile(50, Pawn("White", 50))
        self.gameTiles[51] = Tile(51, Pawn("White", 51))
        self.gameTiles[52] = Tile(52, Pawn("White", 52))
        self.gameTiles[53] = Tile(53, Pawn("White", 53))
        self.gameTiles[54] = Tile(54, Pawn("White", 54))
        self.gameTiles[55] = Tile(55, Pawn("White", 55))

        # Second line
        self.gameTiles[56] = Tile(56, Rook("White", 56))
        self.gameTiles[57] = Tile(57, Knight("White", 57))
        self.gameTiles[58] = Tile(58, Bishop("White", 58))
        self.gameTiles[59] = Tile(59, Queen("White", 59))
        self.gameTiles[60] = Tile(60, King("White", 60))
        self.gameTiles[61] = Tile(61, Bishop("White", 61))
        self.gameTiles[62] = Tile(62, Knight("White", 62))
        self.gameTiles[63] = Tile(63, Rook("White", 63))
예제 #14
0
 def __init__(self, win, tiles, width, height):
     self.win = win
     self.overlay = pygame.Surface((width, height))
     self.tiles = tiles
     self.pieces = Pieces(win)
     self.width = width
     self.height = height
     self.colorToMove = 0
     self.flipped = False
     self.flipOnEveryMove = False
     self.tempBoard = []
     self.kings = [King(4, 0, self.tiles, 0), King(4, 7, self.tiles, 1)]
     self.previousBoards = []
     self.moves = []
예제 #15
0
    def createBoard(self):

        for c in range(65):
            self.gameTiles.append(None)
        
        for tile in range(65):
            self.gameTiles[tile] = Tile(tile, NullPiece())
        

        self.gameTiles[1] = Tile(1, Rook(1, "Black"))
        self.gameTiles[2] = Tile(2, Knight(2, "Black"))
        self.gameTiles[3] = Tile(3, Bishop(3, "Black"))
        self.gameTiles[4] = Tile(4, Queen(4, "Black"))
        self.gameTiles[5] = Tile(5, King(5, "Black"))
        self.gameTiles[6] = Tile(6, Bishop(6, "Black"))
        self.gameTiles[7] = Tile(7, Knight(7, "Black"))
        self.gameTiles[8] = Tile(8, Rook(8, "Black"))
        self.gameTiles[9] = Tile(9, Pawn(9, "Black"))
        self.gameTiles[10] = Tile(10, Pawn(10, "Black"))
        self.gameTiles[11] = Tile(11, Pawn(11, "Black"))
        self.gameTiles[12] = Tile(12, Pawn(12, "Black"))
        self.gameTiles[13] = Tile(13, Pawn(13, "Black"))
        self.gameTiles[14] = Tile(14, Pawn(14, "Black"))
        self.gameTiles[15] = Tile(15, Pawn(15, "Black"))
        self.gameTiles[16] = Tile(16, Pawn(16, "Black"))


        
        #self.gameTiles[35] = Tile(35, Bishop(35, "White"))

        #self.gameTiles[14] = Tile(14,Queen(14, "White"))

        self.gameTiles[49] = Tile(49, Pawn(49, "White"))
        self.gameTiles[50] = Tile(50, Pawn(50, "White"))
        self.gameTiles[51] = Tile(51, Pawn(51, "White"))
        self.gameTiles[52] = Tile(52, Pawn(52, "White"))
        self.gameTiles[53] = Tile(53, Pawn(53, "White"))
        self.gameTiles[54] = Tile(54, Pawn(54, "White"))
        self.gameTiles[55] = Tile(55, Pawn(55, "White"))
        self.gameTiles[56] = Tile(56, Pawn(56, "White"))
        self.gameTiles[57] = Tile(57, Rook(57, "White"))
        self.gameTiles[58] = Tile(58, Knight(58, "White"))
        self.gameTiles[59] = Tile(59, Bishop(59, "White"))
        self.gameTiles[60] = Tile(60, Queen(60, "White"))
        self.gameTiles[61] = Tile(61, King(61, "White"))
        self.gameTiles[62] = Tile(62, Bishop(62, "White"))
        self.gameTiles[63] = Tile(63, Knight(63, "White"))
        self.gameTiles[64] = Tile(64, Rook(64, "White"))
예제 #16
0
 def place_pieces(self, color):
     column = 0
     color_string = "black"
     if color == Color.WHITE:
         column = 7
         color_string = "white"
     self.board[column][0] = Rook(color, column, 0,
                                  images[color_string + "_rook"])
     self.board[column][1] = Knight(color, column, 1,
                                    images[color_string + "_knight"])
     self.board[column][2] = Bishop(color, column, 2,
                                    images[color_string + "_bishop"])
     self.board[column][3] = Queen(color, column, 3,
                                   images[color_string + "_queen"])
     self.board[column][4] = King(color, column, 4,
                                  images[color_string + "_king"])
     self.board[column][5] = Bishop(color, column, 5,
                                    images[color_string + "_bishop"])
     self.board[column][6] = Knight(color, column, 6,
                                    images[color_string + "_knight"])
     self.board[column][7] = Rook(color, column, 7,
                                  images[color_string + "_rook"])
     if color == Color.BLACK:
         column = 1
     else:
         column = 6
     for i in range(0, NUM_SQUARES):
         self.board[column][i] = Pawn(color, column, i,
                                      images[color_string + "_pawn"])
예제 #17
0
    def test_king(self):
        b = board.Board()

        b.set_piece((1, 1), board.Piece(board.PieceType.KING, True))

        self.assertItemsEqual([(0, 0), (0, 1), (0, 2), (1, 0), (1, 2), (2, 0),
                               (2, 1), (2, 2)],
                              King.get_moves((1, 1), b[(1, 1)], b))
예제 #18
0
    def test_king_get_possible_moves(self):
        matrix = PieceTest.initialize_empty_grid()
        king = King('white', 'white_king')
        positions = [(0, 0), (0, 7), (3, 4), (7, 0), (7, 7)]
        expectations = {
            (0, 0): [(1, 1, 'mov'), (1, 0, 'mov'), (0, 1, 'mov')],
            (0, 7): [(1, 6, 'mov'), (1, 7, 'mov'), (0, 6, 'mov')],
            (3, 4):
            [(2, 5, 'mov'), (2, 3, 'mov'), (4, 5, 'mov'), (4, 3, 'mov'),
             (2, 4, 'mov'), (4, 4, 'mov'), (3, 5, 'mov'), (3, 3, 'mov')],
            (7, 0): [(6, 1, 'mov'), (6, 0, 'mov'), (7, 1, 'mov')],
            (7, 7): [(6, 6, 'mov'), (6, 7, 'mov'), (7, 6, 'mov')]
        }

        for pos in positions:
            possible_moves = list(set(king.get_possible_moves(pos, matrix)))
            self.assertCountEqual(possible_moves, expectations[pos])
예제 #19
0
 def _populate_major_and_minor(self, row, color):
     self.grid[row][4] = King(self, [row, 4], color)
     self.grid[row][3] = Queen(self, [row, 3], color)
     self.grid[row][2] = Bishop(self, [row, 2], color)
     self.grid[row][5] = Bishop(self, [row, 5], color)
     self.grid[row][0] = Rook(self, [row, 0], color)
     self.grid[row][7] = Rook(self, [row, 7], color)
     self.grid[row][1] = Knight(self, [row, 1], color)
     self.grid[row][6] = Knight(self, [row, 6], color)
예제 #20
0
파일: board.py 프로젝트: HourGlss/Chess
    def reset_pieces(self):

        # p2 = Pawn("black")
        # b.board[5][6].add_piece(p)

        # WHITE PAWNS
        color = "white"
        for i in range(Config.BOARD_SIZE):
            p = Pawn(color)
            self.board[i][6].add_piece(p)
        # Other white pieces
        pieces = [
            Rook(color),
            Knight(color),
            Bishop(color),
            Queen(color),
            King(color),
            Bishop(color),
            Knight(color),
            Rook(color)
        ]
        for i in range(len(pieces)):
            self.board[i][7].add_piece(pieces[i])

        # BLACK PAWNS
        color = "black"
        for i in range(Config.BOARD_SIZE):
            p = Pawn(color)
            self.board[i][1].add_piece(p)
        # Other black pieces
        pieces = [
            Rook(color),
            Knight(color),
            Bishop(color),
            Queen(color),
            King(color),
            Bishop(color),
            Knight(color),
            Rook(color)
        ]
        for i in range(len(pieces)):
            self.board[i][0].add_piece(pieces[i])
예제 #21
0
class KingTests(unittest.TestCase):
    def setUp(self):
        chess_coord_white = ChessCoord('E', '6')
        self.king_white = King(chess_coord_white, white)
        chess_coord_black = ChessCoord('G', '5')
        self.king_black = King(chess_coord_black, black)

    def test_constructor_white(self):
        self.failUnless(self.king_white.letter == 'K')
        self.failUnless(self.king_white.chess_coord == ChessCoord('E', '6'))
        self.failUnless(self.king_white.grid_coord == GridCoord(4, 5))
        self.failUnless(self.king_white.colour == white)
        self.failUnless(self.king_white.symbol == '♔')
        self.failUnless(util.compare_lists(self.king_white.move_directions,
                                           directions.move_directions_queen()))

    def test_constructor_black(self):
        self.failUnless(self.king_black.letter == 'K')
        self.failUnless(self.king_black.chess_coord == ChessCoord('G', '5'))
        self.failUnless(self.king_black.grid_coord == GridCoord(6, 4))
        self.failUnless(self.king_black.colour == black)
        self.failUnless(self.king_black.symbol == '♚')
        self.failUnless(util.compare_lists(self.king_black.move_directions,
                                           directions.move_directions_queen()))

    def test_white_king_allowed_to_move_north1_east1(self):
        pieces = []

        move_inspect_result = self.king_white.inspect_move(pieces,
                                                           ChessCoord('F', '7'))

        self.failUnless(move_inspect_result ==
                        MoveInspectResult(True, False, [GridCoord(5, 6)], None))

    def test_white_king_not_allowed_to_move_two_steps(self):
        pieces = []

        move_inspect_result = self.king_white.inspect_move(pieces,
                                                           ChessCoord('E', '4'))

        self.failUnless(move_inspect_result ==
                        MoveInspectResult(False, False, [], None))
예제 #22
0
    def populate_board(self):
        # self.board[0][0] = Rook(0, 0, "black")
        # self.board[1][0] = Knight(1, 0, "black")
        # self.board[2][0] = Bishop(2, 0, "black")
        # self.board[4][0] = Queen(4, 0, "black")
        # self.board[3][0] = King(3, 0, "black")
        # self.board[5][0] = Bishop(5, 0, "black")
        # self.board[6][0] = Knight(6, 0, "black")
        # self.board[7][0] = Rook(7, 0, "black")
        # for i in range(8):
        #     self.board[i][1] = Pawn(i, 1, "black")
        #     self.board[i][6] = Pawn(i, 6, "white")

        # self.board[0][7] = Rook(0, 7, "white")
        # self.board[1][7] = Knight(1, 7, "white")
        # self.board[2][7] = Bishop(2, 7, "white")
        # self.board[4][7] = Queen(4, 7, "white")
        # self.board[3][7] = King(3, 7, "white")
        # self.board[5][7] = Bishop(5, 7, "white")
        # self.board[6][7] = Knight(6, 7, "white")
        # self.board[7][7] = Rook(7, 7, "white")
        self.board[0][0] = Rook(0, 0, "black")
        self.board[0][1] = Knight(0, 1, "black")
        self.board[0][2] = Bishop(0, 2, "black")
        self.board[0][3] = Queen(0, 3, "black")
        self.board[0][4] = King(0, 4, "black")
        self.board[0][5] = Bishop(0, 5, "black")
        self.board[0][6] = Knight(0, 6, "black")
        self.board[0][7] = Rook(0, 7, "black")
        for i in range(8):
            self.board[1][i] = Pawn(1, i, "black")
            self.board[6][i] = Pawn(6, i, "white")

        self.board[7][0] = Rook(7, 0, "white")
        self.board[7][1] = Knight(7, 1, "white")
        self.board[7][2] = Bishop(7, 2, "white")
        self.board[7][3] = Queen(7, 3, "white")
        self.board[7][4] = King(7, 4, "white")
        self.board[7][5] = Bishop(7, 5, "white")
        self.board[7][6] = Knight(7, 6, "white")
        self.board[7][7] = Rook(7, 7, "white")
예제 #23
0
 def roque_menor(self,cor_do_jogador):
     ut = util()
     cor_do_jogador = cor_do_jogador.upper()
     if cor_do_jogador == "WHITE":
         if (self.contagem_movimento_rei_brancas > 0) or (self.contagem_movimento_torre_rm_brancas > 0) or (ut.peca_ameacada(62,cor_do_jogador)) or (ut.peca_ameacada(63,cor_do_jogador)) or (type(self.gameTiles[62].pieceOnTile) is not NullPiece ) or (type(self.gameTiles[63].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)):
             return True
         else:
             self.gameTiles[63] = Tile(63, King(63, "White"))
             self.gameTiles[62] = Tile(62, Rook(62, "White"))
             self.gameTiles[61] = Tile(61, NullPiece())
             self.gameTiles[64] = Tile(64, NullPiece())
             return False
     if cor_do_jogador == 'BLACK':
         if (self.contagem_movimento_rei_pretas > 0) or (self.contagem_movimento_torre_rm_pretas > 0) or (ut.peca_ameacada(6,cor_do_jogador)) or (ut.peca_ameacada(7,cor_do_jogador)) or (type(self.gameTiles[6].pieceOnTile) is not NullPiece) or (type(self.gameTiles[7].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)):
             return True
         else:
             self.gameTiles[6] = Tile(6,Rook(6,"Black"))
             self.gameTiles[7] = Tile(7, King(7, "Black"))
             self.gameTiles[5] = Tile(5, NullPiece())
             self.gameTiles[8] = Tile(8, NullPiece())
             return False
예제 #24
0
 def roque_maior(self,cor_do_jogador):
     ut = util()
     cor_do_jogador = cor_do_jogador.upper()
     if cor_do_jogador == "WHITE":
         if (self.contagem_movimento_rei_brancas > 0) or (self.contagem_movimento_torre_rma_brancas > 0) or (ut.peca_ameacada(60,cor_do_jogador)) or (ut.peca_ameacada(59,cor_do_jogador)) or (ut.peca_ameacada(58,cor_do_jogador)) or (type(self.gameTiles[60].pieceOnTile) is not NullPiece ) or (type(self.gameTiles[59].pieceOnTile) is not NullPiece) or (type(self.gameTiles[58].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)):
             return True
         else:
             self.gameTiles[59] = Tile(59, King(59, "White"))
             self.gameTiles[60] = Tile(60, Rook(60, "White"))
             self.gameTiles[61] = Tile(61, NullPiece())
             self.gameTiles[57] = Tile(64, NullPiece())
             return False
     if cor_do_jogador == 'BLACK':
         if (self.contagem_movimento_rei_pretas > 0) or (self.contagem_movimento_torre_rma_pretas > 0) or (ut.peca_ameacada(4,cor_do_jogador)) or (ut.peca_ameacada(3,cor_do_jogador)) or (ut.peca_ameacada(2,cor_do_jogador)) or (type(self.gameTiles[4].pieceOnTile) is not NullPiece) or (type(self.gameTiles[3].pieceOnTile) is not NullPiece) or (type(self.gameTiles[2].pieceOnTile) is not NullPiece) or (ut.xeque(cor_do_jogador)):
             return True
         else:
             self.gameTiles[4] = Tile(4,Rook(4,"Black"))
             self.gameTiles[3] = Tile(3, King(3, "Black"))
             self.gameTiles[5] = Tile(5, NullPiece())
             self.gameTiles[1] = Tile(1, NullPiece())
             return False
예제 #25
0
    def createBoard(self):
        for rows in range(8):
            for cols in range(8):
                self.board[rows][cols] = Tile(nullPiece())

        #Pawn create
        for cols in range(8):
            self.board[1][cols] = Tile(Pawn("B", 1, cols))

        for cols in range(8):
            self.board[6][cols] = Tile(Pawn("W", 6, cols))

        #create King
        self.board[0][4] = Tile(King("B", 0, 4))
        self.board[7][4] = Tile(King("W", 7, 4))

        # create Queen
        self.board[0][3] = Tile(Queen("B", 0, 3))
        self.board[7][3] = Tile(Queen("W", 7, 3))

        # create Rock
        self.board[0][0] = Tile(Rook("B", 0, 0))
        self.board[0][7] = Tile(Rook("B", 0, 7))

        self.board[7][0] = Tile(Rook("W", 7, 0))
        self.board[7][7] = Tile(Rook("W", 7, 7))

        # create Bishop
        self.board[0][2] = Tile(Bishop("B", 0, 2))
        self.board[0][5] = Tile(Bishop("B", 0, 5))

        self.board[7][2] = Tile(Bishop("W", 7, 2))
        self.board[7][5] = Tile(Bishop("W", 7, 5))

        # create Knight
        self.board[0][1] = Tile(Knight("B", 0, 1))
        self.board[0][6] = Tile(Knight("B", 0, 6))

        self.board[7][1] = Tile(Knight("W", 7, 1))
        self.board[7][6] = Tile(Knight("W", 7, 6))
예제 #26
0
    def createBoard(self):
        for square in range(64):
            self.squares[square] = Square(square, EmptyPiece())

        self.squares[0] = Square(0, Rook("Black", 0))
        self.squares[1] = Square(1, Knight("Black", 1))
        self.squares[2] = Square(2, Bishop("Black", 2))
        self.squares[3] = Square(3, Queen("Black", 3))
        self.squares[4] = Square(4, King("Black", 4))
        self.squares[5] = Square(5, Bishop("Black", 5))
        self.squares[6] = Square(6, Knight("Black", 6))
        self.squares[7] = Square(7, Rook("Black", 7))

        self.squares[8] = Square(8, Pawn("Black", 8))
        self.squares[9] = Square(9, Pawn("Black", 9))
        self.squares[10] = Square(10, Pawn("Black", 10))
        self.squares[11] = Square(11, Pawn("Black", 11))
        self.squares[12] = Square(12, Pawn("Black", 12))
        self.squares[13] = Square(13, Pawn("Black", 13))
        self.squares[14] = Square(14, Pawn("Black", 14))
        self.squares[15] = Square(15, Pawn("Black", 15))

        self.squares[56] = Square(56, Rook("White", 56))
        self.squares[57] = Square(57, Knight("White", 57))
        self.squares[58] = Square(58, Bishop("White", 58))
        self.squares[59] = Square(59, Queen("White", 59))
        self.squares[60] = Square(60, King("White", 60))
        self.squares[61] = Square(61, Bishop("White", 61))
        self.squares[62] = Square(62, Knight("White", 62))
        self.squares[63] = Square(63, Rook("White", 63))

        self.squares[48] = Square(48, Pawn("White", 48))
        self.squares[49] = Square(49, Pawn("White", 49))
        self.squares[50] = Square(50, Pawn("White", 50))
        self.squares[51] = Square(51, Pawn("White", 51))
        self.squares[52] = Square(52, Pawn("White", 52))
        self.squares[53] = Square(53, Pawn("White", 53))
        self.squares[54] = Square(54, Pawn("White", 54))
        self.squares[55] = Square(55, Pawn("White", 55))
예제 #27
0
    def createPiece(self):
        chessboard[(6, 0)] = BlackPawn(6, 0, True)
        chessboard[(6, 1)] = BlackPawn(6, 1, True)
        chessboard[(6, 2)] = BlackPawn(6, 2, True)
        chessboard[(6, 3)] = BlackPawn(6, 3, True)
        chessboard[(6, 4)] = BlackPawn(6, 4, True)
        chessboard[(6, 5)] = BlackPawn(6, 5, True)
        chessboard[(6, 6)] = BlackPawn(6, 6, True)
        # chessboard[(6, 7)] = BlackPawn(6, 7, True)

        chessboard[(7, 0)] = Rook(7, 0, True)
        chessboard[(7, 1)] = Knight(7, 1, True)
        chessboard[(7, 2)] = Bishop(7, 2, True)
        chessboard[(7, 3)] = King(7, 3, True)
        self.white_king = chessboard[(7, 3)]
        chessboard[(7, 4)] = Queen(7, 4, True)
        chessboard[(7, 5)] = Bishop(7, 5, True)
        chessboard[(7, 6)] = Knight(7, 6, True)
        chessboard[(7, 7)] = Rook(7, 7, True)

        chessboard[(0, 0)] = Rook(0, 0, False)
        chessboard[(0, 1)] = Knight(0, 1, False)
        chessboard[(0, 2)] = Bishop(0, 2, False)
        chessboard[(0, 3)] = King(0, 3, False)
        self.black_king = chessboard[(0, 3)]
        chessboard[(0, 4)] = Queen(0, 4, False)
        chessboard[(0, 5)] = Bishop(0, 5, False)
        chessboard[(0, 6)] = Knight(0, 6, False)
        chessboard[(0, 7)] = Rook(0, 7, False)

        # chessboard[(1, 0)] = WhitePawn(1, 0, False)
        chessboard[(1, 1)] = WhitePawn(1, 1, False)
        chessboard[(1, 2)] = WhitePawn(1, 2, False)
        chessboard[(1, 3)] = WhitePawn(1, 3, False)
        chessboard[(1, 4)] = WhitePawn(1, 4, False)
        chessboard[(1, 5)] = WhitePawn(1, 5, False)
        chessboard[(1, 6)] = WhitePawn(1, 6, False)
        chessboard[(1, 7)] = WhitePawn(1, 7, False)
예제 #28
0
def basic_king_test():
    print("========================================")
    print("Performing the basic king movement test")
    print("========================================")
    board = Board()
    piece = King(Colour.WHITE)
    current_location = Location('d', 5)
    board.__add_piece__(piece, current_location)
    print(board)

    allowed_moves = piece.allowed_moves(current_location, board)
    print("For a {} {} starting at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(allowed_moves)

    new_location = allowed_moves[0]
    board.move_piece(piece, current_location, new_location)
    current_location = new_location
    print("Moved the {} to position {}".format(piece.name, new_location))
    print(board)
    print("For a {} {} at position {} the moves are:".format(
        piece.colour, piece.name, current_location))
    print(piece.allowed_moves(current_location, board))
예제 #29
0
 def __init__(self, colour, piece_type, file, rank):
     self.colour = colour
     self.file = file
     self.rank = rank
     self.type = piece_type
     if self.type == 'pawn':
         self.typemgr = Pawn(colour=colour, file=file, rank=rank)
     elif self.type == 'knight':
         self.typemgr = Knight(colour=colour, file=file, rank=rank)
     elif self.type == 'bishop':
         self.typemgr = Bishop(colour=colour, file=file, rank=rank)
     elif self.type == 'rook':
         self.typemgr = Rook(colour=colour, file=file, rank=rank)
     elif self.type == 'queen':
         self.typemgr = Queen(colour=colour, file=file, rank=rank)
     elif self.type == 'king':
         self.typemgr = King(colour=colour, file=file, rank=rank)
예제 #30
0
    def __init__(self, board, turn, boards):
        self.turn = turn

        objBoard = []
        kings = []

        #Define a board filled with piece objects
        y_Pos = 0
        for y in board:
            x_ObjBoard = []

            x_Pos = 0
            for x in y:
                if x != "":
                    piece = Piece(
                        x[0], x[1], (y_Pos, x_Pos), None
                    )  #Why did I define it as (y,x)? I have no idea... But I regret doing so... deeply....
                    x_ObjBoard.append(piece)
                    if x[1] == "R":
                        movement = Rook(x[0], x[1], (y_Pos, x_Pos), x[2])
                    elif x[1] == "B":
                        movement = Bishop(x[0], x[1], (y_Pos, x_Pos))
                    elif x[1] == "Q":
                        movement = Queen(x[0], x[1], (y_Pos, x_Pos))
                    elif x[1] == "k":
                        movement = Knight(x[0], x[1], (y_Pos, x_Pos))
                    elif x[1] == "P":
                        movement = Pawn(x[0], x[1], (y_Pos, x_Pos), x[2])
                    elif x[1] == "K":
                        movement = King(x[0], x[1], (y_Pos, x_Pos), x[2])
                        kings.append(piece)
                    piece.setMove(
                        movement
                    )  #Append the unique movement code to the piece object

                else:
                    x_ObjBoard.append("")
                x_Pos += 1
            objBoard.append(x_ObjBoard)
            y_Pos += 1
        self.promote = [False, False]  #If a pawn is promoting (White,Black)
        self.colour = "R"
        self.boards = boards
        self.board = objBoard
        self.kings = kings  #King objects stored for checking checks
예제 #31
0
def build_white_pieces():
    white_pieces = []
    # rooks
    white_pieces.append(Rook(WHITE, Coordinate(0, 7)))
    white_pieces.append(Rook(WHITE, Coordinate(7, 7)))
    # knights
    white_pieces.append(Knight(WHITE, Coordinate(1, 7)))
    white_pieces.append(Knight(WHITE, Coordinate(6, 7)))
    # bishops
    white_pieces.append(Bishop(WHITE, Coordinate(2, 7)))
    white_pieces.append(Bishop(WHITE, Coordinate(5, 7)))
    # queen
    white_pieces.append(Queen(WHITE, Coordinate(3, 7)))
    # king
    white_pieces.append(King(WHITE, Coordinate(4, 7)))
    # pawns
    for x in range(8):
        white_pieces.append(Pawn(WHITE, Coordinate(x, 6)))
    return white_pieces
예제 #32
0
def build_black_pieces():
    black_pieces = []
    # rooks
    black_pieces.append(Rook(BLACK, Coordinate(0, 0)))
    black_pieces.append(Rook(BLACK, Coordinate(7, 0)))
    # knights
    black_pieces.append(Knight(BLACK, Coordinate(1, 0)))
    black_pieces.append(Knight(BLACK, Coordinate(6, 0)))
    # bishops
    black_pieces.append(Bishop(BLACK, Coordinate(2, 0)))
    black_pieces.append(Bishop(BLACK, Coordinate(5, 0)))
    # queen
    black_pieces.append(Queen(BLACK, Coordinate(3, 0)))
    # king
    black_pieces.append(King(BLACK, Coordinate(4, 0)))
    # pawns
    for x in range(8):
        black_pieces.append(Pawn(BLACK, Coordinate(x, 1)))
    return black_pieces
예제 #33
0
def __add_inital_pieces__(board, colour):
    first_row = 1 if colour == Colour.WHITE else 8
    second_row = 2 if colour == Colour.WHITE else 7

    board.__add_piece__(Rook(colour), Location('a', first_row))
    board.__add_piece__(Knight(colour), Location('b', first_row))
    board.__add_piece__(Bishop(colour), Location('c', first_row))
    board.__add_piece__(Queen(colour), Location('d', first_row))
    board.__add_piece__(King(colour), Location('e', first_row))
    board.__add_piece__(Bishop(colour), Location('f', first_row))
    board.__add_piece__(Knight(colour), Location('g', first_row))
    board.__add_piece__(Rook(colour), Location('h', first_row))

    board.__add_piece__(Pawn(colour), Location('a', second_row))
    board.__add_piece__(Pawn(colour), Location('b', second_row))
    board.__add_piece__(Pawn(colour), Location('c', second_row))
    board.__add_piece__(Pawn(colour), Location('d', second_row))
    board.__add_piece__(Pawn(colour), Location('e', second_row))
    board.__add_piece__(Pawn(colour), Location('f', second_row))
    board.__add_piece__(Pawn(colour), Location('g', second_row))
    board.__add_piece__(Pawn(colour), Location('h', second_row))
예제 #34
0
	def new_piece(color: int, piece_type: int, position: tuple[int, int], tilesize: int):
		"""
		Créée et renvoit une pièce du type passé.

		:param color: La couleur de la pièce
		:param piece_type: Le type de la pièce
		:param position: La case de l'échiquier
		:param tilesize: La taille en pixels des cases, pour mettre l'image de la pièce à l'échelle
		:return: La pièce appropriée
		"""
		if piece_type == ROI:
			return King(color, position, tilesize)
		if piece_type == REINE:
			return Queen(color, position, tilesize)
		if piece_type == TOUR:
			return Rook(color, position, tilesize)
		if piece_type == CAVALIER:
			return Knight(color, position, tilesize)
		if piece_type == FOU:
			return Bishop(color, position, tilesize)
		if piece_type == PION:
			return Pawn(color, position, tilesize)
예제 #35
0
 def setUp(self):
     chess_coord_white = ChessCoord('E', '6')
     self.king_white = King(chess_coord_white, white)
     chess_coord_black = ChessCoord('G', '5')
     self.king_black = King(chess_coord_black, black)
예제 #36
0
파일: board.py 프로젝트: mAxYoLo01/Chess
 def createNewBoard(self):
     self.board = []
     for i in range(8):
         row = []
         for j in range(8):
             row.append(NullPiece((i, j)))
         self.board.append(row)
     rookB1 = Rook('B')
     rookB2 = Rook('B')
     knightB1 = Knight('B')
     knightB2 = Knight('B')
     bishopB1 = Bishop('B')
     bishopB2 = Bishop('B')
     queenB = Queen('B')
     kingB = King('B')
     pawnB1 = Pawn('B')
     pawnB2 = Pawn('B')
     pawnB3 = Pawn('B')
     pawnB4 = Pawn('B')
     pawnB5 = Pawn('B')
     pawnB6 = Pawn('B')
     pawnB7 = Pawn('B')
     pawnB8 = Pawn('B')
     rookW1 = Rook('W')
     rookW2 = Rook('W')
     knightW1 = Knight('W')
     knightW2 = Knight('W')
     bishopW1 = Bishop('W')
     bishopW2 = Bishop('W')
     queenW = Queen('W')
     kingW = King('W')
     pawnW1 = Pawn('W')
     pawnW2 = Pawn('W')
     pawnW3 = Pawn('W')
     pawnW4 = Pawn('W')
     pawnW5 = Pawn('W')
     pawnW6 = Pawn('W')
     pawnW7 = Pawn('W')
     pawnW8 = Pawn('W')
     self.addPiece(rookB1, (0, 0))
     self.addPiece(knightB1, (0, 1))
     self.addPiece(bishopB1, (0, 2))
     self.addPiece(queenB, (0, 3))
     self.addPiece(kingB, (0, 4))
     self.addPiece(bishopB2, (0, 5))
     self.addPiece(knightB2, (0, 6))
     self.addPiece(rookB2, (0, 7))
     self.addPiece(pawnB1, (1, 0))
     self.addPiece(pawnB2, (1, 1))
     self.addPiece(pawnB3, (1, 2))
     self.addPiece(pawnB4, (1, 3))
     self.addPiece(pawnB5, (1, 4))
     self.addPiece(pawnB6, (1, 5))
     self.addPiece(pawnB7, (1, 6))
     self.addPiece(pawnB8, (1, 7))
     self.addPiece(pawnW1, (6, 0))
     self.addPiece(pawnW2, (6, 1))
     self.addPiece(pawnW3, (6, 2))
     self.addPiece(pawnW4, (6, 3))
     self.addPiece(pawnW5, (6, 4))
     self.addPiece(pawnW6, (6, 5))
     self.addPiece(pawnW7, (6, 6))
     self.addPiece(pawnW8, (6, 7))
     self.addPiece(rookW1, (7, 0))
     self.addPiece(knightW1, (7, 1))
     self.addPiece(bishopW1, (7, 2))
     self.addPiece(queenW, (7, 3))
     self.addPiece(kingW, (7, 4))
     self.addPiece(bishopW2, (7, 5))
     self.addPiece(knightW2, (7, 6))
     self.addPiece(rookW2, (7, 7))
예제 #37
0
class CheckTests(unittest.TestCase):
    def setUp(self):
        self.king_white = King(ChessCoord('E', '6'), white)
        self.king_black = King(ChessCoord('G', '5'), black)
        self.bishop_black = Bishop(ChessCoord('B', '4'), black)
        self.rook_white = Bishop(ChessCoord('A', '2'), white)
        self.pieces = [self.king_black, self.king_white,
                       self.bishop_black, self.rook_white]
        self.move_inspect_result_no_piece = MoveInspectResult(True, False, [], None)

    def test_check_no_false_positive(self):
        is_in_check = self.king_black.check_for_putting_self_in_check(self.pieces,
                                                                      ChessCoord('F', '4'),
                                                                      self.move_inspect_result_no_piece)
        self.failIf(is_in_check)

    def test_king_moves_into_check_detected(self):
        is_in_check = self.king_white.check_for_putting_self_in_check(self.pieces,
                                                                      ChessCoord('E', '7'),
                                                                      self.move_inspect_result_no_piece)

        self.failUnless(is_in_check)

    def test_piece_moves_and_puts_own_king_into_check(self):
        self.bishop_black.update_coord(ChessCoord('B', '3'))
        self.rook_white.update_coord(ChessCoord('D', '5'))
        is_in_check = self.rook_white.check_for_putting_self_in_check(self.pieces,
                                                                      ChessCoord('D', '6'),
                                                                      self.move_inspect_result_no_piece)
        self.failUnless(is_in_check)

    def test_with_full_pieces_block_check(self):
        all_pieces = copy.deepcopy(starting_pieces)
        # move e2 white pawn
        e2_pawn = select_piece(ChessCoord('E', '2'), all_pieces)
        is_in_check = e2_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('E', '4'),
                                                              self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        e2_pawn.update_coord(ChessCoord('E', '4'))

        # move black pawn d6
        d7_pawn = select_piece(ChessCoord('D', '7'), all_pieces)
        is_in_check = d7_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('D', '6'),
                                                              self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        d7_pawn.update_coord(ChessCoord('D', '6'))

        # move black bishop g4
        c8_bishop = select_piece(ChessCoord('C', '8'), all_pieces)
        is_in_check = c8_bishop.check_for_putting_self_in_check(all_pieces,
                                                                ChessCoord('G', '4'),
                                                                self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        c8_bishop.update_coord(ChessCoord('G', '4'))

        # move white king to e2
        e1_king = select_piece(ChessCoord('E', '1'), all_pieces)
        is_in_check = e1_king.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('E', '2'),
                                                              self.move_inspect_result_no_piece)
        self.failUnless(is_in_check)
        e1_king.update_coord(ChessCoord('E', '2'))

        # move white pawn to f3
        f2_pawn = select_piece(ChessCoord('F', '2'), all_pieces)
        is_in_check = f2_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('F', '3'),
                                                              self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        f2_pawn.update_coord(ChessCoord('F', '3'))

    def test_with_full_pieces_take_checking_piece(self):
        all_pieces = copy.deepcopy(starting_pieces)
        # move e2 white pawn
        e2_pawn = select_piece(ChessCoord('E', '2'), all_pieces)
        is_in_check = e2_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('E', '4'),
                                                              self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        e2_pawn.update_coord(ChessCoord('E', '4'))

        # move d7 pawn to d6
        d7_pawn = select_piece(ChessCoord('D', '7'), all_pieces)
        is_in_check = d7_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('D', '6'),
                                                              self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        d7_pawn.update_coord(ChessCoord('D', '6'))

        # move f1 bishop to b5
        f1_bishop = select_piece(ChessCoord('F', '1'), all_pieces)
        is_in_check = f1_bishop.check_for_putting_self_in_check(all_pieces,
                                                                ChessCoord('B', '5'),
                                                                self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        f1_bishop.update_coord(ChessCoord('B', '5'))

        # move c7 pawn to c6
        c7_pawn = select_piece(ChessCoord('C', '7'), all_pieces)
        is_in_check = c7_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('C', '6'),
                                                              self.move_inspect_result_no_piece)
        self.failIf(is_in_check)
        c7_pawn.update_coord(ChessCoord('C', '6'))

        # move b5 bishop to c6 take
        b5_bishop = select_piece(ChessCoord('B', '5'), all_pieces)
        is_in_check = b5_bishop.check_for_putting_self_in_check(all_pieces,
                                                                ChessCoord('C', '6'),
                                                                MoveInspectResult(True, False, [], c7_pawn))
        self.failIf(is_in_check)
        b5_bishop.update_coord(ChessCoord('C', '6'))
        all_pieces.remove(c7_pawn)

        # move b7 to c6
        b7_pawn = select_piece(ChessCoord('B', '7'), all_pieces)
        is_in_check = b7_pawn.check_for_putting_self_in_check(all_pieces,
                                                              ChessCoord('C', '6'),
                                                              MoveInspectResult(True, False, [], b5_bishop))
        self.failIf(is_in_check)
        b7_pawn.update_coord(ChessCoord('C', '6'))
        all_pieces.remove(b5_bishop)

    def test_king_takes_checking_piece_and_can_keep_moving(self):
        all_pieces = copy.deepcopy(starting_pieces)
        d2_pawn = select_piece(ChessCoord('D', '2'), all_pieces)
        e7_pawn = select_piece(ChessCoord('E', '7'), all_pieces)
        f8_bishop = select_piece(ChessCoord('F', '8'), all_pieces)
        d1_queen = select_piece(ChessCoord('D', '1'), all_pieces)
        e1_king = select_piece(ChessCoord('E', '1'), all_pieces)

        d2_pawn.inspect_move(all_pieces, ChessCoord('D', '4'))
        d2_pawn.update_coord(ChessCoord('D', '4'))

        e7_pawn.inspect_move(all_pieces, ChessCoord('E', '6'))
        e7_pawn.update_coord(ChessCoord('E', '6'))

        f8_bishop.inspect_move(all_pieces, ChessCoord('B', '4'))
        f8_bishop.update_coord(ChessCoord('B', '4'))

        move_inspect_result = d1_queen.inspect_move(all_pieces, ChessCoord('D', '2'))
        self.failIf(move_inspect_result.will_put_self_in_check)
        d1_queen.update_coord(ChessCoord('D', '2'))

        f8_bishop.inspect_move(all_pieces, ChessCoord('D', '2'))
        f8_bishop.update_coord(ChessCoord('D', '2'))

        all_pieces.remove(d1_queen)

        move_inspect_result = e1_king.inspect_move(all_pieces, ChessCoord('D', '2'))
        self.failIf(move_inspect_result.will_put_self_in_check)
        e1_king.update_coord(ChessCoord('D', '2'))

        all_pieces.remove(f8_bishop)

        move_inspect_result = e1_king.inspect_move(all_pieces, ChessCoord('D', '3'))
        self.failIf(move_inspect_result.will_put_self_in_check)
        self.failUnless(move_inspect_result.is_valid_move)
        e1_king.update_coord(ChessCoord('D', '3'))

    def test_king_not_allowed_to_move_into_check(self):
        all_pieces = copy.deepcopy(starting_pieces)
        select_piece(ChessCoord('A', '2'), all_pieces).update_coord(ChessCoord('A', '4'))
        select_piece(ChessCoord('B', '7'), all_pieces).update_coord(ChessCoord('B', '6'))
        select_piece(ChessCoord('B', '1'), all_pieces).update_coord(ChessCoord('C', '3'))
        select_piece(ChessCoord('C', '8'), all_pieces).update_coord(ChessCoord('A', '6'))
        select_piece(ChessCoord('E', '2'), all_pieces).update_coord(ChessCoord('E', '4'))
        select_piece(ChessCoord('D', '8'), all_pieces).update_coord(ChessCoord('C', '8'))

        e1_king = select_piece(ChessCoord('E', '1'), all_pieces)
        move_inspect_result = e1_king.inspect_move(all_pieces, ChessCoord('E', '2'))

        self.failUnless(move_inspect_result.will_put_self_in_check)
        self.failIf(move_inspect_result.is_valid_move)
예제 #38
0
class Board:
    squares = {}
    black_king = King("Black", 4)
    white_king = King("White", 60)

    def __init__(self):
        pass

    def createBoard(self):
        square_color = ["light", "dark"]
        x = 0
        for i in range(64):
            self.squares[i] = Square(square_color[x % 2], i, nullPiece())
            if i % 8 == 0 and i != 0:
                x += 2
            else:
                x += 1

        self.squares[0] = Square("light", 0, Rook("Black", 0))
        self.squares[1] = Square("dark", 1, Knight("Black", 1))
        self.squares[2] = Square("light", 2, Bishop("Black", 2))
        self.squares[3] = Square("dark", 3, Queen("Black", 3))
        self.squares[4] = Square("light", 4, self.black_king)
        self.squares[5] = Square("dark", 5, Bishop("Black", 5))
        self.squares[6] = Square("light", 6, Knight("Black", 6))
        self.squares[7] = Square("dark", 7, Rook("Black", 7))
        self.squares[8] = Square("dark", 8, Pawn("Black", 8))
        self.squares[9] = Square("light", 9, Pawn("Black", 9))
        self.squares[10] = Square("dark", 10, Pawn("Black", 10))
        self.squares[11] = Square("light", 11, Pawn("Black", 11))
        self.squares[12] = Square("dark", 12, Pawn("Black", 12))
        self.squares[13] = Square("light", 13, Pawn("Black", 13))
        self.squares[14] = Square("dark", 14, Pawn("Black", 14))
        self.squares[15] = Square("light", 15, Pawn("Black", 15))

        self.squares[16] = Square("light", 16, nullPiece())
        self.squares[24] = Square("dark", 24, nullPiece())
        self.squares[32] = Square("light", 32, nullPiece())
        self.squares[40] = Square("dark", 40, nullPiece())

        self.squares[48] = Square("light", 48, Pawn("White", 48))
        self.squares[49] = Square("dark", 49, Pawn("White", 49))
        self.squares[50] = Square("light", 50, Pawn("White", 50))
        self.squares[51] = Square("dark", 51, Pawn("White", 51))
        self.squares[52] = Square("light", 52, Pawn("White", 52))
        self.squares[53] = Square("dark", 53, Pawn("White", 53))
        self.squares[54] = Square("light", 54, Pawn("White", 54))
        self.squares[55] = Square("dark", 55, Pawn("White", 55))
        self.squares[56] = Square("dark", 56, Rook("White", 56))
        self.squares[57] = Square("light", 57, Knight("White", 57))
        self.squares[58] = Square("dark", 58, Bishop("White", 58))
        self.squares[59] = Square("light", 59, Queen("White", 59))
        self.squares[60] = Square("dark", 60, self.white_king)
        self.squares[61] = Square("light", 61, Bishop("White", 61))
        self.squares[62] = Square("dark", 62, Knight("White", 62))
        self.squares[63] = Square("light", 63, Rook("White", 63))

    def printBoard(self):
        count = 0
        for i in range(64):
            print("|", end=self.squares[i].piece.toString())
            count += 1
            if count % 8 == 0:
                print("|", "\n")

    def all_possible_moves(self, king_color, position_of_king):
        temp_list = []
        for i in range(64):
            if i != position_of_king:
                piece_to_check = self.squares[i].piece
                if piece_to_check.toString(
                ) != "-" and piece_to_check.toString().lower(
                ) != "k" and piece_to_check.color != king_color:
                    if piece_to_check.toString().lower() == "p":
                        pawn_captures = piece_to_check.getChecks(self)
                        print("pc:", pawn_captures)
                        temp_list += pawn_captures
                        piece_to_check.clearLists()
                    else:
                        temp_list += self.squares[i].piece.possibleMoves(self)
                        self.squares[i].piece.clearLists()

        return temp_list

    def in_check(self, color):
        if color == "White":
            return self.white_king.currently_in_check(self)
        else:
            return self.black_king.currently_in_check(self)