Feb-06-2021, 02:11 AM
Hello,
I am trying to write a chess program that grabs a random chess position, sends the position to an evaluation engine, and returns both the position and the evaluation.
I am using three libraries: random, which generates random numbers, stockfish, which is a Python API wrapper for the chess engine "stockfish," and chess, which displays a board and parses the human-readable "pgn" file format and translates it to the "fen" format readable by stockfish.
The issue I believe I am running in to is in the generator in the "move_picker" function below. I have tested the "game_picker" function on its own, and it works properly in isolation.
The documentation for the chess library is not good. This is all the relevant information they give:
I am trying to write a chess program that grabs a random chess position, sends the position to an evaluation engine, and returns both the position and the evaluation.
I am using three libraries: random, which generates random numbers, stockfish, which is a Python API wrapper for the chess engine "stockfish," and chess, which displays a board and parses the human-readable "pgn" file format and translates it to the "fen" format readable by stockfish.
The issue I believe I am running in to is in the generator in the "move_picker" function below. I have tested the "game_picker" function on its own, and it works properly in isolation.
import chess.pgn import random import stockfish from stockfish import Stockfish stockfish = Stockfish("/Users/User/stockfish_20011801_x64.exe") pgn = open("Capablanca.pgn") def game_picker(): i = 1 game_val = random.randint(2,550) the_game = chess.pgn.read_game(pgn) for i in range(game_val): the_game = chess.pgn.read_game(pgn) return the_game def move_picker(): Move_val = random.randint(10,40) board3 = the_game.board() moves_list = iter(the_game.mainline_moves()) i = 1 for i in range(Move_val): board3.push(next(moves_list)) fen_position = board3.fen() yield fen_position def run_the_game(): game_picker() move_picker() fen_position = move_picker() stockfish.set_fen_position(fen_position) eval = stockfish.get_evaluation() eval = eval["value"] print("Stockfish says... " + str(eval)) display(board3) run_the_game()When I run this code, it just hangs, and I get a Windows crash report.
The documentation for the chess library is not good. This is all the relevant information they give:
>>> import chess.pgn >>> >>> pgn = open("data/pgn/kasparov-deep-blue-1997.pgn") >>> >>> first_game = chess.pgn.read_game(pgn) >>> second_game = chess.pgn.read_game(pgn) >>> >>> first_game.headers["Event"] 'IBM Man-Machine, New York USA' >>> >>> # Iterate through all moves and play them on a board. >>> board = first_game.board() >>> for move in first_game.mainline_moves(): ... board.push(move)But I don't want to iterate through ALL moves, just through a random number of moves. So when I try it like this, mirroring the code from the documentation as much as possible, I also get an error.
import chess import chess.pgn import random import stockfish from stockfish import Stockfish stockfish = Stockfish("/Users/User/stockfish_20011801_x64.exe") pgn = open("Capablanca.pgn") def game_picker(): i = 1 game_val = random.randint(2,550) the_game = chess.pgn.read_game(pgn) for i in range(game_val): the_game = chess.pgn.read_game(pgn) return the_game def move_picker(): Move_val = random.randint(10,40) board3 = the_game.board() for Move_val in the_game.mainline_moves(): board3.push(move) return board3 def run_the_game(): game_picker() move_picker() fen_position = move_picker() fen_position = board3.fen() stockfish.set_fen_position(fen_position) eval = stockfish.get_evaluation() eval = eval["value"] print("Stockfish says... " + str(eval)) display(board3) run_the_game()
Error:---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-8c9bfbf62c3a> in <module>
51
52
---> 53 run_the_game()
54
<ipython-input-9-8c9bfbf62c3a> in run_the_game()
32 game_picker()
33 the_game = game_picker()
---> 34 move_picker()
35 fen_position = move_picker()
36 fen_position = board3.fen()
<ipython-input-9-8c9bfbf62c3a> in move_picker()
23 def move_picker():
24 Move_val = random.randint(10,40)
---> 25 board3 = the_game.board()
26 for Move_val in the_game.mainline_moves():
27 board3.push(move)
NameError: name 'the_game' is not defined
I'm at a loss and any help would be greatly appreciated.