Ejemplo n.º 1
0
def stockFish_init(request):
    stockfish = Stockfish(parameters={
        "Threads": 2,
        "Minimum Thinking Time": 30
    })
    stockfish.set_position(["e2e4", "e7e6"])
    #posición de Forsyth–Edwards Notation (FEN)
    stockfish.set_fen_position(
        "rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2")
    #coge el mejor movimiento
    stockfish.get_best_move(
    )  #stockfish.get_best_move_time(1000) en base al tiempo
    #nivel del motor
    stockfish.set_skill_level(15)
    #parámetros por defecto
    stockfish.get_parameters()
    #coge la posición FEN
    stockfish.get_fen_position()
    #coge el tablero por defecto del usuario
    stockfish.get_board_visual()
    #evalua jugada
    stockfish.get_evaluation()
    p = subprocess.Popen('stockfish-x64.exe',
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    p.stdin.write("position startpos moves e2e4 e7e5\n")
    p.stdin.write("go infinite\n")
    p.stdin.flush()
    time.sleep(1)
    print(p.communicate())
Ejemplo n.º 2
0
def gen_examples(randomness: float = 0.7,
                 randomness_decline: float = 0.95,
                 max_moves: int = 80,
                 table: str = c.DEFAULT_TABLE) -> None:
    """
    Generates training examples using Stockfish and stores them in a database in algebraic notation. Set up a MySQL 
    database first and set the connection in constants.py. Also make sure that Stockfish is installed correctly.

    :param table: Table the data is stored in.
    :param randomness: Starting Probability for proceeding with are random move instead of the best move. This is
        necessary to not simulate the same game each time.
    :param randomness_decline: Factor applied to the randomness with each move. Should be less than 1 to have less 
        randomness later in the game.
    :param max_moves: Stops the simulated game early to prevent too long end games.
    """
    game = Game()
    stockfish = Stockfish(c.STOCKFISH_PATH)
    examples = []
    moves = []
    for _ in range(max_moves):
        stockfish.set_position(moves)
        best_move = stockfish.get_best_move()

        value = _value(stockfish.get_evaluation())
        if best_move:
            examples.append((_truncate_fen(stockfish.get_fen_position()),
                             (best_move[:4], value)))

        if best_move and random.random() > randomness:
            move_alg = best_move
            move_tuple = _from_algebraic(move_alg)
        else:
            move_tuple = random.sample(game.game_legal_moves(), 1)[0]
            move_alg = _to_algebraic(move_tuple)

        if len(move_alg) == 5:
            print('pawn promotion')

        try:
            game.make_move(move_tuple[0], move_tuple[1])
            moves.append(move_alg)
        except ValueError:
            moves[-1] = moves[-1] + 'q'
            print(examples)

        randomness *= randomness_decline

        if game.game_winner():
            break
    db = Connector()
    db.insert_examples(examples, table)
Ejemplo n.º 3
0
#https://pypi.org/project/stockfish/
#https://stockfishchess.org/download/
#I used Linux 64-bit but the compiled versions should work fine
#Michael's Location: "/mnt/c/Users/micha/Desktop/3D Stuff/light-blue/stockfish_20090216_x64"
#Michael's laptop location (blade and thinkpad): "/home/michael/Desktop/light-blue/stockfish_20090216_x64"
#Change the location below to yours (or add to path)
from stockfish import Stockfish
stockfish = Stockfish("stockfish_20090216_x64",parameters={"Threads": 2})

print(stockfish.get_best_move())
stockfish.set_position(['d2d4', 'e7e6'])        #Position as a list of all moves made in order
print(stockfish.get_best_move())

stockfish.set_fen_position("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")  #Starting position in FEN notation 
print(stockfish.get_best_move())
stockfish.set_fen_position("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2")  #After 2 moves

move = stockfish.get_best_move()
print(move)

print(stockfish.get_fen_position())

print(stockfish.get_board_visual())