-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
65 lines (50 loc) · 2.25 KB
/
generate.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import chess #https://python-chess.readthedocs.io/en/latest/
from stockfish import Stockfish #https://pypi.org/project/stockfish/
import random
import json
output_file = open("output.txt", "a")
#must download stockfish and point to stockfish executeable on your PC
stockfish = Stockfish("/usr/local/Cellar/stockfish/12/bin/stockfish")
games_to_gen = 100
games_genned = 0
while games_genned < games_to_gen:
board = chess.Board()
moves_to_do = random.randrange(30,50)
moves_done = 0
while moves_done < moves_to_do and board.is_game_over() == False:
list_of_possible_moves = []
for move in board.legal_moves:
list_of_possible_moves.append(move)
move = random.choice(list_of_possible_moves)
board.push(move)
moves_done += 1
# this is our randomized fen!
rand_fen = board.fen()
stockfish.set_fen_position(rand_fen)
legal_move_count = board.legal_moves.count()
# If there is atleast two moves and the worst move is the only one that causes mate,
# then its a good puzzle!
if legal_move_count > 1:
evaled_moves = stockfish.get_top_moves(legal_move_count)
if evaled_moves[-1]["Mate"] != None:
if evaled_moves[-2]["Mate"] == None:
print(rand_fen)
print(evaled_moves)
puzzle = {
"fen": rand_fen,
"mate_in": str(evaled_moves[-1]["Mate"]),
"answer": evaled_moves[-1]["Move"]
}
moves = []
board.push( chess.Move.from_uci(puzzle["answer"]) )
while board.is_game_over() == False:
stockfish.set_fen_position(board.fen())
best_move = stockfish.get_best_move()
board.push( chess.Move.from_uci( best_move ) )
moves.append(best_move)
puzzle["mate_moves"] = " ".join(moves)
if ( abs(int(puzzle["mate_in"])) != (len(moves)+1)/2 ):
continue
games_genned+=1
output_file.write( str( json.dumps(puzzle) ) + "\n")
output_file.close()