Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

game.get_sequence() result like on sgf file #28

Closed
sugizo opened this issue May 20, 2022 · 7 comments
Closed

game.get_sequence() result like on sgf file #28

sugizo opened this issue May 20, 2022 · 7 comments

Comments

@sugizo
Copy link

sugizo commented May 20, 2022

game.play(4, 4)
game.play(4, 15)
game.play(15, 15)
print(game.get_sequence())

result

[<sente.Move B[dd]>, <sente.Move W[do]>, <sente.Move B[oo]>]

expected result like on sgf file, more human readable

B[dd];W[do];B[oo]

question
how to achieve it in sente ?

thanks and best regards,

@atw1020
Copy link
Owner

atw1020 commented May 20, 2022

the functionality you are looking for would come from using loads and dumps ("load string" and "dump string"). As per the documentation, sente.get_sequence() returns a python list of move objects. This list of moves can be sliced, reversed or passed through any other sort of list manipulation. A list of moves can be played using the play_sequence() method.

>>> sequence = game.get_sequence()
>>> first_five_moves = sequence[:5]
>>> last_2_moves = sequence[-2:]
>>> reversed = sequence[::-1]

@sugizo
Copy link
Author

sugizo commented May 20, 2022

the code on initial issue is the shortest one, here what i want to achieve

def random_player(game):
    move = random.choice(list(game.get_legal_moves() ) )
    return move

def play_game(player1, player2):
    game = sente.Game(9, sente.rules.JAPANESE)
    try:
        while not game.is_over():
            move = random_player(game)
            game.play(move)
            clear_output(wait = True)
            print(game)
    except KeyboardInterrupt:
        msg = "Game interrupted!"
        return (None, msg, game)
    file_sgf = "sgf/sente-random_player.sgf"
    sgf.dump(game, file_sgf)
    print('Winner :', game.get_winner() )
    print('Game Sequence :', game. get_sequence() )

it works but not expected,
expected result on the last line, make it more human readable like on sgf file

best regards

@atw1020
Copy link
Owner

atw1020 commented May 21, 2022

What you want to do is use sgf.dumps(game) instead of game.get_sequence(). dumps will give you the plain text of the SGF file.

@atw1020
Copy link
Owner

atw1020 commented May 21, 2022

Full code with the change

def random_player(game):
    move = random.choice(list(game.get_legal_moves() ) )
    return move

def play_game(player1, player2):
    game = sente.Game(9, sente.rules.JAPANESE)
    try:
        while not game.is_over():
            move = random_player(game)
            game.play(move)
            clear_output(wait = True)
            print(game)
    except KeyboardInterrupt:
        msg = "Game interrupted!"
        return (None, msg, game)
    file_sgf = "sgf/sente-random_player.sgf"
    sgf.dump(game, file_sgf)
    print('Winner :', game.get_winner() )
    print('Game Sequence :', sgf.dumps(game))

@sugizo
Copy link
Author

sugizo commented May 21, 2022

already tested before
print('Game Sequence :', sgf.dumps(game))

result
the content of *.sgf file

expected result
just the sequence, like mention on initial issue above
B[dd];W[do];B[oo]

think like *.pgn on chess, sometime others want to learn the sequence move of players,
the headers info (size of board [9, 13, 19], rules (jp, zh, kr), game result, winner (black or white) ) is needed as an extra info

in python chess module https://pypi.org/project/chess/, it can be achieve using
print(game.mainline_moves() )

@atw1020
Copy link
Owner

atw1020 commented May 21, 2022

I think this is what you should do as a workaround then:

def move_str(move):
	"""

	translates a move into a string

	:param move: move to translate into a string
	:returns: string representation of the move
	"""

	color = move.get_color()
	x = move.get_x()
	y = move.get_y()

	if x >= ord("i"):
		x += 1
	if y >= ord("i"):
		y += 1

	return color + "[" + chr(x) + chr(y) + "]"

you can then apply the function to the entire list and join the new list with semicolons.

>>> sequence = game.get_sequence()
>>> sequence = [move_to_str(move) for move in sequence]
>>> print(";".join(sequence))

When I get the chance I will make it so that calling str on a move will implement the function listed above. This should work in the meantime.

@atw1020
Copy link
Owner

atw1020 commented May 24, 2022

Fixed in sente v0.4.2

@atw1020 atw1020 closed this as completed May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants