Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with an iterator
#1
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.



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.
Reply


Messages In This Thread
Problem with an iterator - by grimm1111 - Feb-06-2021, 02:11 AM
RE: Problem with an iterator - by Larz60+ - Feb-06-2021, 03:28 AM
RE: Problem with an iterator - by grimm1111 - Feb-06-2021, 03:45 AM
RE: Problem with an iterator - by deanhystad - Feb-06-2021, 05:00 AM
RE: Problem with an iterator - by grimm1111 - Feb-06-2021, 05:45 AM
RE: Problem with an iterator - by deanhystad - Feb-06-2021, 05:14 AM
RE: Problem with an iterator - by deanhystad - Feb-06-2021, 05:57 AM
RE: Problem with an iterator - by grimm1111 - Feb-06-2021, 08:28 AM
RE: Problem with an iterator - by deanhystad - Feb-06-2021, 04:44 PM
RE: Problem with an iterator - by grimm1111 - Feb-06-2021, 09:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,953 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  resetting an iterator to full Skaperen 7 7,099 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  popping an iterator Skaperen 11 3,794 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  q re glob.iglob iterator and close jimr 2 2,274 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Multi-class iterator Pedroski55 2 2,419 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  is a str object a valid iterator? Skaperen 6 5,709 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  discard one from an iterator Skaperen 1 2,023 Dec-29-2019, 11:02 PM
Last Post: ichabod801
  how do i pass duplicates in my range iterator? pseudo 3 2,401 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  looking for a sprcil iterator Skaperen 7 3,412 Jun-13-2019, 01:40 AM
Last Post: Clunk_Head
  last pass of for x in iterator: Skaperen 13 5,959 May-20-2019, 10:05 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020