diff options
-rwxr-xr-x | go.py | 9 | ||||
-rw-r--r-- | imago/engine/core.py | 2 | ||||
-rw-r--r-- | imago/engine/parseHelpers.py | 19 | ||||
-rw-r--r-- | imago/gameLogic/gameBoard.py | 6 | ||||
-rw-r--r-- | tests/test_enums.py | 15 | ||||
-rw-r--r-- | tests/test_gameBoard.py | 20 | ||||
-rw-r--r-- | tests/test_parseHelpers.py | 63 |
7 files changed, 82 insertions, 52 deletions
@@ -2,7 +2,7 @@ """Play Go!""" -from imago.engine.parseHelpers import ParseCodes, parseVertex +from imago.engine.parseHelpers import parseVertex from imago.gameLogic.gameState import GameState if __name__ == "__main__": @@ -10,14 +10,15 @@ if __name__ == "__main__": GAMESTATE = GameState(5) #GAMESTATE = GameState(19) - while 1: + while True: GAMESTATE.getBoard().printBoard() move = input("Move (" + GAMESTATE.getPlayerCode() + "): ") - move = parseVertex(move, GAMESTATE.size) - if move == ParseCodes.ERROR: + try: + move = parseVertex(move, GAMESTATE.size) + except Exception as err: print("Invalid move syntax. Example of move: A1") continue diff --git a/imago/engine/core.py b/imago/engine/core.py index 4028bb3..4e383d4 100644 --- a/imago/engine/core.py +++ b/imago/engine/core.py @@ -3,7 +3,7 @@ from imago.engine.decisionAlgorithmFactory import DecisionAlgorithms, DecisionAlgorithmFactory from imago.gameLogic.gameState import GameState -DEF_SIZE = 7 +DEF_SIZE = 9 DEF_KOMI = 5.5 DEF_ALGORITHM = DecisionAlgorithms.KERAS diff --git a/imago/engine/parseHelpers.py b/imago/engine/parseHelpers.py index fd5ea63..fc42845 100644 --- a/imago/engine/parseHelpers.py +++ b/imago/engine/parseHelpers.py @@ -15,29 +15,24 @@ VALID_BLACK_STRINGS = { "BLACK" } -class ParseCodes(Enum): - """Return values of the move parser.""" - ERROR = enumAuto() - QUIT = enumAuto() - def parseMove(args, boardsize): """Converts the textual representation of a move to a move instance.""" if len(args) != 2: - print("[ERROR] - Wrong n of args for move") - return ParseCodes.ERROR + raise RuntimeError( + "Unable to transform string %s to move: Wrong format." + % args) color = parseColor(args[0]) vertex = parseVertex(args[1], boardsize) return GtpMove(color, vertex) def parseColor(text): """Returns color of a move given its input string.""" - text = text.upper() - if text in VALID_WHITE_STRINGS: + textUpper = text.upper() + if textUpper in VALID_WHITE_STRINGS: return Player.WHITE - if text in VALID_BLACK_STRINGS: + if textUpper in VALID_BLACK_STRINGS: return Player.BLACK - print("[ERROR] - Unknown color.") - return ParseCodes.ERROR + raise RuntimeError("Unknown color %s." % text) def parseVertex(text, boardSize): """Returns row and column of a vertex given its input string. A vertex can also be the diff --git a/imago/gameLogic/gameBoard.py b/imago/gameLogic/gameBoard.py index 9054a18..611c4cb 100644 --- a/imago/gameLogic/gameBoard.py +++ b/imago/gameLogic/gameBoard.py @@ -51,14 +51,14 @@ class GameBoard: return set() emptyCells = set() exploredCells = set() - self.__exploreLiberties(row, col, groupColor, emptyCells, exploredCells) + self._exploreLiberties(row, col, groupColor, emptyCells, exploredCells) return emptyCells def getGroupLibertiesCount(self, row, col): """Returns the number of liberties of a group.""" return len(self.getGroupLiberties(row, col)) - def __exploreLiberties(self, row, col, groupColor, emptyCells, exploredCells): + def _exploreLiberties(self, row, col, groupColor, emptyCells, exploredCells): """Adds surrounding empty cells to array if they have not been added yet and explores adjacent occupied cells of the same group. """ @@ -78,7 +78,7 @@ class GameBoard: rowToExplore = row + side[0] colToExplore = col + side[1] if self.isMoveInBoardBounds(rowToExplore, colToExplore): - self.__exploreLiberties(rowToExplore, colToExplore, groupColor, + self._exploreLiberties(rowToExplore, colToExplore, groupColor, emptyCells, exploredCells) def getGroupVertices(self, row, col): diff --git a/tests/test_enums.py b/tests/test_enums.py new file mode 100644 index 0000000..73f4009 --- /dev/null +++ b/tests/test_enums.py @@ -0,0 +1,15 @@ +"""Tests for enums module.""" + +import unittest + +from imago.data.enums import Player + +class TestEnums(unittest.TestCase): + """Test parseHelpers module.""" + + def testOtherPlayer(self): + """Test method to get the other player""" + + self.assertEqual(Player.otherPlayer(Player.BLACK), Player.WHITE) + self.assertEqual(Player.otherPlayer(Player.WHITE), Player.BLACK) + self.assertEqual(Player.otherPlayer(''), Player.EMPTY) diff --git a/tests/test_gameBoard.py b/tests/test_gameBoard.py index 3a4d5a0..8a7b127 100644 --- a/tests/test_gameBoard.py +++ b/tests/test_gameBoard.py @@ -10,20 +10,33 @@ TEST_BOARD_SIZE = 19 class TestGameBoard(unittest.TestCase): """Test gameBoard module.""" - def testGetGroupLiberties(self): - """Test calculation of group liberties.""" + def testGetGroupCounts(self): + """Test calculation of group stones and liberties.""" board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) #Empty cell liberties self.assertEqual(board.getGroupLiberties(0,0), set()) self.assertEqual(board.getGroupLibertiesCount(0,0), 0) - # Lone stone liberties + # Lone stone board.board[3][3] = Player.WHITE + self.assertEqual(board.getGroupVertices(3,3), + {(3,3)}) + self.assertEqual(board.getGroupVerticesCount(3,3), 1) self.assertEqual(board.getGroupLiberties(3,3), {(2,3), (3,2), (4,3), (3,4)}) self.assertEqual(board.getGroupLibertiesCount(3,3), 4) + # L group (don't compute twice liberty inside L) + board.board[3][4] = Player.WHITE + board.board[2][4] = Player.WHITE + self.assertEqual(board.getGroupVertices(3,3), + {(3,3), (3,4), (2,4)}) + self.assertEqual(board.getGroupVerticesCount(3,3), 3) + self.assertEqual(board.getGroupLiberties(3,3), + {(2,3), (3,2), (4,3), (4, 4), (3,5), (2,5), (1,4)}) + self.assertEqual(board.getGroupLibertiesCount(3,3), 7) + def testIsCellEye(self): """Tests the isCellEye method.""" board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) @@ -101,6 +114,5 @@ class TestGameBoard(unittest.TestCase): board.board[9][0] = Player.WHITE self.assertEqual((9, 21), board.score()) - if __name__ == '__main__': unittest.main() diff --git a/tests/test_parseHelpers.py b/tests/test_parseHelpers.py index d03f253..7bbf152 100644 --- a/tests/test_parseHelpers.py +++ b/tests/test_parseHelpers.py @@ -13,16 +13,19 @@ class TestParseHelpers(unittest.TestCase): def testParseMove(self): """Test parsing of GtpMove""" - self.assertEqual(parseHelpers.parseMove(["B"], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.parseMove(["A1"], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.parseMove(["B", "A1", "W"], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - + wrongMoves = [ + ["B"], + ["A1"], + ["B", "A1", "W"] + ] + for move in wrongMoves: + self.assertRaises( + RuntimeError, + parseHelpers.parseMove, + move, TEST_BOARD_SIZE + ) parsedMove = parseHelpers.parseMove(["B", "t1"], TEST_BOARD_SIZE) - targetMove = parseHelpers.GtpMove(Player.BLACK, [18, 18]) self.assertEqual(parsedMove.color, targetMove.color) self.assertEqual(parsedMove.vertex, targetMove.vertex) @@ -33,15 +36,19 @@ class TestParseHelpers(unittest.TestCase): self.assertEqual(parseHelpers.parseColor("B"), Player.BLACK) self.assertEqual(parseHelpers.parseColor("w"), Player.WHITE) self.assertEqual(parseHelpers.parseColor("W"), Player.WHITE) - self.assertEqual(parseHelpers.parseColor("bw"), parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.parseColor("wb"), parseHelpers.ParseCodes.ERROR) + + self.assertRaises(RuntimeError, parseHelpers.parseColor, "bw") + self.assertRaises(RuntimeError, parseHelpers.parseColor, "wb") def testParseVertexWrongInputs(self): """Test wrong inputs for parseVertex.""" inputs = ( "a", "1", "1a", "aa1", "a0", "u1", "a"+str(TEST_BOARD_SIZE+1) ) for text in inputs: - self.assertEqual(parseHelpers.parseVertex(text, TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) + self.assertRaises( + RuntimeError, + parseHelpers.parseVertex, + text, TEST_BOARD_SIZE + ) def testParseVertexCorrectInputs(self): """Test correct inputs and their resulting coordinates for parseVertex.""" @@ -85,22 +92,22 @@ class TestParseHelpers(unittest.TestCase): self.assertEqual(parseHelpers.vertexToString("pass", TEST_BOARD_SIZE), "pass") - self.assertEqual(parseHelpers.vertexToString([-1,0], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([0,-1], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([-1,-1], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([19,0], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([0,19], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([19,19], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([0], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) - self.assertEqual(parseHelpers.vertexToString([0,0,0], TEST_BOARD_SIZE), - parseHelpers.ParseCodes.ERROR) + wrongVertices = [ + [-1,0], + [0,-1], + [-1,-1], + [19,0], + [0,19], + [19,19], + [0], + [0,0,0] + ] + for vertex in wrongVertices: + self.assertRaises( + RuntimeError, + parseHelpers.vertexToString, + vertex, TEST_BOARD_SIZE + ) if __name__ == '__main__': unittest.main() |