Skip to content

Commit

Permalink
Use namedtuples for bestmove
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Mar 3, 2015
1 parent 0d3d7c3 commit 5c5bcdd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Up for the next release
The `result` property and `wait()` have been removed in favor of a
synchronously waiting `result()` method.

* The result of the `stop` and `go` UCI commands are now named tuples (instead
of just normal tuples).

New in v0.7.0
-------------

Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,13 @@ Features
>>> board = chess.Bitboard("1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - 0 1")
>>> engine.position(board)
>>> engine.go(movetime=2000) # Gets tuple of bestmove and pondermove.
(Move.from_uci('d6d1'), Move.from_uci('c1d1'))
Bestmove(bestmove=Move.from_uci('d6d1'), pondermove=Move.from_uci('c1d1'))
>>> # Synchronous communication, but search in background.
>>> engine.go(infinite=True)
>>> time.sleep(2)
>>> engine.stop()
(Move.from_uci('d6d1'), Move.from_uci('c1d1'))
Bestmove(bestmove=Move.from_uci('d6d1'), pondermove=Move.from_uci('c1d1'))
>>> # Asynchronous mode.
>>> def callback(command):
Expand All @@ -246,7 +246,7 @@ Features
>>> command.done()
False
>>> command.result()
(Move.from_uci('d6d1'), Move.from_uci('c1d1'))
Bestmove(bestmove=Move.from_uci('d6d1'), pondermove=Move.from_uci('c1d1'))
>>> command.done()
True
Expand Down
9 changes: 7 additions & 2 deletions chess/uci.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Score(collections.namedtuple("Score", ["cp", "mate", "lowerbound", "upperb
pass


class Bestmove(collections.namedtuple("Bestmove", ["bestmove", "pondermove"])):
"""A bestmove and pondermove sent by an UCI engine."""
pass


class OptionMap(collections.MutableMapping):
def __init__(self, data=None, **kwargs):
self._store = dict()
Expand Down Expand Up @@ -494,7 +499,7 @@ def execute(self, engine):
self.set_result(None)
else:
engine.bestmove_received.wait()
self.set_result((engine.bestmove, engine.ponder))
self.set_result(Bestmove(engine.bestmove, engine.ponder))


class StopCommand(Command):
Expand All @@ -511,7 +516,7 @@ def execute(self, engine):
engine.readyok.wait()

engine.bestmove_received.wait(STOP_TIMEOUT)
self.set_result((engine.bestmove, engine.ponder))
self.set_result(Bestmove(engine.bestmove, engine.ponder))


class PonderhitCommand(IsReadyCommand):
Expand Down
4 changes: 2 additions & 2 deletions docs/uci.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ By default all operations are executed synchronously and their result is
returned. For example

>>> engine.go(movetime=2000)
(Move.from_uci('e2e4'), None)
Bestmove(bestmove=Move.from_uci('e2e4'), pondermove=None)

will take about 2000 milliseconds. All UCI commands have an optional
*async_callback* argument. They will then immediately return information about
Expand All @@ -67,7 +67,7 @@ the command and continue.
>>> command.done()
False
>>> command.result() # Synchronously wait for the command to finish
(Move.from_uci('e2e4'), None)
Bestmove(bestmove=Move.from_uci('e2e4'), pondermove=None)
>>> command.done()
True

Expand Down

0 comments on commit 5c5bcdd

Please sign in to comment.