Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with an iterator
#10
(Feb-06-2021, 04:44 PM)deanhystad Wrote: This should work. It uses list() to replace the code that built the moves_list.
def move_picker(game):
    board = game.board()
    num_moves = random.randint(5,50)
    for move in list(game.mainline_moves())[:num_moves]:
        board.push(move)

    sf.set_fen_position(board.fen())
    print(game.headers["White"] + ' vs ' + game.headers["Black"])
    print(game.headers["Event"])
    display(board)
Avoid using "global". It looks like the purpose of the global variable fen_position was to return my_board.fen to run_the_game() which did this "sf.set_fen_position(fen_position)". Does it make more sense to move this code from run_the_game() to move_picker()?

The problem with global variables is it isn't apparent where they are used. The only way to learn this information is search for the name, and since there can be local variables with the same name, this can be confusing. It is far better to return the needed information.

This is an example of returning the pieces required to do the bookkeeping. I like that each function has a well defined roll. Pick_game() randomly selects a game to play. It does not play the game. It does not cause the game to be played. It just does what the name implies. Play_game() plays the game. It doesn't pick the game. It doesn't display results. it doesn't update collect information that it doesn't use. Run_the_game() is the organizer and bookkeeper. It delegates selecting and playing the game.
import chess
import chess.pgn
from random import randint
from stockfish import Stockfish
   
sf = Stockfish("/Users/User/stockfish_20011801_x64.exe")

def pick_game(pgn):
    """Play a randomly selected game"""
    game = chess.pgn.read_game(pgn)
    for _ in range(randint(2,550)):
        game = chess.pgn.read_game(pgn)
    return game

def play_game(game):
    """Play randomly selected number of moves of game"""
    board = game.board()
    num_moves = randint(5,50)
    for move in list(game.mainline_moves())[:num_moves]:
        board.push(move)

def run_the_game(game_file):
    with open(game_file) as pgn:
        game = pick_game(pgn)
        play_game(game)
    
    print(game.headers["White"] + ' vs ' + game.headers["Black"])
    print(game.headers["Event"])
    display(game.board())
     
    sf.set_fen_position(game.board().fen())
    position_evaluation = sf.get_evaluation()
    position_evaluation = position_evaluation["value"]
    print("Stockfish says...  " + str(position_evaluation))     
       
run_the_game("Capablanca.pgn")

Point well taken about global variables. I need to learn more about namespaces, scope, and passing variables between functions. I have been doing some reading on this today, so again thank you.

The code as you have it does not work fully. It is only returning the first move of any given game, so there is some issue here...

    for move in list(game.mainline_moves())[:num_moves]:
        board.push(move)
which I think has something to do with the mainline_moves method from the chess library. For some reason I do not understand, when I create a new list and append each move from the mainline moves output, and iterate through that list, then it works fine. But if I try to do like you're doing here and iterate through it directly and feed the output to 'board.push' then it does not function properly.

I have yet to understand that. I do see that '__ITER__' does not appear in the 'dir' for 'mainline_moves' however, but so then why does it work to iterate through it and create a new list? Who knows.

Thank you again
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,876 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  resetting an iterator to full Skaperen 7 7,006 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  popping an iterator Skaperen 11 3,721 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  q re glob.iglob iterator and close jimr 2 2,241 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Multi-class iterator Pedroski55 2 2,397 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  is a str object a valid iterator? Skaperen 6 5,652 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  discard one from an iterator Skaperen 1 1,997 Dec-29-2019, 11:02 PM
Last Post: ichabod801
  how do i pass duplicates in my range iterator? pseudo 3 2,365 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  looking for a sprcil iterator Skaperen 7 3,370 Jun-13-2019, 01:40 AM
Last Post: Clunk_Head
  last pass of for x in iterator: Skaperen 13 5,897 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