-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess_tf.py
32 lines (30 loc) · 1.27 KB
/
chess_tf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import tensorflow as tf
import chess
import chess.pgn
import chess.engine
import numpy as np
import random
import os
import json
# Call stockfish from the command line to play games against a model
# Model has one convolution+pool layer so that it can take in an 8x8 matrix input
# Along with a flatten as well as two dense layers and
# Maps to a position and piece layer matrix that is then translated
# Into standard chess notation which stockfish then accepts as an input
def main():
#Call chess.engine to play many games against itself
#At each point save the board state and the best move predicted by stockfish
engine = chess.engine.SimpleEngine.popen_uci("stockfish")
for i in range(10):
board = chess.Board()
while not board.is_game_over():
#Get the best move from stockfish
info = engine.analyse(board, chess.engine.Limit(time=0.1))
board.push(info["move"])
#Save the board state and the best move by storing it in a json file
#Being sure to append to the current contents and don't overwrite
#And use the python json library
with open("data.json", "a") as f:
f.write(json.dumps({"board": board.fen(), "move": info["move"].uci()}) + "\n")
if __name__ == "__main__":
main()