Comments (7)
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.
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.
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.
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.
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.
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.
Fixed in sente v0.4.2
from sente.
Related Issues (9)
- generated sgf File did not perfectly match SGF format HOT 4
- sgf replay can't get game.get_winner() HOT 4
- game.get_results() like on sgf file HOT 4
- board display option HOT 2
- an example code how to make it a game human vs comp or human vs human HOT 2
- Require Sabaki to jump to a certain node HOT 4
- [sgf.load] How to handle games with handicap HOT 5
- pip installation not working in sente 0.4.3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sente.