Exemplo n.º 1
0
    def createBoard(self):
        for tile in range(64):
            self.gameTiles[tile] = Tile(tile, 'white', NullPiece(), False,
                                        False)

        self.gameTiles[0] = Tile(0, 'white', rook.Rook("Black", 0), False,
                                 False)
        self.gameTiles[1] = Tile(1, 'black', knight.Knight("Black", 1), False,
                                 False)
        self.gameTiles[2] = Tile(2, 'white', Bishop("Black", 2), False, False)
        self.gameTiles[3] = Tile(3, 'black', Queen("Black", 3), False, False)
        self.gameTiles[4] = Tile(4, 'white', king.King("Black", 4), False,
                                 False)
        self.gameTiles[5] = Tile(5, 'black', Bishop("Black", 5), False, False)
        self.gameTiles[6] = Tile(6, 'white', knight.Knight("Black", 6), False,
                                 False)
        self.gameTiles[7] = Tile(7, 'black', rook.Rook("Black", 7), False,
                                 False)
        self.gameTiles[8] = Tile(8, 'black', pawn.Pawn("Black", 8), False,
                                 False)
        self.gameTiles[9] = Tile(9, 'white', pawn.Pawn("Black", 9), False,
                                 False)
        self.gameTiles[10] = Tile(10, 'black', pawn.Pawn("Black", 10), False,
                                  False)
        self.gameTiles[11] = Tile(11, 'white', pawn.Pawn("Black", 11), False,
                                  False)
        self.gameTiles[12] = Tile(12, 'black', pawn.Pawn("Black", 12), False,
                                  False)
        self.gameTiles[13] = Tile(13, 'white', pawn.Pawn("Black", 13), False,
                                  False)
        self.gameTiles[14] = Tile(14, 'black', pawn.Pawn("Black", 14), False,
                                  False)
        self.gameTiles[15] = Tile(15, 'white', pawn.Pawn("Black", 15), False,
                                  False)

        count1 = 0
        count2 = 0
        color = ['white', 'black']
        for tile in range(16, 48):
            for _ in range(8):
                self.gameTiles[tile] = Tile(tile, color[count1 % 2],
                                            NullPiece(), False, False)
            count1 += 1
            count2 += 1
            if count2 % 8 == 0:
                count1 += 1
                count2 = 0

        self.gameTiles[48] = Tile(48, 'white', pawn.Pawn("White", 48), False,
                                  False)
        self.gameTiles[49] = Tile(49, 'black', pawn.Pawn("White", 49), False,
                                  False)
        self.gameTiles[50] = Tile(50, 'white', pawn.Pawn("White", 50), False,
                                  False)
        self.gameTiles[51] = Tile(51, 'black', pawn.Pawn("White", 51), False,
                                  False)
        self.gameTiles[52] = Tile(52, 'white', pawn.Pawn("White", 52), False,
                                  False)
        self.gameTiles[53] = Tile(53, 'black', pawn.Pawn("White", 53), False,
                                  False)
        self.gameTiles[54] = Tile(54, 'white', pawn.Pawn("White", 54), False,
                                  False)
        self.gameTiles[55] = Tile(55, 'black', pawn.Pawn("White", 55), False,
                                  False)
        self.gameTiles[56] = Tile(56, 'black', rook.Rook("White", 56), False,
                                  False)
        self.gameTiles[57] = Tile(57, 'white', knight.Knight("White", 57),
                                  False, False)
        self.gameTiles[58] = Tile(58, 'black', Bishop("White", 58), False,
                                  False)
        self.gameTiles[59] = Tile(59, 'white', Queen("White", 59), False,
                                  False)
        self.gameTiles[60] = Tile(60, 'black', king.King("White", 60), False,
                                  False)
        self.gameTiles[61] = Tile(61, 'white', Bishop("White", 61), False,
                                  False)
        self.gameTiles[62] = Tile(62, 'black', knight.Knight("White", 62),
                                  False, False)
        self.gameTiles[63] = Tile(63, 'white', rook.Rook("White", 63), False,
                                  False)
Exemplo n.º 2
0
    def createBoard(self):

        for x in range(64):
            self.gameTiles[x] = Tile(x, nullPiece.NullPiece())
        self.gameTiles[0] = Tile(0, rook.Rook("Black", 0))
        self.gameTiles[1] = Tile(1, knight.Knight("Black", 1))
        self.gameTiles[2] = Tile(2, bishop.Bishop("Black", 2))
        self.gameTiles[3] = Tile(3, queen.Queen("Black", 3))
        self.gameTiles[4] = Tile(4, king.King("Black", 4))
        self.gameTiles[5] = Tile(5, bishop.Bishop("Black", 5))
        self.gameTiles[6] = Tile(6, knight.Knight("Black", 6))
        self.gameTiles[7] = Tile(7, rook.Rook("Black", 7))
        self.gameTiles[8] = Tile(8, pawn.Pawn("Black", 8))
        self.gameTiles[9] = Tile(9, pawn.Pawn("Black", 9))
        self.gameTiles[10] = Tile(10, pawn.Pawn("Black", 10))
        self.gameTiles[11] = Tile(11, pawn.Pawn("Black", 11))
        self.gameTiles[12] = Tile(12, pawn.Pawn("Black", 12))
        self.gameTiles[13] = Tile(13, pawn.Pawn("Black", 13))
        self.gameTiles[14] = Tile(14, pawn.Pawn("Black", 14))
        self.gameTiles[15] = Tile(15, pawn.Pawn("Black", 15))

        # self.gameTiles[25] = Tile(25, pawn.Pawn("White", 25))
        # #self.gameTiles[18] = Tile(18, pawn.Pawn("Black", 18))
        # self.gameTiles[26] = Tile(26, pawn.Pawn("Black", 26))
        # self.gameTiles[24] = Tile(24, pawn.Pawn("Black", 24))

        self.gameTiles[48] = Tile(48, pawn.Pawn("White", 48))
        self.gameTiles[49] = Tile(49, pawn.Pawn("White", 49))
        self.gameTiles[50] = Tile(50, pawn.Pawn("White", 50))
        self.gameTiles[51] = Tile(51, pawn.Pawn("White", 51))
        self.gameTiles[52] = Tile(52, pawn.Pawn("White", 52))
        self.gameTiles[53] = Tile(53, pawn.Pawn("White", 53))
        self.gameTiles[54] = Tile(54, pawn.Pawn("White", 54))
        self.gameTiles[55] = Tile(55, pawn.Pawn("White", 55))
        self.gameTiles[56] = Tile(56, rook.Rook("White", 56))
        self.gameTiles[57] = Tile(57, knight.Knight("White", 57))
        self.gameTiles[58] = Tile(58, bishop.Bishop("White", 58))
        self.gameTiles[59] = Tile(59, queen.Queen("White", 59))
        self.gameTiles[60] = Tile(60, king.King("White", 60))
        self.gameTiles[61] = Tile(61, bishop.Bishop("White", 61))
        self.gameTiles[62] = Tile(62, knight.Knight("White", 62))
        self.gameTiles[63] = Tile(63, rook.Rook("White", 63))
Exemplo n.º 3
0
    def createNewBoard(self):

        newBoard = Board()
        gameTiles = {}

        # IF THERE WAS AN ENPASSANT PAWN AND WE USED A PAWN TO ATTACK IT'S SQUARE
        # SET A MARKED FOR IT FOR IT TO BE SKIPPED
        enpassLocation = None
        if self.movedPiece.toString() == 'P':
            if not self.board.enPassPawn == None:
                if self.destination == self.board.enPassPawnBehind:
                    enpassLocation = self.board.enPassPawn.position
        elif self.movedPiece.toString() == 'p':
            if not self.board.enPassPawn == None:
                if self.destination == self.board.enPassPawnBehind:
                    enpassLocation = self.board.enPassPawn.position

        # FILL NEW BOARD WITH EVERYTHING EXCEPT DESTINATION, CURRENT LOCATION AND ENPASS LOCATION
        # ELSE FILL IT WITH A NULL PIECE FOR NOW
        for tile in range(64):
            if not tile == self.movedPiece.position and not tile == self.destination and not tile == enpassLocation:
                gameTiles[tile] = self.board.gameTiles[tile]
            else:
                gameTiles[tile] = Tile(tile, nullPiece.NullPiece())

        # IF WE CASTLED, REPLACE THE ROOK WITH A NULL AND MOVE IT TO CORRECT POSITION
        if self.movedPiece.toString() == 'K' and self.movedPiece.firstMove:
            if self.destination == 2:
                if self.board.gameTiles[0].pieceOnTile.toString() == "R" \
                        and self.board.gameTiles[0].pieceOnTile.firstMove:

                    gameTiles[0] = Tile(0, nullPiece.NullPiece())
                    gameTiles[3] = Tile(3, rook.Rook("Black", 3))
            elif self.destination == 6:
                if self.board.gameTiles[7].pieceOnTile.toString() == "R" \
                        and self.board.gameTiles[7].pieceOnTile.firstMove:

                    gameTiles[7] = Tile(7, nullPiece.NullPiece())
                    gameTiles[5] = Tile(5, rook.Rook("Black", 5))

        # CASTLE FOR WHITE
        elif self.movedPiece.toString() == 'k':
            if self.destination == 58:
                if self.board.gameTiles[56].pieceOnTile.toString() == "r" \
                        and self.board.gameTiles[56].pieceOnTile.firstMove:

                    gameTiles[56] = Tile(56, nullPiece.NullPiece())
                    gameTiles[59] = Tile(59, rook.Rook("White", 59))
            elif self.destination == 62:
                if self.board.gameTiles[63].pieceOnTile.toString() == "r" \
                        and self.board.gameTiles[56].pieceOnTile.firstMove:

                    gameTiles[63] = Tile(63, nullPiece.NullPiece())
                    gameTiles[61] = Tile(61, rook.Rook("White", 61))

        # FINALLY CREATE A COPY OF MOVED PIECE AND ASSIGN IT TO ITS LOCATION
        updatePiece = copy.copy(self.movedPiece)
        updatePiece.firstMove = False
        updatePiece.position = self.destination
        gameTiles[self.destination] = Tile(self.destination, updatePiece)
        # THEN ASSIGN NEW BOARD'S TILES AS NEW TILES
        newBoard.gameTiles = gameTiles

        # IT WAS A PAWN JUMP MOVE, ASSIGN IT TO BE THE ENPASS PAWN AND SET IT'S LOCATION BEHIND IT
        if self.movedPiece.toString() == 'P':
            if self.movedPiece.position + 16 == self.destination:
                newBoard.enPassPawn = updatePiece
                newBoard.enPassPawnBehind = self.movedPiece.position + 8
        elif self.movedPiece.toString() == 'p':
            if self.movedPiece.position - 16 == self.destination:
                newBoard.enPassPawn = updatePiece
                newBoard.enPassPawnBehind = self.movedPiece.position - 8

        # IF IT WAS A PAWN MOVE AND IT REACHED THE LAST RANK, REPLACE IT WAS A QUEEN
        if self.movedPiece.toString() == 'P':
            if self.destination in self.movedPiece.eighthRow:
                newBoard.gameTiles[self.destination] = Tile(
                    self.destination, queen.Queen("Black", self.destination))
        elif self.movedPiece.toString() == 'p':
            if self.destination in self.movedPiece.firstRow:
                newBoard.gameTiles[self.destination] = Tile(
                    self.destination, queen.Queen("White", self.destination))

        # SWITCH CURRENT PLAYER OF THE BOARD
        newBoard.currentPlayer = self.board.currentPlayer
        if newBoard.currentPlayer == "White":
            newBoard.currentPlayer = "Black"
        elif newBoard.currentPlayer == "Black":
            newBoard.currentPlayer = "White"

        # WITH THE NEW BOARD CREATED, CHECK IF THE OPPONENT DID A VALID MOVE
        good = self.checkChecks(newBoard)

        if not good:
            return False

        mate = self.checkCheckmateOrStalemate(newBoard, newBoard.currentPlayer)
        if mate:
            return False

        return newBoard
Exemplo n.º 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())
Exemplo n.º 5
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.º 6
0
    def createNewBoard(self):

        newBoard = Board()
        gameTiles = {}

        # TODO CHECK if enpassant attack
        enpassLocation = None
        if self.movedPiece.toString() == 'P':
            if not self.board.enPassPawn == None:
                if self.destination == self.board.enPassPawnBehind:
                    #print("b enpassatk")
                    enpassLocation = self.board.enPassPawn.position
        elif self.movedPiece.toString() == 'p':
            if not self.board.enPassPawn == None:
                if self.destination == self.board.enPassPawnBehind:
                    #print("w enpassatk")
                    enpassLocation = self.board.enPassPawn.position

        for tile in range(64):
            #print(tile)

            if not tile == self.movedPiece.position and not tile == self.destination and not tile == enpassLocation:
                gameTiles[tile] = self.board.gameTiles[tile]
            else:
                gameTiles[tile] = Tile(tile, nullPiece.NullPiece())

        # TODO Check if it is castle move
        if self.movedPiece.toString() == 'K' and self.movedPiece.firstMove:
            if self.destination == 2:
                if self.board.gameTiles[0].pieceOnTile.toString(
                ) == "R" and self.board.gameTiles[0].pieceOnTile.firstMove:
                    #print('b qc')
                    gameTiles[0] = Tile(0, nullPiece.NullPiece())
                    gameTiles[3] = Tile(3, rook.Rook("Black", 3))
            elif self.destination == 6:
                if self.board.gameTiles[7].pieceOnTile.toString(
                ) == "R" and self.board.gameTiles[7].pieceOnTile.firstMove:
                    #print('b kc')
                    gameTiles[7] = Tile(7, nullPiece.NullPiece())
                    gameTiles[5] = Tile(5, rook.Rook("Black", 5))

        elif self.movedPiece.toString() == 'k':
            if self.destination == 58:
                if self.board.gameTiles[56].pieceOnTile.toString(
                ) == "r" and self.board.gameTiles[56].pieceOnTile.firstMove:
                    #print('w qc')
                    gameTiles[56] = Tile(56, nullPiece.NullPiece())
                    gameTiles[59] = Tile(59, rook.Rook("White", 59))
            elif self.destination == 62:
                if self.board.gameTiles[63].pieceOnTile.toString(
                ) == "r" and self.board.gameTiles[56].pieceOnTile.firstMove:
                    #print('w kc')
                    gameTiles[63] = Tile(63, nullPiece.NullPiece())
                    gameTiles[61] = Tile(61, rook.Rook("White", 61))

        updatePiece = copy.copy(self.movedPiece)
        updatePiece.firstMove = False
        updatePiece.position = self.destination
        gameTiles[self.destination] = Tile(self.destination, updatePiece)
        newBoard.gameTiles = gameTiles

        # TODO if pawn was jump, set it to enpassant pawn
        if self.movedPiece.toString() == 'P':
            if self.movedPiece.position + 16 == self.destination:
                #print("Black j")
                newBoard.enPassPawn = updatePiece
                newBoard.enPassPawnBehind = self.movedPiece.position + 8
        elif self.movedPiece.toString() == 'p':
            if self.movedPiece.position - 16 == self.destination:
                #print("White j")
                newBoard.enPassPawn = updatePiece
                newBoard.enPassPawnBehind = self.movedPiece.position - 8

        #TODO add promotion
        if self.movedPiece.toString() == 'P':
            if self.destination in self.movedPiece.eighthRow:
                print("Promote")
                newBoard.gameTiles[self.destination] = Tile(
                    self.destination, queen.Queen("Black", self.destination))
        elif self.movedPiece.toString() == 'p':
            if self.destination in self.movedPiece.firstRow:
                print("Promote")
                newBoard.gameTiles[self.destination] = Tile(
                    self.destination, queen.Queen("White", self.destination))

        # Switch players
        newBoard.currentPlayer = self.board.currentPlayer
        if newBoard.currentPlayer == "White":
            newBoard.currentPlayer = "Black"
        elif newBoard.currentPlayer == "Black":
            newBoard.currentPlayer = "White"

        # TODO Check checks
        good = self.checkChecks(newBoard)

        if not good:
            # print("InvalidMove")
            # return self.board
            return False

        return newBoard
Exemplo n.º 7
0
    def createBoard(self):
        for tile in range(64):
            self.gameTiles[tile] = Tile(tile, NullPiece())
        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))
        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))

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