Example #1
0
    def getAction(self):
        bestMove = ""
        with chess.polyglot.open_reader("bookfish.bin") as reader:
            for entry in reader.find_all(self.board):
                bestMove = entry.move.__str__()
                break

        #If the try succeed we can use bookfish, else we use min max with aplha beta
        try:
            move = chess.Move.from_uci(bestMove)
            method = "(Move by Bookfish)"

        except:
            move = MinMax.minimaxRoot(3, self.board, True)
            method = "(Move by Min Max and Alpha Beta pruning"

        #Player
        if (self.turnId % 2 == 0):
            action = input("Donner une action à réaliser (ex:" + str(move) +
                           ") : ")
            while (self.moveEstLegal(action) == False):
                print("L'action n'est pas autorisée, veuillez recommencer")
                action = input("Donner une action à réaliser (ex:" +
                               str(move) + ") : ")
            return chess.Move.from_uci(action)
        else:
            print(self.nameAI + " a joue " + str(move), method)
            return move
Example #2
0
def IAvsIA():
    board = chess.Board()
    display(SVG(chess.svg.board(board=board)))
    print("")
    while not (board.is_game_over()):
        moves = board.legal_moves

        #It look if the best_move function has a move to push
        #If not, we use alpha-beta method
        if best_move(board) == "Nothing":

            move = MinMax.minimaxRoot(3, board, True)
            line.append(move)
            board.push(move)
            display(SVG(chess.svg.board(board=board, lastmove=move)))
            print("")
            #If there is a move from polyglot, we push it
        elif best_move(board) in moves:
            moveToPush = best_move(board)
            line.append(moveToPush)
            board.push(moveToPush)
            display(SVG(chess.svg.board(board=board, lastmove=moveToPush)))
            print("")

        #We look after every move if the game is over in order to end it
        if board.is_game_over():
            print("The game is over")
            print(board.result())
            game.headers["Result"] = board.result()

    ###Game saving ###
    game.add_line(line)
    new_pgn = open("D:/Polytech/FI 3/Proj/gitTheo2.0/savedGames.pgn",
                   "a",
                   encoding="utf-8")
    exporter = chess.pgn.FileExporter(new_pgn)
    game.accept(exporter)

    new_pgn.close()
Example #3
0
    def getAction(self):
        bestMove = ""
        with chess.polyglot.open_reader("bookfish.bin") as reader:
            for entry in reader.find_all(self.board):
                bestMove = entry.move.__str__()
                break

        #If the try succeed we can use bookfish, else we use min max with aplha beta
        try:
            move = chess.Move.from_uci(bestMove)
            method = "(Move by Bookfish)"

        except:
            move = MinMax.minimaxRoot(3, self.board, True)
            method = "Move by Min Max and Alpha Beta pruning"

        #Player
        if (self.turnId % 2 == 0):
            print(self.nameAI1 + " a joue " + str(move), method)
            return move
        else:
            print(self.nameAI2 + " a joue " + str(move), method)
            return move
Example #4
0
def MANvsIA():

    board = chess.Board()
    display(SVG(chess.svg.board(board=board)))

    while not (board.is_game_over()):
        moves = board.legal_moves
        #The MAN start everytime
        man_move = chess.Move.from_uci(input("your move : "))
        if man_move in moves:
            board.push(man_move)
            line.append(man_move)
            display(SVG(chess.svg.board(board=board, lastmove=man_move)))
        else:
            while man_move not in moves:
                print("Illegal move, please try an other one")
                print("Here is the list of legal move")
                for move in board.legal_moves:
                    print(move)
                man_move = chess.Move.from_uci(input("your move : "))
            board.push(man_move)
            line.append(man_move)
            display(SVG(chess.svg.board(board=board, lastmove=man_move)))

    #We look after every move if the game is over in order to end it
        if board.is_game_over():
            print("The game is over")
            print(board.result())
            game.headers["Result"] = board.result()
            print("")

        #Now it's IA turns

        moves = board.legal_moves

        #It look if the best_move function has a move to push
        #If not, we use alpha-beta method
        if best_move(board) == "Nothing":

            move = MinMax.minimaxRoot(3, board, True)
            line.append(move)
            board.push(move)
            display(SVG(chess.svg.board(board=board, lastmove=move)))
            print("")
            #If there is a move from polyglot, we push it
        elif best_move(board) in moves:
            moveToPush = best_move(board)
            line.append(moveToPush)
            board.push(moveToPush)
            display(SVG(chess.svg.board(board=board, lastmove=moveToPush)))
            print("")

        #We look after every move if the game is over in order to end it
        if board.is_game_over():
            print("The game is over")
            print(board.result())
            game.headers["Result"] = board.result()
            print("")

    ###Game saving ###
    game.add_line(line)
    new_pgn = open("D:/Polytech/FI 3/Proj/gitTheo2.0/savedGames.pgn",
                   "a",
                   encoding="utf-8")
    exporter = chess.pgn.FileExporter(new_pgn)
    game.accept(exporter)

    new_pgn.close()