aboutsummaryrefslogtreecommitdiff
path: root/imago/engine/imagoIO.py
diff options
context:
space:
mode:
Diffstat (limited to 'imago/engine/imagoIO.py')
-rw-r--r--imago/engine/imagoIO.py108
1 files changed, 57 insertions, 51 deletions
diff --git a/imago/engine/imagoIO.py b/imago/engine/imagoIO.py
index 9a89095..c983949 100644
--- a/imago/engine/imagoIO.py
+++ b/imago/engine/imagoIO.py
@@ -6,30 +6,6 @@ from imago.engine import parseHelpers
from imago.engine.core import GameEngine
-def _response(text=""):
- print("= %s" % text)
- print()
-
-
-def _responseError(text=""):
- print("? %s" % text)
- print()
-
-
-def protocol_version(_):
- """Version of the GTP Protocol"""
- _response("2")
-
-
-def name(_):
- """Name of the engine"""
- _response("Imago")
-
-
-def version(_):
- """Version of the engine"""
- _response("0.0.0")
-
def getCoordsText(row, col):
"""Returns a string representation of row and col.
@@ -41,11 +17,11 @@ def getCoordsText(row, col):
class ImagoIO:
"""Recieves and handles commands."""
- def __init__(self, decisionAlgorithmId=None):
+ def __init__(self, decisionAlgorithmId=None, outputStream=sys.stdin):
self.commands_set = {
- protocol_version,
- name,
- version,
+ self.protocol_version,
+ self.name,
+ self.version,
self.known_command,
self.list_commands,
self.boardsize,
@@ -59,6 +35,17 @@ class ImagoIO:
self.undo
}
self.gameEngine = GameEngine(decisionAlgorithmId)
+ self.outputStream = outputStream
+
+ def _response(self, text=""):
+ print("= %s" % text, file=self.outputStream)
+ print(file=self.outputStream)
+
+
+ def _responseError(self, text=""):
+ print("? %s" % text, file=self.outputStream)
+ print(file=self.outputStream)
+
def start(self):
"""Starts reading commands interactively."""
@@ -70,7 +57,8 @@ class ImagoIO:
continue
if input_tokens[0] == "quit":
- sys.exit(0)
+ #sys.exit(0)
+ break
command = None
for comm in self.commands_set:
@@ -81,28 +69,46 @@ class ImagoIO:
arguments = input_tokens[1:]
command(arguments)
else:
- _responseError("unknown command")
+ self._responseError("unknown command")
except Exception as err:
- _responseError("An uncontrolled error ocurred. The error was: %s" % err)
+ self._responseError("An uncontrolled error ocurred. The error was: %s" % err)
+
def known_command(self, args):
"""True if command is known, false otherwise"""
if len(args) != 1:
- _responseError("Wrong number of arguments.")
- _responseError("Usage: known_command COMMAND_NAME")
+ self._responseError("Wrong number of arguments.")
+ self._responseError("Usage: known_command COMMAND_NAME")
return
out = "false"
for c in self.commands_set:
if c.__name__ == args[0]:
out = "true"
- _response(out)
+ self._response(out)
+
def list_commands(self, _):
"""List of commands, one per row"""
output = ""
for c in self.commands_set:
output += ("%s - %s\n" % (c.__name__, c.__doc__))
- _response(output)
+ self._response(output)
+
+
+ def protocol_version(self, _):
+ """Version of the GTP Protocol"""
+ self._response("2")
+
+
+ def name(self, _):
+ """Name of the engine"""
+ self._response("Imago")
+
+
+ def version(self, _):
+ """Version of the engine"""
+ self._response("0.0.0")
+
def boardsize(self, args):
"""Changes the size of the board.
@@ -110,44 +116,44 @@ class ImagoIO:
It is wise to call clear_board after this command.
"""
if len(args) != 1:
- _responseError("Wrong number of arguments")
- _responseError("Usag. boardsize <newSize>")
+ self._responseError("Wrong number of arguments")
+ self._responseError("Usag. boardsize <newSize>")
return
size = int(args[0])
self.gameEngine.setBoardsize(size)
- _response()
+ self._response()
def clear_board(self, _):
"""The board is cleared, the number of captured stones reset to zero and the move
history reset to empty.
"""
self.gameEngine.clearBoard()
- _response()
+ self._response()
def komi(self, args):
"""Sets a new value of komi."""
if len(args) != 1:
- _responseError("Wrong number of arguments")
- _responseError("Usage: komi <newKomi>")
+ self._responseError("Wrong number of arguments")
+ self._responseError("Usage: komi <newKomi>")
return
komi = float(args[0])
self.gameEngine.setKomi(komi)
- _response()
+ self._response()
def fixed_handicap(self, args):
"""Handicap stones are placed on the board on standard vertices.
These vertices follow the GTP specification.
"""
if len(args) != 1:
- _responseError("Wrong number of arguments")
- _responseError("Usage: fixed_handicap <count>")
+ self._responseError("Wrong number of arguments")
+ self._responseError("Usage: fixed_handicap <count>")
return
stones = float(args[0])
vertices = self.gameEngine.setFixedHandicap(stones)
out = getCoordsText(vertices[0][0], vertices[0][1])
for vertex in vertices[1:]:
out += " " + getCoordsText(vertex[0], vertex[1])
- _response(out)
+ self._response(out)
def place_free_handicap(self, args):
"""Handicap stones are placed on the board by the AI criteria."""
@@ -160,23 +166,23 @@ class ImagoIO:
def play(self, args):
"""A stone of the requested color is played at the requested vertex."""
if len(args) != 2:
- _responseError("Wrong number of arguments.")
- _responseError("Usage: play <color> <vertex>")
+ self._responseError("Wrong number of arguments.")
+ self._responseError("Usage: play <color> <vertex>")
return
move = parseHelpers.parseMove(args, self.gameEngine.gameState.size)
self.gameEngine.play(move.color, move.vertex)
- _response()
+ self._response()
def genmove(self, args):
"""A stone of the requested color is played where the engine chooses."""
if len(args) != 1:
- _responseError("Wrong number of arguments.")
- _responseError("Usage: genmove <color>")
+ self._responseError("Wrong number of arguments.")
+ self._responseError("Usage: genmove <color>")
return
color = parseHelpers.parseColor(args[0])
output = parseHelpers.vertexToString(self.gameEngine.genmove(color),
self.gameEngine.gameState.size)
- _response(output)
+ self._response(output)
self.gameEngine.gameState.getBoard().printBoard()
def undo(self, _):