Esempio n. 1
0
    def create_pieces(self):
        header = 'XABCDEFGHX'
        for i in range(self.size):
            self.board[0][i] = EmptyField('None', [0, i], header[i])
            self.board[self.size - 1][i] = EmptyField('None',
                                                      [i, self.size - 1],
                                                      header[i])

        for i in range(1, self.size - 1, 1):
            self.board[i][0] = EmptyField('None', [i, 0], (self.size - 1) - i)
            self.board[i][self.size - 1] = EmptyField('None',
                                                      [i, self.size - 1],
                                                      (self.size - 1) - i)

        self.board[1][1] = Rook('B', [1, 1])
        self.board[1][8] = Rook('B', [1, 8])
        self.board[8][1] = Rook('W', [8, 1])
        self.board[8][8] = Rook('W', [8, 8])
        self.board[1][2] = Knight('B', [1, 2])
        self.board[1][7] = Knight('B', [1, 7])
        self.board[8][2] = Knight('W', [8, 2])
        self.board[8][7] = Knight('W', [8, 8])
        self.board[1][3] = Bishop('B', [1, 3])
        self.board[1][6] = Bishop('B', [1, 6])
        self.board[8][3] = Bishop('W', [8, 3])
        self.board[8][6] = Bishop('W', [8, 6])
        self.board[1][4] = Queen('B', [1, 4])
        self.board[8][4] = Queen('W', [8, 4])
        self.board[1][5] = King('B', [1, 5])
        self.board[8][5] = King('W', [8, 5])

        for i in range(1, self.size - 1, 1):
            self.board[2][i] = Pawn('B', [2, i])
            self.board[7][i] = Pawn('W', [7, i])
Esempio n. 2
0
    def __init__(self):
        BOARD_SIZE = 518

        super().__init__([BOARD_SIZE, BOARD_SIZE])

        self.COLOR_RED = [255, 0, 0]
        self.PADDING = 10
        self.BOX_SIDE_LENGTH = 60
        self.BOX_BORDER_RADIUS = 5

        self.image = pygame.image.load('assets/Chess_Board.png')
        self.blit(self.image, [0, 0])

        # board but in matrix 6x6
        self.array = [
            [Rook(BLACK, 0, 0), Knight(BLACK, 1, 0), Queen(BLACK, 2, 0),
             King(BLACK, 3, 0), Knight(BLACK, 4, 0), Rook(BLACK, 5, 0)],
            [Pawn(BLACK, x, 1) for x in range(6)],
            [None for x in range(6)],
            [None for x in range(6)],
            [Pawn(WHITE, x, 4) for x in range(6)],
            [Rook(WHITE, 0, 5), Knight(WHITE, 1, 5), Queen(WHITE, 2, 5),
             King(WHITE, 3, 5), Knight(WHITE, 4, 5), Rook(WHITE, 5, 5)]
        ]

        # flatten 2d array and get only valid pieces to draw (delete None values)
        self.pieces = [
            piece for row in self.array for piece in row if piece is not None]

        # generate valid moves for white player
        self.generate_valid_moves_for_player_pieces(WHITE, BLACK)

        self.sprites_group = pygame.sprite.Group()
        self.sprites_group.add(self.pieces)
Esempio n. 3
0
    def init(self):
        """Initialize board pieces."""
        self._pieces = {
            BLACK: PList([]),
            WHITE: PList([]),
        }
        self._playing_color = WHITE
        for color in [BLACK, WHITE]:
            for pawn_n in range(8):
                x = 1 if color is TOP_COLOR else 6
                y = pawn_n
                pawn = Pawn(x, y, color)
                self._add_piece(pawn)

            lign = 0 if color is TOP_COLOR else 7
            self._add_piece(Bishop(lign, 2, color))
            self._add_piece(Bishop(lign, 5, color))
            self._add_piece(Knight(lign, 1, color))
            self._add_piece(Knight(lign, 6, color))
            self._add_piece(Rook(lign, 0, color))
            self._add_piece(Rook(lign, 7, color))
            if TOP_COLOR is BLACK:
                self._add_piece(Queen(lign, 3, color))
                self._add_piece(King(lign, 4, color))
            else:
                self._add_piece(King(lign, 3, color))
                self._add_piece(Queen(lign, 4, color))
Esempio n. 4
0
    def __init__(self, screen):
        self.screen = screen
        self.is_whites_turn = True
        self.board = [[0 for j in range(NO_OF_COLS)]
                      for i in range(NO_OF_ROWS)]

        self.board[0][0] = Rook(screen, 0, 0, "black")
        self.board[0][1] = Knight(screen, 0, 1, "black")
        self.board[0][2] = Bishop(screen, 0, 2, "black")
        self.board[0][3] = Queen(screen, 0, 3, "black")
        self.board[0][4] = King(screen, 0, 4, "black")
        self.board[0][5] = Bishop(screen, 0, 5, "black")
        self.board[0][6] = Knight(screen, 0, 6, "black")
        self.board[0][7] = Rook(screen, 0, 7, "black")

        self.board[7][0] = Rook(screen, 7, 0, "white")
        self.board[7][1] = Knight(screen, 7, 1, "white")
        self.board[7][2] = Bishop(screen, 7, 2, "white")
        self.board[7][3] = Queen(screen, 7, 3, "white")
        self.board[7][4] = King(screen, 7, 4, "white")
        self.board[7][5] = Bishop(screen, 7, 5, "white")
        self.board[7][6] = Knight(screen, 7, 6, "white")
        self.board[7][7] = Rook(screen, 7, 7, "white")

        for j in range(NO_OF_COLS):
            self.board[1][j] = Pawn(screen, 1, j, "black")

        for j in range(NO_OF_COLS):
            self.board[6][j] = Pawn(screen, 6, j, "white")

        self.to_highlight = []
Esempio n. 5
0
File: Board.py Progetto: Neyri/chess
 def init_pieces(self):
     # Init the pieces
     # black
     self.board[0][0].piece = Rook(0, 0, 'black', self)
     self.board[0][7].piece = Rook(0, 7, 'black', self)
     self.board[0][1].piece = Knight(0, 1, 'black', self)
     self.board[0][6].piece = Knight(0, 6, 'black', self)
     self.board[0][2].piece = Bishop(0, 2, 'black', self)
     self.board[0][5].piece = Bishop(0, 5, 'black', self)
     self.board[0][3].piece = Queen(0, 3, 'black', self)
     self.board[0][4].piece = King(0, 4, 'black', self)
     # white
     self.board[7][0].piece = Rook(7, 0, 'white', self)
     self.board[7][7].piece = Rook(7, 7, 'white', self)
     self.board[7][1].piece = Knight(7, 1, 'white', self)
     self.board[7][6].piece = Knight(7, 6, 'white', self)
     self.board[7][2].piece = Bishop(7, 2, 'white', self)
     self.board[7][5].piece = Bishop(7, 5, 'white', self)
     # self.board[4][2].piece = Bishop(4, 2, 'white', self)
     self.board[7][3].piece = Queen(7, 3, 'white', self)
     # self.board[3][7].piece = Queen(3, 7, 'white', self)
     self.board[7][4].piece = King(7, 4, 'white', self)
     for j in range(8):
         self.board[1][j].piece = Pawn(1, j, 'black', self)
         self.board[6][j].piece = Pawn(6, j, 'white', self)
Esempio n. 6
0
    def _setup_board(self):
        self.parent.title = 'Chess'
        self.parent.geometry('600x600')
        tk.Grid.rowconfigure(self.parent, 0, weight=1)
        tk.Grid.columnconfigure(self.parent, 0, weight=1)
        self.frame.grid(row=0, column=0, sticky='nsew')
        grid = tk.Frame(self.frame)
        grid.grid(sticky='nsew', column=0, row=7, columnspan=2)
        tk.Grid.rowconfigure(self.frame, 7, weight=1)
        tk.Grid.columnconfigure(self.frame, 0, weight=1)

        player1_pieces = []
        player2_pieces = []

        # create pawns
        for i in range(8):
            player1_pieces.append(
                WhitePawn(img_path=IMAGE_PATH.format('P'), x=i, y=1))
            player2_pieces.append(
                BlackPawn(img_path=IMAGE_PATH.format('p'), x=i, y=6))

        # create others
        player1_pieces.append(Rook(img_path=IMAGE_PATH.format('R'), x=0, y=0))
        player1_pieces.append(Rook(img_path=IMAGE_PATH.format('R'), x=7, y=0))
        player1_pieces.append(Knight(img_path=IMAGE_PATH.format('N'), x=1,
                                     y=0))
        player1_pieces.append(Knight(img_path=IMAGE_PATH.format('N'), x=6,
                                     y=0))
        player1_pieces.append(Bishop(img_path=IMAGE_PATH.format('B'), x=2,
                                     y=0))
        player1_pieces.append(Bishop(img_path=IMAGE_PATH.format('B'), x=5,
                                     y=0))

        player2_pieces.append(Rook(img_path=IMAGE_PATH.format('r'), x=0, y=7))
        player2_pieces.append(Rook(img_path=IMAGE_PATH.format('r'), x=7, y=7))
        player2_pieces.append(Knight(img_path=IMAGE_PATH.format('n'), x=1,
                                     y=7))
        player2_pieces.append(Knight(img_path=IMAGE_PATH.format('n'), x=6,
                                     y=7))
        player2_pieces.append(Bishop(img_path=IMAGE_PATH.format('b'), x=2,
                                     y=7))
        player2_pieces.append(Bishop(img_path=IMAGE_PATH.format('b'), x=5,
                                     y=7))

        # create king + queens
        player1_pieces.append(King(img_path=IMAGE_PATH.format('K'), x=3, y=0))
        player1_pieces.append(Queen(img_path=IMAGE_PATH.format('Q'), x=4, y=0))
        player2_pieces.append(King(img_path=IMAGE_PATH.format('k'), x=4, y=7))
        player2_pieces.append(Queen(img_path=IMAGE_PATH.format('q'), x=3, y=7))

        self.players[0].pieces = player1_pieces
        self.players[1].pieces = player2_pieces

        self.add_pieces_to_board()

        for x in range(8):
            tk.Grid.columnconfigure(self.frame, x, weight=1)

        for y in range(8):
            tk.Grid.rowconfigure(self.frame, y, weight=1)
Esempio n. 7
0
    def setBoard(self):
        self.board = [[None for i in range(8)] for i in range(8)]

        for piece in self.board[1]:
            self.board[1][self.board[1].index(piece)] = Pawn(
                [1, self.board[1].index(piece)], "black")
        for piece in self.board[6]:
            self.board[6][self.board[6].index(piece)] = Pawn(
                [6, self.board[6].index(piece)], "white")

        self.board[0][0] = Rook([0, 0], "black")
        self.board[0][7] = Rook([0, 7], "black")
        self.board[0][1] = Knight([0, 1], "black")  # black knight
        self.board[0][6] = Knight([0, 6], "black")  # black knight
        self.board[0][2] = Bishop([0, 2], "black")  # black bishop
        self.board[0][5] = Bishop([0, 5], "black")  # black bishop
        self.board[0][3] = Queen([0, 3], "black")  # black king
        self.board[0][4] = King([0, 4], "black")  # black queen

        self.board[7][0] = Rook([7, 0], "white")
        self.board[7][7] = Rook([7, 7], "white")
        self.board[7][1] = Knight([7, 1], "white")  # white knight
        self.board[7][6] = Knight([7, 6], "white")  # white knight
        self.board[7][2] = Bishop([7, 2], "white")  # white bishop
        self.board[7][5] = Bishop([7, 5], "white")  # white bishop
        self.board[7][3] = Queen([7, 3], "white")  # white king
        self.board[7][4] = King([7, 4], "white")  # white queen
Esempio n. 8
0
    def undo_move(self):
        self.player_to_move *= -1
        move = self.moves.pop()
        if move[6] == "0-0" or move[
                6] == "0-0-0":  ##  Undoing castling requires a good deal of special logic
            row = move[1]
            color = move[2]
            if move[6] == "0-0":  #kingside
                squares = (King(color, row, 4,
                                self), 0, 0, Rook(color, row, 7, self))

                def col_func(
                    j
                ):  ## a quick helper function to grab the appropriate columns on the matrix
                    return 4 + j

                rangee = 4
            else:
                squares = (King(color, row, 4,
                                self), 0, 0, 0, Rook(color, row, 0,
                                                     self))  #queenside

                def col_func(j):
                    return 4 - j

                rangee = 5
            for i in range(rangee):
                self.matrix[row][col_func(i)] = squares[i]
        else:  #  If the last move wasn't castling, undo the piece move, and if it captured something, put the captured piece back on the board.
            self.matrix[move[0]][move[1]] = self.matrix[move[2]][move[3]]
            self.matrix[move[2]][move[3]] = move[4]
            if move[5] == True:  #If there was an EnPassant capture, put a pawn on the appropriate square
                color = self.matrix[move[0]][move[1]].color
                self.matrix[move[0]][move[3]] = Pawn(-1 * color, move[0],
                                                     move[3])
Esempio n. 9
0
 def __init__(self):
     self.num_rows = 8
     self.num_cols = 8
     self.board = np.array([
         [
             Rook(0, 0, 'WHITE'),
             Knight(1, 0, 'WHITE'),
             Bishop(2, 0, 'WHITE'),
             King(3, 0, 'WHITE'),
             Queen(4, 0, 'WHITE'),
             Bishop(5, 0, 'WHITE'),
             Knight(6, 0, 'WHITE'),
             Rook(7, 0, 'WHITE')
         ],
         [Pawn(i, 1, 'WHITE') for i in range(self.num_cols)],
         [None for _ in range(self.num_cols)],
         [None for _ in range(self.num_cols)],
         [None for _ in range(self.num_cols)],
         [None for _ in range(self.num_cols)],
         [Pawn(i, 6, 'BLACK') for i in range(self.num_cols)],
         [
             Rook(0, 7, 'BLACK'),
             Knight(1, 7, 'BLACK'),
             Bishop(2, 7, 'BLACK'),
             King(3, 7, 'BLACK'),
             Queen(4, 7, 'BLACK'),
             Bishop(5, 7, 'BLACK'),
             Knight(6, 7, 'BLACK'),
             Rook(7, 7, 'BLACK')
         ],
     ])
Esempio n. 10
0
    def CreateNewBoard(cls, fen=None):
        if fen:
            raise NotImplementedError()
        else:
            pieces = [Pawn((i, 1), "white") for i in range(8)
                      ] + [Pawn((i, 6), "black") for i in range(8)]
            pieces.extend([
                Rook((0, 0), "white"),
                Rook((7, 0), "white"),
                Rook((0, 7), "black"),
                Rook((7, 7), "black")
            ])
            pieces.extend([
                Knight((1, 0), "white"),
                Knight((6, 0), "white"),
                Knight((1, 7), "black"),
                Knight((6, 7), "black")
            ])
            pieces.extend([
                Bishop((2, 0), "white"),
                Bishop((5, 0), "white"),
                Bishop((2, 7), "black"),
                Bishop((5, 7), "black")
            ])
            pieces.extend([King((4, 0), "white"), King((4, 7), "black")])
            pieces.extend([Queen((3, 0), "white"), Queen((3, 7), "black")])

        return cls(pieces, "white")
Esempio n. 11
0
 def newPieces(self):
     self.wPieces = []
     self.bPieces = []
     self.empty = []
     for i in range(8):
         self.wPieces.append(Pawn('w', (i, 1), 10 + i))
         self.bPieces.append(Pawn('b', (i, 6), 10 + i))
     for i in [0, 7]:
         self.wPieces.append(Rook('w', (i, 0), 20 + i))
         self.bPieces.append(Rook('b', (i, 7), 20 + i))
     for i in [1, 6]:
         self.wPieces.append(Knight('w', (i, 0), 30 + i))
         self.bPieces.append(Knight('b', (i, 7), 30 + i))
     for i in [2, 5]:
         self.wPieces.append(Bishop('w', (i, 0), 40 + i))
         self.bPieces.append(Bishop('b', (i, 7), 40 + i))
     self.wPieces.append(Queen('w', (3, 0), 50))
     self.bPieces.append(Queen('b', (3, 7), 50))
     self.wPieces.append(King('w', (4, 0), 60))
     self.bPieces.append(King('b', (4, 7), 60))
     for i in range(8):
         self.empty.append(Empty((i, 2)))
         self.empty.append(Empty((i, 3)))
         self.empty.append(Empty((i, 4)))
         self.empty.append(Empty((i, 5)))
     self.allPieces = self.wPieces + self.bPieces + self.empty
Esempio n. 12
0
 def __init__(self):
     self.spaceSize = 50
     self.blackRow = 0
     self.whiteRow = 7
     self.whiteKing = King(self.whiteRow,4,'white')
     self.blackKing = King(self.blackRow,4,'black')
     #Set up the board
     self.blackPieces =[Rook(self.blackRow,0,'black'), Knight(self.blackRow,1,'black'), 
          Bishop(self.blackRow,2,'black'), Queen(self.blackRow,3,'black'),
          self.blackKing, Bishop(self.blackRow,5,'black'), 
          Knight(self.blackRow,6,'black'), Rook(self.blackRow,self.whiteRow,'black')]
     
     self.whitePieces = [Rook(self.whiteRow,0,'white'), Knight(self.whiteRow,1,'white'), 
          Bishop(self.whiteRow,2,'white'), Queen(self.whiteRow,3,'white'),
          self.whiteKing, Bishop(self.whiteRow,5,'white'), 
          Knight(self.whiteRow,6,'white'), Rook(self.whiteRow,self.whiteRow,'white')]
     
     self.board = [
         [],
         [],
         [0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0],
         [0,0,0,0,0,0,0,0],
         [],
         [] 
         ]
     
     setPawns(self.board)
     setPieces(self.board,self.blackPieces,self.whitePieces)
     
     dBoard = [x[:] for x in self.board]
     self.positions = [[dBoard,0]]
Esempio n. 13
0
    def init_all_pieces(self, is_captured=False, p_start_pos=False, k_start_pos=False):
        '''Initializes all chess pieces'''

        # Piece attributes
        self.pieces = {
            'w_pawns': [Pawn(i + 1, is_captured=is_captured, start_pos=p_start_pos) for i in range(8)],
            'b_pawns': [Pawn(i + 1, colour='Black', is_captured=is_captured, start_pos=p_start_pos) for i in range(8)],
            'w_pieces': [
                Rook(1, is_captured=is_captured),
                Knight(1, is_captured=is_captured),
                Bishop(1, is_captured=is_captured),
                Queen(is_captured=is_captured),
                King(is_captured=is_captured, start_pos=k_start_pos),
                Bishop(2, is_captured=is_captured),
                Knight(2, is_captured=is_captured),
                Rook(2, is_captured=is_captured),
            ],
            'b_pieces': [
                Rook(1, colour='Black', is_captured=is_captured),
                Knight(1, colour='Black', is_captured=is_captured),
                Bishop(1, colour='Black', is_captured=is_captured),
                Queen(colour='Black', is_captured=is_captured),
                King(colour='Black', is_captured=is_captured, start_pos=k_start_pos),
                Bishop(2, colour='Black', is_captured=is_captured),
                Knight(2, colour='Black', is_captured=is_captured),
                Rook(2, colour='Black', is_captured=is_captured),
            ]
        }
Esempio n. 14
0
def read_piece_on_square(driver, pos):
    class_on_square = read_class_of_square(driver, pos, "piece")
    if class_on_square is None:
        return None
    else:
        if "wr" in class_on_square:
            return Rook(1, pos.X, pos.Y, True)  # hack autoset the id to 1
        if "wn" in class_on_square:
            return Knight(1, pos.X, pos.Y, True)  # hack autoset the id to 1
        if "wb" in class_on_square:
            return Bishop(1, pos.X, pos.Y, True)  # hack autoset the id to 1
        if "wq" in class_on_square:
            return Queen(1, pos.X, pos.Y, True)  # hack autoset the id to 1
        if "wk" in class_on_square:
            return King(1, pos.X, pos.Y, True)  # hack autoset the id to 1
        if "wp" in class_on_square:
            return Pawn(1, pos.X, pos.Y, True)  # hack autoset the id to 1
        if "br" in class_on_square:
            return Rook(1, pos.X, pos.Y, False)  # hack autoset the id to 1
        if "bn" in class_on_square:
            return Knight(1, pos.X, pos.Y, False)  # hack autoset the id to 1
        if "bb" in class_on_square:
            return Bishop(1, pos.X, pos.Y, False)  # hack autoset the id to 1
        if "bq" in class_on_square:
            return Queen(1, pos.X, pos.Y, False)  # hack autoset the id to 1
        if "bk" in class_on_square:
            return King(1, pos.X, pos.Y, False)  # hack autoset the id to 1
        if "bp" in class_on_square:
            return Pawn(1, pos.X, pos.Y, False)  # hack autoset the id to 1
Esempio n. 15
0
    def __init__(self, side='w'):
        self.side = side  # side closes to you aka the color you are playing

        if self.side == 'w':
            self.opponent = 'b'
        else:
            self.opponent = 'w'

        self.white_to_move = True
        self.moves = []  # log of moves

        self.board = np.full((DIM, DIM), Null(), dtype=object)

        self.board[0][0] = Rook((0, 0), self.opponent)
        self.board[0][1] = Knight((0, 1), self.opponent)
        self.board[0][2] = Bishop((0, 2), self.opponent)
        self.board[0][5] = Bishop((0, 5), self.opponent)
        self.board[0][6] = Knight((0, 6), self.opponent)
        self.board[0][7] = Rook((0, 7), self.opponent)

        self.board[1][0] = Pawn((1, 0), self.opponent)
        self.board[1][1] = Pawn((1, 1), self.opponent)
        self.board[1][2] = Pawn((1, 2), self.opponent)
        self.board[1][3] = Pawn((1, 3), self.opponent)
        self.board[1][4] = Pawn((1, 4), self.opponent)
        self.board[1][5] = Pawn((1, 5), self.opponent)
        self.board[1][6] = Pawn((1, 6), self.opponent)
        self.board[1][7] = Pawn((1, 7), self.opponent)

        self.board[6][0] = Pawn((6, 0), self.side)
        self.board[6][1] = Pawn((6, 1), self.side)
        self.board[6][2] = Pawn((6, 2), self.side)
        self.board[6][3] = Pawn((6, 3), self.side)
        self.board[6][4] = Pawn((6, 4), self.side)
        self.board[6][5] = Pawn((6, 5), self.side)
        self.board[6][6] = Pawn((6, 6), self.side)
        self.board[6][7] = Pawn((6, 7), self.side)

        self.board[7][0] = Rook((7, 0), self.side)
        self.board[7][1] = Knight((7, 1), self.side)
        self.board[7][2] = Bishop((7, 2), self.side)
        self.board[7][5] = Bishop((7, 5), self.side)
        self.board[7][6] = Knight((7, 6), self.side)
        self.board[7][7] = Rook((7, 7), self.side)

        # flip the King/Queen depending on the side
        if self.side == 'w':
            qcol = 3
            kcol = 4
        else:
            qcol = 4
            kcol = 3

        self.board[0][qcol] = Queen((0, 3), self.opponent)
        self.board[0][kcol] = King((0, 4), self.opponent)
        self.board[7][qcol] = Queen((7, 3), self.side)
        self.board[7][kcol] = King((7, 4), self.side)
Esempio n. 16
0
    def __init__(self, rows, columns):
        self.rows = rows
        self.columns = columns
        self.ready = False
        self.last = None
        self.copy = True

        self.board = [[0 for x in range(8)] for _ in range(rows)]

        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, "blackwhite")
        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")

        self.board[1][0] = Pawn(1, 0, "black")
        self.board[1][1] = Pawn(1, 1, "black")
        self.board[1][2] = Pawn(1, 2, "black")
        self.board[1][3] = Pawn(1, 3, "black")
        self.board[1][4] = Pawn(1, 4, "black")
        self.board[1][5] = Pawn(1, 5, "black")
        self.board[1][6] = Pawn(1, 6, "black")
        self.board[1][7] = Pawn(1, 7, "black")

        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")

        self.board[6][0] = Pawn(6, 0, "white")
        self.board[6][1] = Pawn(6, 1, "white")
        self.board[6][2] = Pawn(6, 2, "white")
        self.board[6][3] = Pawn(6, 3, "white")
        self.board[6][4] = Pawn(6, 4, "white")
        self.board[6][5] = Pawn(6, 5, "white")
        self.board[6][6] = Pawn(6, 6, "white")
        self.board[6][7] = Pawn(6, 7, "white")

        self.p1Name = "Player 1"
        self.p2Name = "Player 2"
        self.turn = "white"

        self.time1 = 900
        self.time2 = 900
        self.storedTime1 = 0
        self.storedTime2 = 0

        self.winner = None
        self.startTime = time.time()
Esempio n. 17
0
 def set_initial_state(self):
     '''
     Creates and initializes the board
     '''
     self.board = [[Rook(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Rook(0)],
                   [Knight(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Knight(0)],
                   [Bishop(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Bishop(0)],
                   [Queen(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Queen(0)],
                   [King(1), Pawn(1), 0, 0, 0, 0, Pawn(0), King(0)],
                   [Bishop(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Bishop(0)],
                   [Knight(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Knight(0)],
                   [Rook(1), Pawn(1), 0, 0, 0, 0, Pawn(0), Rook(0)]]
Esempio n. 18
0
    def populate_board(self):
        """
        Initialize board with positions of pieces.
        """
        self.board = [
            [
                Rook(owner='white', position=(0, 0)),
                Knight(owner='white', position=(1, 0)),
                Bishop(owner='white', position=(2, 0)),
                Queen(owner='white', position=(3, 0)),
                King(owner='white', position=(4, 0)),
                Bishop(owner='white', position=(5, 0)),
                Knight(owner='white', position=(6, 0)),
                Rook(owner='white', position=(7, 0)),
            ],
            [Pawn(owner='white', position=(i, 1)) for i in range(8)],
            *[[None] * 8 for _ in range(4)],
            [Pawn(owner='black', position=(i, 6)) for i in range(8)],
            [
                Rook(owner='black', position=(0, 7)),
                Knight(owner='black', position=(1, 7)),
                Bishop(owner='black', position=(2, 7)),
                Queen(owner='black', position=(3, 7)),
                King(owner='black', position=(4, 7)),
                Bishop(owner='black', position=(5, 7)),
                Knight(owner='black', position=(6, 7)),
                Rook(owner='black', position=(7, 7)),
            ],
        ]

        self.char_board = []
        for row in self.board:
            disp_row = []
            for piece in row:
                if piece:
                    disp_row.append(piece.cli_characterset)
                else:
                    disp_row.append(None)
            self.char_board.append(disp_row)

        for i, row in enumerate(self.board):
            self.pieces_in_play['white'].extend(
                [piece for piece in row if i < 2])
            self.pieces_in_play['black'].extend(
                [piece for piece in row if i > 5])

            # Add reference to the kings for both players
            self.w_king = self.board[0][4]
            self.b_king = self.board[7][4]
Esempio n. 19
0
    def setBoard(self):
        backRank, nextRank = 0, 1

        if(self.color == "black"):
            backRank = 7
            nextRank = 6

        for piece in self.piecesNameList:
            if(piece == "r"):
                self.board[backRank][0] = Rook(self.color, (0, backRank))
                self.board[backRank][7] = Rook(self.color, (7, backRank))
            elif(piece == "n"):
                self.board[backRank][1] = Knight(self.color, (1, backRank))
                self.board[backRank][6] = Knight(self.color, (6, backRank))
            elif(piece == "b"):
                self.board[backRank][2] = Bishop(self.color, (2, backRank))
                self.board[backRank][5] = Bishop(self.color, (5, backRank))
            elif(piece == "q"):
                self.board[backRank][3] = Queen(self.color, (3, backRank))
            elif(piece == "k"):
                self.board[backRank][4] = King(self.color, (4, backRank))
            else:
                for file in range(0, 8):
                    self.board[nextRank][file] = Pawn(self.color, 
                        (file, nextRank))
Esempio n. 20
0
 def create_pieces(self, config):
     self.pieces = []
     self.king = None
     for pdict in config:
         if pdict['name'] == 'pawn':
             pawn = Pawn(pdict['coord'], pdict['c'])
             self.pieces.append(pawn)
         elif pdict['name'] == 'bishop':
             bishop = Bishop(pdict['coord'], pdict['c'])
             self.pieces.append(bishop)
         elif pdict['name'] == 'queen':
             queen = Queen(pdict['coord'], pdict['c'])
             self.pieces.append(queen)
         elif pdict['name'] == 'rock':
             rock = Rock(pdict['coord'], pdict['c'])
             self.pieces.append(rock)
         elif pdict['name'] == 'knight':
             knight = Knight(pdict['coord'], pdict['c'])
             self.pieces.append(knight)
         elif pdict['name'] == 'king':
             king = King(pdict['coord'], pdict['c'])
             self.pieces.append(king)
             self.king = king
     
     # get player's color
     self.color = self.king.color
Esempio n. 21
0
def makePiece(pieceString, x, y, color):
    """This method returns a piece in a given x, y, and color
    
    Args: 
        x - x coordinate - integer
        y - y coordinate - integer
        color - White, Black
        
    Returns:
        Piece object
    """
    if (pieceString == "King"):
        piece = King()
    elif (pieceString == "Queen"):
        piece = Queen()
    elif (pieceString == "Rook"):
        piece = Rook()
    elif (pieceString == "Bishop"):
        piece = Bishop()
    elif (pieceString == "Pawn"):
        piece = Pawn()
    elif (pieceString == "Knight"):
        piece = Knight()
    piece.setX(x)
    piece.setY(y)
    if (color == "White"):
        piece.setColor(0)
    if (color == "Black"):
        piece.setColor(1)
    return piece
Esempio n. 22
0
    def create_pieces(self):
        for player, f_row, b_row in zip([self.white, self.black], [1, 6],
                                        [0, 7]):
            #pawns
            for col in self.cols:
                p = Pawn(player)
                if player == self.black:
                    p.direction = -1
                player.add_piece(p)
                self.grid[col][f_row].add_piece(p)

            #rook
            for col in ['a', 'h']:
                p = Rook(player)
                player.add_piece(p)
                self.grid[col][b_row].add_piece(p)

            #knight
            for col in ['b', 'g']:
                p = Knight(player)
                player.add_piece(p)
                self.grid[col][b_row].add_piece(p)

            #bishop
            for col in ['c', 'f']:
                p = Bishop(player)
                player.add_piece(p)
                self.grid[col][b_row].add_piece(p)

            #queen king
            for col, piece in zip(['d', 'e'], [Queen(player), King(player)]):
                player.add_piece(piece)
                self.grid[col][b_row].add_piece(piece)
                if isinstance(piece, King):
                    player.king = piece
Esempio n. 23
0
def unpack(packed_board):
    board = Board()
    board.clear()
    board.moves = eval(packed_board["moves"])
    board.can_castle = eval(packed_board["can_castle"])
    b = packed_board["board"]
    board.player_to_move = packed_board['player_to_move']
    m = board.matrix
    for i in range(8):
        for j in range(8):
            square = b[i][j]
            if square == None:
                m[i][j] = 0
            else:
                color = (square["color"] == "white") * 2 - 1
                row = square["pos_row"]
                column = square["pos_col"]
                if square["type"] == "rook":
                    Rook(color, row, column, board)
                elif square["type"] == "knight":
                    Knight(color, row, column, board)
                elif square["type"] == "bishop":
                    Bishop(color, row, column, board)
                elif square["type"] == "queen":
                    Queen(color, row, column, board)
                elif square["type"] == "pawn":
                    Pawn(color, row, column, board)
                elif square["type"] == "king":
                    kk = King(color, row, column, board)
                    if color == 1:
                        board.white_king = kk
                    else:
                        board.black_king = kk
    return board
Esempio n. 24
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
def test_king_reach():
    king = King(mock_board, 5, 5, WHITE)
    assert king.can_reach(6, 6)
    assert king.can_reach(4, 5)
    assert king.can_reach(5, 4)
    assert not king.can_reach(5, 3)
    assert not king.can_reach(3, 5)
    assert not king.can_reach(1, 1)
Esempio n. 26
0
    def populateBoard(self):
        """ Populates the board array"""
        self.pieceByPosDict = {
            "b8": Knight("black_knight_b", Pos("b8")),
            "g8": Knight("black_knight_g", Pos("g8")),
            "d8": Queen("black_queen", Pos("d8")),
            "e8": King("black_king", Pos("e8")),
            "a8": Rook("black_rook_a", Pos("a8")),
            "h8": Rook("black_rook_h", Pos("h8")),
            "c8": Bishop("black_bishop_c", Pos("c8")),
            "f8": Bishop("black_bishop_f", Pos("f8")),
            "a7": Pawn("black_pawn_a", Pos("a7")),
            "b7": Pawn("black_pawn_b", Pos("b7")),
            "c7": Pawn("black_pawn_c", Pos("c7")),
            "d7": Pawn("black_pawn_d", Pos("d7")),
            "e7": Pawn("black_pawn_e", Pos("e7")),
            "f7": Pawn("black_pawn_f", Pos("f7")),
            "g7": Pawn("black_pawn_g", Pos("g7")),
            "h7": Pawn("black_pawn_h", Pos("h7")),
            "b1": Knight("white_knight_b", Pos("b1")),
            "g1": Knight("white_knight_g", Pos("g1")),
            "d1": Queen("white_queen", Pos("d1")),
            "e1": King("white_king", Pos("e1")),
            "a1": Rook("white_rook_a", Pos("a1")),
            "h1": Rook("white_rook_h", Pos("h1")),
            "c1": Bishop("white_bishop_c", Pos("c1")),
            "f1": Bishop("white_bishop_f", Pos("f1")),
            "a2": Pawn("white_pawn_a", Pos("a2")),
            "b2": Pawn("white_pawn_b", Pos("b2")),
            "c2": Pawn("white_pawn_c", Pos("c2")),
            "d2": Pawn("white_pawn_d", Pos("d2")),
            "e2": Pawn("white_pawn_e", Pos("e2")),
            "f2": Pawn("white_pawn_f", Pos("f2")),
            "g2": Pawn("white_pawn_g", Pos("g2")),
            "h2": Pawn("white_pawn_h", Pos("h2")),
        }

        # Let pieces have a reference to the board
        for key, pieceObj in self.pieceByPosDict.items():
            pieceObj.board = self

        # update the screen datastructure
        for keyStr, pieceObj in self.pieceByPosDict.items():
            pos = Pos(keyStr)
            self.setScreen(pos.x, pos.y, pieceObj.img)
Esempio n. 27
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
Esempio n. 28
0
def test_check_checked_checking_true_two():
    board = Board()
    board.board[3][1] = King(3, 1, "w")
    rook = Rook(3, 7, "b")
    board.board[3][7] = rook
    rook.valid_moves(board)
    actual = board.check_status()
    expected = 'w'
    assert actual == expected
Esempio n. 29
0
    def __init__(self, color, player=1):

        self.name, y = ('player1', 1) if player == 1 else ('player2', 8)
        self.color = color
        self.pawns = [Pawn(color, (x, (2 if player == 1 else 7))) for x in range(1, 9)]
        self.king = King(color, (5, y))
        self.pieces = [Rook(color, (1, y)), Rook(color, (8, y)), Knight(color, (2, y)), Knight(color, (7, y)),
        Bishop(color, (3, y)), Bishop(color, (6, y)), Queen(color, (4, y)), self.king] + self.pawns
        self.turn = False
Esempio n. 30
0
    def __init__(self):
        self._grid = [
            SPACE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_8[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_7[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_6[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_5[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_4[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_3[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_2[:],
            PIPE_AND_UNDERSCORE[:], PIPE_AND_SPACE[:], PIPE_AND_SPACE_1[:],
            PIPE_AND_UNDERSCORE[:], FILES_DRAW[:]
        ]
        self.current_state = {
            'player': SIDES[0],
            'move': None,
            'state': {f + r: None
                      for f in FILES for r in RANKS}
        }
        self.history = []
        self.num_moves = 0

        self._put(Rook(self, SIDES[0], 'a', '1'), 'a', '1')
        self._put(Rook(self, SIDES[0], 'h', '1'), 'h', '1')
        self._put(Knight(self, SIDES[0], 'b', '1'), 'b', '1')
        self._put(Knight(self, SIDES[0], 'g', '1'), 'g', '1')
        self._put(Bishop(self, SIDES[0], 'c', '1'), 'c', '1')
        self._put(Bishop(self, SIDES[0], 'f', '1'), 'f', '1')
        self._put(Queen(self, SIDES[0], 'd', '1'), 'd', '1')
        self._put(King(self, SIDES[0], 'e', '1'), 'e', '1')

        for f in FILES:
            self._put(Pawn(self, SIDES[0], f, '2'), f, '2')

        self._put(Rook(self, SIDES[1], 'a', '8'), 'a', '8')
        self._put(Rook(self, SIDES[1], 'h', '8'), 'h', '8')
        self._put(Knight(self, SIDES[1], 'b', '8'), 'b', '8')
        self._put(Knight(self, SIDES[1], 'g', '8'), 'g', '8')
        self._put(Bishop(self, SIDES[1], 'c', '8'), 'c', '8')
        self._put(Bishop(self, SIDES[1], 'f', '8'), 'f', '8')
        self._put(Queen(self, SIDES[1], 'd', '8'), 'd', '8')
        self._put(King(self, SIDES[1], 'e', '8'), 'e', '8')

        for f in FILES:
            self._put(Pawn(self, SIDES[1], f, '7'), f, '7')