Exemplo n.º 1
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())
Exemplo n.º 2
0
def switchOutPawn(currPiece, screen):
    screen.blit(switchText, (75, 280))
    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    if currPiece.color:
                        return Queen(currPiece.x, currPiece.y,
                                     "images/whiteQueen.png", 1)
                    else:
                        return Queen(currPiece.x, currPiece.y,
                                     "images/blackQueen.png", 0)
                if event.key == pygame.K_r:
                    if currPiece.color:
                        return Rook(currPiece.x, currPiece.y,
                                    "images/whiteRook.png", 1)
                    else:
                        return Rook(currPiece.x, currPiece.y,
                                    "images/blackRook.png", 0)
                if event.key == pygame.K_b:
                    if currPiece.color:
                        return Bishop(currPiece.x, currPiece.y,
                                      "images/whiteBishop.png", 1)
                    else:
                        return Bishop(currPiece.x, currPiece.y,
                                      "images/blackBishop.png", 0)
                if event.key == pygame.K_k:
                    if currPiece.color:
                        return Knight(currPiece.x, currPiece.y,
                                      "images/whiteKnight.png", 1)
                    else:
                        return Knight(currPiece.x, currPiece.y,
                                      "images/blackKnight.png", 0)
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
class BishopTests(unittest.TestCase):
    def setUp(self):
        chess_coord_white = ChessCoord('F', '8')
        self.bishop_white = Bishop(chess_coord_white, white)
        chess_coord_black = ChessCoord('E', '3')
        self.bishop_black = Bishop(chess_coord_black, black)

    def test_constructor_white(self):
        self.failUnless(self.bishop_white.letter == 'B')
        self.failUnless(self.bishop_white.chess_coord == ChessCoord('F', '8'))
        self.failUnless(self.bishop_white.grid_coord == GridCoord(5, 7))
        self.failUnless(self.bishop_white.colour == white)
        self.failUnless(self.bishop_white.symbol == '♗')
        self.failUnless(util.compare_lists(self.bishop_white.move_directions,
                                           directions.move_directions_bishop()))

    def test_constructor_black(self):
        self.failUnless(self.bishop_black.letter == 'B')
        self.failUnless(self.bishop_black.chess_coord == ChessCoord('E', '3'))
        self.failUnless(self.bishop_black.grid_coord == GridCoord(4, 2))
        self.failUnless(self.bishop_black.colour == black)
        self.failUnless(self.bishop_black.symbol == '♝')
        self.failUnless(util.compare_lists(self.bishop_black.move_directions,
                                           directions.move_directions_bishop()))

    def test_white_bishop_allowed_to_move_south3_west3(self):
        pieces = []
        self.failUnless(self.bishop_white.inspect_move(pieces,
                                                       ChessCoord('C', '5')).is_valid_move)

    def test_black_bishop_allowed_to_move_north4_west4(self):
        pieces = []
        self.failUnless(self.bishop_black.inspect_move(pieces,
                                                       ChessCoord('A', '7')).is_valid_move)
Exemplo n.º 5
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"))
Exemplo n.º 6
0
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),
    ]
Exemplo n.º 7
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
Exemplo n.º 8
0
 def setupBoard(self):
     self.tiles.list = [
         [
             Rook(0, 0, self.tiles, 0),
             Knight(1, 0, self.tiles, 0),
             Bishop(2, 0, self.tiles, 0),
             Queen(3, 0, self.tiles, 0), self.kings[0],
             Bishop(5, 0, self.tiles, 0),
             Knight(6, 0, self.tiles, 0),
             Rook(7, 0, self.tiles, 0)
         ],
         [Pawn(x, 1, self.tiles, 0) for x in range(8)],
         ['', '', '', '', '', '', '', ''],
         ['', '', '', '', '', '', '', ''],
         ['', '', '', '', '', '', '', ''],
         ['', '', '', '', '', '', '', ''],
         [Pawn(x, 6, self.tiles, 1) for x in range(8)],
         [
             Rook(0, 7, self.tiles, 1),
             Knight(1, 7, self.tiles, 1),
             Bishop(2, 7, self.tiles, 1),
             Queen(3, 7, self.tiles, 1), self.kings[1],
             Bishop(5, 7, self.tiles, 1),
             Knight(6, 7, self.tiles, 1),
             Rook(7, 7, self.tiles, 1)
         ],
     ]
Exemplo n.º 9
0
def bishop_take_test():
    print("========================================")
    print("Performing the bishop take test")
    print("========================================")
    board = Board()
    piece = Bishop(Colour.WHITE)
    current_location = Location('c', 3)
    board.__add_piece__(piece, current_location)

    friend_piece = Pawn(Colour.WHITE)
    enemy_piece = Pawn(Colour.BLACK)

    board.__add_piece__(friend_piece, Location('b', 2))
    board.__add_piece__(enemy_piece, Location('d', 4))

    print("Added a Bishop and 2 pawns to the board...")

    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))
Exemplo n.º 10
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"])
Exemplo n.º 11
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)
    def test_taking_king_not_allowed(self):
        d4_bishop = Bishop(ChessCoord('D', '4'), black)
        e3_king = King(ChessCoord('E', '3'), white)
        pieces = [d4_bishop, e3_king]
        move_inspect_result = d4_bishop.inspect_move(pieces, ChessCoord('E', '3'))

        self.failUnless(move_inspect_result ==
                        MoveInspectResult(False, True, [], e3_king))
Exemplo n.º 13
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)
Exemplo n.º 14
0
 def test_board_lock(self):
     board = Board(tk.Toplevel())
     b = Bishop('white', 'white_bishop_1')
     board.handle_board_lock(b, 0, 0)
     self.assertTrue(board.lock)
     b.selected = True
     board.lock = True
     board.handle_board_lock(b, 0, 0)
     self.assertFalse(board.lock)
Exemplo n.º 15
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)
         ],
     ]
    def get_valid_moves(self, board):
        moves = []
        diagonal_moves = Bishop.get_valid_moves(
            Bishop(self.row, self.col, self.color), board)
        straight_moves = Rook.get_valid_moves(
            Rook(self.row, self.col, self.color), board)
        moves.extend(diagonal_moves)
        moves.extend(straight_moves)

        return moves
Exemplo n.º 17
0
 def getLegalMoves(self, board):
     rookQ = Rook(self.color)
     rookQ.position = self.position
     legalRook = rookQ.getLegalMoves(board)
     bishopQ = Bishop(self.color)
     bishopQ.position = self.position
     legalBishop = bishopQ.getLegalMoves(board)
     self.LegalMovesList = [
         legalRook[0] + legalBishop[0], legalRook[1] + legalBishop[1]
     ]
     return self.LegalMovesList
Exemplo n.º 18
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))
Exemplo n.º 19
0
    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))
Exemplo n.º 20
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"))
Exemplo n.º 21
0
 def __init__(self):
     pygame.font.init()
     self.width = 400
     self.height = 500
     self.win = pygame.display.set_mode((self.width, self.height))
     self.squares = [[0 for x in range(8)] for y in range(8)]
     self.square_width = 50
     self.square_height = 50
     self.white_pieces = [Pawn(0) for x in range(8)] + [Rook(0), Knight(0), Bishop(0), Queen(0), King(0), Bishop(0), Knight(0), Rook(0)]
     self.black_pieces = [Pawn(1) for x in range(8)] + [Rook(1), Knight(1), Bishop(1), Queen(1), King(1), Bishop(1), Knight(1), Rook(1)]
     self.current_player = 0
     self.chosen = None
     self.big_font = pygame.font.SysFont('Comic Sans MS', 30, bold=True)
     self.small_font = pygame.font.SysFont('Comic Sans MS', 20, bold=True)
     pygame.display.set_caption("pyChess")
     pygame.display.set_icon(pygame.transform.scale(pygame.image.load("assets/pieces/black/black_king.png"), (32, 32)))
Exemplo n.º 22
0
    def valid_moves(self, board: Board) -> List[str]:
        valid = []

        valid += Rook.valid_moves(self, board)
        valid += Bishop.valid_moves(self, board)

        return valid
Exemplo n.º 23
0
    def test_bishop(self):
        b = board.Board()

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

        self.assertItemsEqual([(0, 0), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6),
                               (7, 7), (0, 2), (2, 0)],
                              Bishop.get_moves((1, 1), b[(1, 1)], b))
Exemplo n.º 24
0
 def promote(self, x, y, alliance, piece):
     if (piece == "Q"):
         self.board[x][y] = Tile(Queen(alliance, x, y))
     if (piece == "B"):
         self.board[x][y] = Tile(Bishop(alliance, x, y))
     if (piece == "R"):
         self.board[x][y] = Tile(Rook(alliance, x, y))
     if (piece == "N"):
         self.board[x][y] = Tile(Knight(alliance, x, y))
Exemplo n.º 25
0
    def test_bishop_get_possible_moves(self):
        matrix = PieceTest.initialize_empty_grid()
        bishop = Bishop('white', 'white_bishop_1')
        positions = [(0, 0), (0, 7), (3, 4), (7, 0), (7, 7)]
        expectations = {
            (0, 0): [(i, i, 'mov') for i in range(1, 8)],
            (0, 7): [(i, 7 - i, 'mov') for i in range(1, 8)],
            (3, 4):
            [(2, 3, 'mov'), (1, 2, 'mov'), (0, 1, 'mov'), (2, 5, 'mov'),
             (1, 6, 'mov'), (0, 7, 'mov'), (4, 3, 'mov'), (5, 2, 'mov'),
             (6, 1, 'mov'), (7, 0, 'mov'), (4, 5, 'mov'), (5, 6, 'mov'),
             (6, 7, 'mov')],
            (7, 0): [(i, 7 - i, 'mov') for i in range(0, 7)],
            (7, 7): [(i, i, 'mov') for i in range(0, 7)],
        }

        for pos in positions:
            possible_moves = bishop.get_possible_moves(pos, matrix)
            self.assertCountEqual(possible_moves, expectations[pos])
Exemplo n.º 26
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
Exemplo n.º 27
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
Exemplo n.º 28
0
    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])
Exemplo n.º 29
0
 def generate_moves_for_piece(
         color: int,
         position: tuple[int, int],
         board: list[list[Piece]],
         only_captures: bool = False) -> list[tuple[int, int]]:
     moves = Rook.generate_moves_for_piece(color, position, board,
                                           only_captures)
     moves.extend(
         Bishop.generate_moves_for_piece(color, position, board,
                                         only_captures))
     return moves
Exemplo n.º 30
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")
Exemplo n.º 31
0
    def is_valid_move(self, board, startx, starty, endx, endy, evaluate_only=True):
        if self.color == "white":
            look_for_color = "black"
        else:
            look_for_color = "white"
        if (startx == endx or startx+1 == endx or startx-1 == endx) and (starty + 1== endy or starty - 1 == endy or starty==endy):
            if Bishop.is_valid_move(self, board, startx, starty, endx, endy) or Rook.is_valid_move(self, board, startx,
                                                                                                   starty, endx, endy):
                if not evaluate_only:
                    self.moved = True
                if board.check_if_tile_under_attack(endx,endy,look_for_color):
                    return False
                return True
        elif starty == endy and abs(endx-startx) == 2:
            # F*****G CASTLING
            if not self.moved:
                look_for_color = None

                if endx < startx:
                    # LEFT
                    # print("Castle to the left")

                    if not board.check_if_tile_under_attack(startx, starty, look_for_color):
                        if not board.check_if_tile_under_attack(startx - 1, starty, look_for_color) and board.is_tile_free(startx - 1, starty):
                            if not board.check_if_tile_under_attack(startx - 2, starty,look_for_color) and board.is_tile_free(startx - 2,starty):
                                if board.is_tile_free(startx-3,starty):
                                    other_piece = board.get_piece_at(0, starty)
                                    if other_piece is not None and not other_piece.moved and isinstance(other_piece, Rook):
                                        if not evaluate_only:
                                            board.board[startx - 4][starty].remove_piece()
                                            board.board[startx - 1][endy].add_piece(other_piece)
                                        if board.check_if_tile_under_attack(endx, endy, look_for_color):
                                            return False
                                        return True

                if startx < endx:
                    # RIGHT
                    # print("Castle to the right")

                    if not board.check_if_tile_under_attack(startx,starty,look_for_color):
                        if not board.check_if_tile_under_attack(startx+1,starty,look_for_color) and board.is_tile_free(startx+1,starty):
                            if not board.check_if_tile_under_attack(startx+2,starty,look_for_color) and board.is_tile_free(startx+2,starty):
                                other_piece = board.get_piece_at(7,starty)
                                if other_piece is not None and not other_piece.moved and isinstance(other_piece,Rook):
                                    if not evaluate_only:
                                        print("Yes move it!")
                                        board.board[startx+3][starty].remove_piece()
                                        board.board[startx+1][endy].add_piece(other_piece)
                                    if board.check_if_tile_under_attack(endx, endy, look_for_color):
                                        return False
                                    return True
        else:
            return False
Exemplo n.º 32
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))
Exemplo n.º 33
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))
Exemplo n.º 34
0
 def setUp(self):
     chess_coord_white = ChessCoord('F', '8')
     self.bishop_white = Bishop(chess_coord_white, white)
     chess_coord_black = ChessCoord('E', '3')
     self.bishop_black = Bishop(chess_coord_black, black)
Exemplo n.º 35
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)