Exemple #1
0
def castleRook(prevY, newY, selectedPiece):
    print("move rook")
    # print("toString after call", selectedPiece.toString())
    # **old and new positions:
    # Lf bR = (0,0) => (0, 3)
    # Rt bR = (0,7) => (0, 5)
    # Lf wR = (7, 0) => (7, 3)
    # Rt wR = (7, 7) => (7, 5)
    # bK (0, 4) => L (0, 2), R (0, 6)
    # wK (7, 4) => L (7, 2), R(7, 6)
    castleState = Castling(chessBoard, selectedPiece.validMove, selectedPiece)

    if (selectedPiece.toString() == "K" and castleState.canCastle()):
        if (selectedPiece.alliance == "B"):
            if (newY == prevY + 2):  # black right
                chessBoard.updateBoard(0, 5,
                                       chessBoard.board[0][7].pieceOccupy)
                chessBoard.board[0][5].pieceOccupy.y_coord = 5
                chessBoard.updateBoard(0, 7, nullPiece())
            elif (newY == prevY - 2):  # black left
                chessBoard.updateBoard(0, 3,
                                       chessBoard.board[0][0].pieceOccupy)
                chessBoard.board[0][3].pieceOccupy.y_coord = 3
                chessBoard.updateBoard(0, 0, nullPiece())
        else:
            if (newY == prevY + 2):  # white right
                chessBoard.updateBoard(7, 5,
                                       chessBoard.board[7][7].pieceOccupy)
                chessBoard.board[7][5].pieceOccupy.y_coord = 5
                chessBoard.updateBoard(7, 7, nullPiece())
            elif (newY == prevY - 2):  # white left
                chessBoard.updateBoard(7, 3,
                                       chessBoard.board[7][0].pieceOccupy)
                chessBoard.board[7][3].pieceOccupy.y_coord = 3
                chessBoard.updateBoard(7, 0, nullPiece())
Exemple #2
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))
Exemple #3
0
    def minimax(self, board, depth, maximizingplayer, turn):
        move_dictionary = self.fillMoveDictionary(board, turn)
        self.best_list = []
        if depth == 0:
            return self.boardEval(board), 0

        if maximizingplayer:
            best_move = -9999999
            for key in move_dictionary:
                for move in move_dictionary[key]:
                    captured_piece = board.squares[move].piece
                    board.squares[move].piece = board.squares[key].piece
                    board.squares[move].piece.position = move
                    board.squares[key].piece = nullPiece()
                    best_move = max(
                        best_move,
                        self.minimax(board, depth - 1, not maximizingplayer,
                                     "Black")[0])
                    self.best_list.append([key, move, best_move])
                    board.squares[key].piece = board.squares[move].piece
                    board.squares[key].piece.position = key
                    board.squares[move].piece = captured_piece
                    board.printBoard()
            print(self.best_list)
            return best_move, board.squares[key].piece, move
        else:
            best_move = 9999999
            for key in move_dictionary:
                for move in move_dictionary[key]:
                    captured_piece = board.squares[move].piece
                    board.squares[move].piece = board.squares[key].piece
                    board.squares[move].piece.position = move
                    board.squares[key].piece = nullPiece()
                    last_best = best_move
                    best_move = min(
                        best_move,
                        self.minimax(board, depth - 1, not maximizingplayer,
                                     "White")[0])
                    self.best_list.append([key, move, best_move])
                    board.squares[key].piece = board.squares[move].piece
                    board.squares[key].piece.position = key
                    board.squares[move].piece = captured_piece
                    board.printBoard()
            print(self.best_list)

            return best_move, board.squares[self.best_list[1]
                                            [0]].piece, self.best_list[1][1]
Exemple #4
0
    def Move(self, selectedPiece, x, y):
        chessBoard = self.chessBoard
        x_origin = selectedPiece.x_coord
        y_origin = selectedPiece.y_coord
        moves = 0

        if chessBoard.board[x][y].pieceOccupy.toString() != "0":
            moves = 0
        else:
            moves = 1 

        if selectedPiece.toString() == "P":
            moves = 0
            if selectedPiece.x_coord +2 == x or selectedPiece.x_coord -2 == x:
                selectedPiece.passP = True

            if selectedPiece.alliance == "B" and y != y_origin:
                if chessBoard.board[x-1][y].pieceOccupy.toString() == "P":
                    if chessBoard.board[x-1][y].pieceOccupy.passP == True:
                        chessBoard.updateBoard(x-1, y, nullPiece())

            if selectedPiece.alliance == "W" and y != y_origin:
                if chessBoard.board[x+1][y].pieceOccupy.toString() == "P":
                    if chessBoard.board[x+1][y].pieceOccupy.passP == True:
                        chessBoard.updateBoard(x+1, y, nullPiece())
        selectedPiece.x_coord = x
        selectedPiece.y_coord = y
        selectedPiece.fMove = False
        self.castleRook(y_origin, y, selectedPiece)
        chessBoard.updateBoard(x, y, selectedPiece)
        chessBoard.updateBoard(x_origin, y_origin, nullPiece())
        if selectedPiece.toString() == "P":
            promoteChoice = ["Q", "R", "N", "B"]
            if selectedPiece.alliance == "W" and selectedPiece.x_coord == 0:
                promoteTo = random.choice(promoteChoice)
                chessBoard.promote(selectedPiece.x_coord, selectedPiece.y_coord, "W", promoteTo)

            if selectedPiece.alliance == "B" and selectedPiece.x_coord == 7:
                promoteTo = random.choice(promoteChoice)
                chessBoard.promote(selectedPiece.x_coord, selectedPiece.y_coord, "B", promoteTo)

        return moves
Exemple #5
0
def updateBoard(x, y, new_x, new_y):

    initial_position = getPos(int(y / 100) + 1, int(x / 100) + 1)

    new_position = getPos(int(new_y / 100) + 1, int(new_x / 100) + 1)

    print("new_position :", new_position, (new_x / 100) + 1, (new_y / 100) + 1)
    chessBoard.squares[new_position].piece = chessBoard.squares[
        initial_position].piece
    chessBoard.squares[new_position].piece.position = new_position
    chessBoard.squares[initial_position].piece = nullPiece()
Exemple #6
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))
Exemple #7
0
def main():
    global gO
    global currentAlliance
    global currentPieces
    global wPieces
    global bPieces
    global checked
    global mode
    global selectedPiece
    global pieceMove
    global count
    global moves

    while not gO:

        if currentAlliance == "W":
            currentPieces = wPieces
        else:
            currentPieces = bPieces
        if mode == "DB" and currentAlliance != playerAlliance:
            selectedPiece, move = dumbBot(chessBoard,
                                          currentPieces).randomMoves()
            if move != 0:
                moves[currentAlliance] += move
            else:
                moves[currentAlliance] = 0
            if selectedPiece is None:
                gO = True
                display_gOmessage("Game Over, Bot lose")
                break
            print(moves)
            switchSide()
            continue
        ReturnButton(screen)
        for event in pygame.event.get():
            #print(event)
            if event.type == pygame.QUIT:
                gO = True
                pygame.quit()
                quit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                #get UI coordinate
                cols, rows = pygame.mouse.get_pos()
                if cols > 600 or rows > 650:
                    pass
                if 5 < cols < 295 and 605 < rows < 645:
                    gO = True
                elif 305 < cols < 595 and 605 < rows < 645:
                    exit()

                for i in currentPieces:
                    if i[0] < rows < i[0] + 75 and i[1] < cols < i[1] + 75:
                        bRows = (int)(i[0] / 75)
                        bCols = (int)(i[1] / 75)
                        if currentAlliance == "B" and mode == "P2F":
                            bRows = (int)((525 - i[0]) / 75)
                            bCols = (int)((525 - i[1]) / 75)
                        if chessBoard.board[bRows][
                                bCols].pieceOccupy.alliance == currentAlliance:
                            #print(bRows, bCols)
                            pieceMove.clear()

                            selectedPiece = chessBoard.board[bRows][
                                bCols].pieceOccupy
                            x_origin = bRows
                            y_origin = bCols
                            print(selectedPiece, "at coordination: [", bRows,
                                  ", ", bCols, "]")

                            ck = Castling(chessBoard, selectedPiece.validMove,
                                          selectedPiece)

                            if (checked == True):
                                kingMove = check.isCheckMate()
                                protector = check.toProtect()
                                if (protector == True):
                                    display_message(
                                        "Must move a pice to protect the king or move king"
                                    )
                                    if (selectedPiece.toString() == 'K'):
                                        pieceMove = check.isCheckMate()
                                    else:
                                        pieceMove = selectedPiece.validMove(
                                            chessBoard.board)
                                else:
                                    if (kingMove == []):
                                        if (currentAlliance == "W"):
                                            display_gOmessage(
                                                "White Is in checkMate, Black Wins"
                                            )
                                            gO = True
                                        else:
                                            display_gOmessage(
                                                "Black Is in checkMate, White Wins"
                                            )
                                            gO = True
                                    if (selectedPiece.toString() != 'K'):
                                        display_message("King must be moved")
                                        pieceMove = []
                                    else:
                                        pieceMove = check.isCheckMate()
                            else:
                                pieceMove = selectedPiece.validMove(
                                    chessBoard.board)
                                if (selectedPiece.toString() == "K"):
                                    if (selectedPiece.alliance == "B"):
                                        if ck.canCastleR():
                                            print("castle bR")
                                            pieceMove.append([0, 6])
                                        if ck.canCastleL():
                                            print("castle bL")
                                            pieceMove.append([0, 2])
                                    else:
                                        if ck.canCastleR():
                                            print("castle wL")
                                            pieceMove.append([7, 6])
                                        if ck.canCastleL():
                                            print("castle wR")
                                            pieceMove.append([7, 2])
                            print("validMoves:", pieceMove)
                            drawBoard()
                            drawPieces(flip)
                            for j in pieceMove:
                                j[0] = j[0] * 75
                                j[1] = j[1] * 75
                                if currentAlliance == "B" and mode == "P2F":
                                    j[0] = 525 - j[0]
                                    j[1] = 525 - j[1]
                                img = pygame.image.load(
                                    "./art/green_circle_neg.png")
                                img = pygame.transform.scale(img, (75, 75))
                                screen.blit(img, (j[1], j[0]))
                                currentPieces.extend([[j[0], j[1]]])
                            #print(currentPieces)
                            break
                        elif selectedPiece != None:
                            if chessBoard.board[bRows][
                                    bCols].pieceOccupy.toString() != "0":
                                moves[currentAlliance] = 0
                            else:
                                moves[currentAlliance] += 1
                            if selectedPiece.toString() == "P":
                                moves[currentAlliance] = 0
                                if selectedPiece.x_coord + 2 == bRows or selectedPiece.x_coord - 2 == bRows:
                                    selectedPiece.passP = True

                                if selectedPiece.alliance == "B" and bCols != y_origin:
                                    if chessBoard.board[
                                            bRows -
                                            1][bCols].pieceOccupy.toString(
                                            ) == "P":
                                        if chessBoard.board[bRows - 1][
                                                bCols].pieceOccupy.passP == True:
                                            chessBoard.updateBoard(
                                                bRows - 1, bCols, nullPiece())

                                if selectedPiece.alliance == "W" and bCols != y_origin:
                                    if chessBoard.board[
                                            bRows +
                                            1][bCols].pieceOccupy.toString(
                                            ) == "P":
                                        if chessBoard.board[bRows + 1][
                                                bCols].pieceOccupy.passP == True:
                                            chessBoard.updateBoard(
                                                bRows + 1, bCols, nullPiece())

                            prevKY = selectedPiece.y_coord
                            selectedPiece.x_coord = bRows
                            selectedPiece.y_coord = bCols
                            newKY = selectedPiece.y_coord
                            castleRook(prevKY, newKY, selectedPiece)
                            if selectedPiece.fMove:
                                selectedPiece.fMove = False
                            chessBoard.updateBoard(bRows, bCols, selectedPiece)
                            chessBoard.updateBoard(x_origin, y_origin,
                                                   nullPiece())
                            count += 1
                            print(moves)
                            # promoting check
                            if selectedPiece.toString() == "P":
                                if selectedPiece.alliance == "W" and selectedPiece.x_coord == 0:
                                    promoteTo = check.promoteCheck(
                                        screen, clock)
                                    chessBoard.promote(selectedPiece.x_coord,
                                                       selectedPiece.y_coord,
                                                       "W", promoteTo)

                                if selectedPiece.alliance == "B" and selectedPiece.x_coord == 7:
                                    promoteTo = check.promoteCheck(
                                        screen, clock)
                                    chessBoard.promote(selectedPiece.x_coord,
                                                       selectedPiece.y_coord,
                                                       "B", promoteTo)

                            switchSide()

            pygame.display.update()
            clock.tick(60)