aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInigoGutierrez <inigogf.95@gmail.com>2021-01-29 15:06:27 +0100
committerInigoGutierrez <inigogf.95@gmail.com>2021-01-29 15:06:27 +0100
commit65adea3cf601525c32279a5ae36a23c1cde6a387 (patch)
treeacc46020c4bbdadd135d718ae6286c313c53b8dc
parentddde2a9a43daf870c26bef33f47abe45b414c3d0 (diff)
downloadimago-65adea3cf601525c32279a5ae36a23c1cde6a387.tar.gz
imago-65adea3cf601525c32279a5ae36a23c1cde6a387.zip
Started implementing tests for gameBoard.
-rw-r--r--imago/gameLogic/gameBoard.py24
-rw-r--r--tests/test_gameBoard.py30
2 files changed, 44 insertions, 10 deletions
diff --git a/imago/gameLogic/gameBoard.py b/imago/gameLogic/gameBoard.py
index 265b646..bf6c9e4 100644
--- a/imago/gameLogic/gameBoard.py
+++ b/imago/gameLogic/gameBoard.py
@@ -4,7 +4,7 @@ from copy import deepcopy
from imago.data.enums import Player
-def getNewBoard(size):
+def _getNewBoard(size):
"""Return a new board."""
board = []
for row in range(size):
@@ -14,9 +14,10 @@ def getNewBoard(size):
return board
class GameBoard:
+ """Logic and state related to the board."""
def __init__(self, size):
- self.board = getNewBoard(size)
+ self.board = _getNewBoard(size)
self.capturesBlack = 0
self.capturesWhite = 0
self.lastStone = None
@@ -30,13 +31,16 @@ class GameBoard:
newBoard.board = deepcopy(self.board)
return newBoard
+ def getGroupLibertiesCount(self, row, col):
+ return len(self.getGroupLiberties(row, col))
+
def getGroupLiberties(self, row, col):
- """Returns the empty vertexes adjacent to the group occupying a cell (its
- liberties) or -1 if the cell is empty.
+ """Returns the empty vertexes adjacent to the group occupying a vertex (its
+ liberties) as a set. An empty set is returned if the vertex is empty.
"""
groupColor = self.board[row][col]
if groupColor == Player.EMPTY:
- return -1
+ return {}
emptyCells = set()
exploredCells = set()
self.__exploreLiberties(row, col, groupColor, emptyCells, exploredCells)
@@ -114,25 +118,25 @@ class GameBoard:
if (self.board[row-1][col] != player
and self.board[row-1][col] != Player.EMPTY
and len(self.getGroupLiberties(row-1, col)) == 0):
- captured += self.captureGroup(row-1, col)
+ captured += self.__captureGroup(row-1, col)
if row < len(self.board)-1:
if (self.board[row+1][col] != player
and self.board[row+1][col] != Player.EMPTY
and len(self.getGroupLiberties(row+1, col)) == 0):
- captured += self.captureGroup(row+1, col)
+ captured += self.__captureGroup(row+1, col)
if col > 0:
if (self.board[row][col-1] != player
and self.board[row][col-1] != Player.EMPTY
and len(self.getGroupLiberties(row, col-1)) == 0):
- captured += self.captureGroup(row, col-1)
+ captured += self.__captureGroup(row, col-1)
if col < len(self.board[0])-1:
if (self.board[row][col+1] != player
and self.board[row][col+1] != Player.EMPTY
and len(self.getGroupLiberties(row, col+1)) == 0):
- captured += self.captureGroup(row, col+1)
+ captured += self.__captureGroup(row, col+1)
return captured
- def captureGroup(self, row, col):
+ def __captureGroup(self, row, col):
"""Removes all the stones from the group occupying the given cell and returns the
number of removed stones.
"""
diff --git a/tests/test_gameBoard.py b/tests/test_gameBoard.py
new file mode 100644
index 0000000..2fa1037
--- /dev/null
+++ b/tests/test_gameBoard.py
@@ -0,0 +1,30 @@
+"""Tests for gameBoard module."""
+
+import unittest
+
+from imago.data.enums import Player
+from imago.gameLogic.gameBoard import GameBoard
+
+#from imago.data.enums import Player
+
+TEST_BOARD_SIZE = 19
+
+class TestGameBoard(unittest.TestCase):
+ """Test gameBoard module."""
+
+ def testGetGroupLiberties(self):
+ """Test calculation of group liberties."""
+ board = GameBoard(TEST_BOARD_SIZE)
+
+ #Empty cell liberties
+ self.assertEqual(board.getGroupLiberties(0,0), {})
+ self.assertEqual(board.getGroupLibertiesCount(0,0), 0)
+
+ # Lone stone liberties
+ board.board[3][3] = Player.WHITE
+ self.assertEqual(board.getGroupLiberties(3,3),
+ {(2,3), (3,2), (4,3), (3,4)})
+ self.assertEqual(board.getGroupLibertiesCount(3,3), 4)
+
+if __name__ == '__main__':
+ unittest.main()