Giter Site home page Giter Site logo

Comments (7)

atw1020 avatar atw1020 commented on May 23, 2024

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]

from sente.

sugizo avatar sugizo commented on May 23, 2024

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

from sente.

atw1020 avatar atw1020 commented on May 23, 2024

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.

from sente.

atw1020 avatar atw1020 commented on May 23, 2024

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))

from sente.

sugizo avatar sugizo commented on May 23, 2024

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() )

from sente.

atw1020 avatar atw1020 commented on May 23, 2024

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.

from sente.

atw1020 avatar atw1020 commented on May 23, 2024

Fixed in sente v0.4.2

from sente.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.