diff options
author | InigoGutierrez <inigogf.95@gmail.com> | 2021-06-12 10:58:21 +0200 |
---|---|---|
committer | InigoGutierrez <inigogf.95@gmail.com> | 2021-06-12 10:58:21 +0200 |
commit | 4a39a8fd07e49db5feb0c403b784423f0b673f97 (patch) | |
tree | ad53281fbd275b278445f0d28195040102943d39 /tests | |
parent | e8a3007e25c32ed8014b5e524849dfb38e9bef13 (diff) | |
download | imago-4a39a8fd07e49db5feb0c403b784423f0b673f97.tar.gz imago-4a39a8fd07e49db5feb0c403b784423f0b673f97.zip |
First MonteCarlo match simulations.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_gameBoard.py | 84 | ||||
-rw-r--r-- | tests/test_gameMove.py | 78 | ||||
-rw-r--r-- | tests/test_monteCarlo.py | 22 |
3 files changed, 180 insertions, 4 deletions
diff --git a/tests/test_gameBoard.py b/tests/test_gameBoard.py index 2fa1037..3a4d5a0 100644 --- a/tests/test_gameBoard.py +++ b/tests/test_gameBoard.py @@ -5,8 +5,6 @@ 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): @@ -14,10 +12,10 @@ class TestGameBoard(unittest.TestCase): def testGetGroupLiberties(self): """Test calculation of group liberties.""" - board = GameBoard(TEST_BOARD_SIZE) + board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) #Empty cell liberties - self.assertEqual(board.getGroupLiberties(0,0), {}) + self.assertEqual(board.getGroupLiberties(0,0), set()) self.assertEqual(board.getGroupLibertiesCount(0,0), 0) # Lone stone liberties @@ -26,5 +24,83 @@ class TestGameBoard(unittest.TestCase): {(2,3), (3,2), (4,3), (3,4)}) self.assertEqual(board.getGroupLibertiesCount(3,3), 4) + def testIsCellEye(self): + """Tests the isCellEye method.""" + board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) + + # Empty board is eye + self.assertEqual(Player.EMPTY, board.isCellEye(0, 0)) + self.assertEqual(Player.EMPTY, board.isCellEye(3, 3)) + self.assertEqual(Player.EMPTY, board.isCellEye(TEST_BOARD_SIZE-1, TEST_BOARD_SIZE-1)) + + # Board with 1 stone is eye + board.board[5][6] = Player.WHITE + self.assertEqual(Player.WHITE, board.isCellEye(3, 3)) + + # Board with 2 stones of different color is not eye + board.board[9][9] = Player.BLACK + self.assertEqual(Player.EMPTY, board.isCellEye(3, 3)) + + # Surrounded cell is eye + board.board[6][5] = Player.WHITE + board.board[6][7] = Player.WHITE + board.board[7][6] = Player.WHITE + + self.assertEqual(Player.WHITE, board.isCellEye(6, 6)) + + # Surrounded cell with 2 different colors is not eye + board.board[6][5] = Player.BLACK + self.assertEqual(Player.EMPTY, board.isCellEye(6, 6)) + + def testScore(self): + """Tests the score method.""" + board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) + + # Empty board has no score. + self.assertEqual((0, 0), board.score()) + + # Board with 1 black stone has totalVertices-1 points for black. + board.board[3][3] = Player.BLACK + self.assertEqual((TEST_BOARD_SIZE*TEST_BOARD_SIZE-1, 0), board.score()) + + # Board with 2 black stones has totalVertices-2 points for black. + board.board[5][5] = Player.BLACK + self.assertEqual((TEST_BOARD_SIZE*TEST_BOARD_SIZE-2, 0), board.score()) + + # Board with lone stones of different colors has no score. + board.board[7][7] = Player.WHITE + self.assertEqual((0, 0), board.score()) + + # Black group with surrounded territory. + board.board[2][3] = Player.BLACK + board.board[1][3] = Player.BLACK + board.board[0][3] = Player.BLACK + board.board[3][2] = Player.BLACK + board.board[3][1] = Player.BLACK + board.board[3][0] = Player.BLACK + self.assertEqual((9, 0), board.score()) + + # White group besides black group. + board.board[6][7] = Player.WHITE + board.board[5][7] = Player.WHITE + board.board[5][6] = Player.WHITE + board.board[5][5] = Player.WHITE + board.board[5][4] = Player.WHITE + board.board[5][3] = Player.WHITE + board.board[5][2] = Player.WHITE + board.board[5][1] = Player.WHITE + board.board[5][0] = Player.WHITE + board.board[8][7] = Player.WHITE + board.board[9][7] = Player.WHITE + board.board[9][6] = Player.WHITE + board.board[9][5] = Player.WHITE + board.board[9][4] = Player.WHITE + board.board[9][3] = Player.WHITE + board.board[9][2] = Player.WHITE + board.board[9][1] = Player.WHITE + board.board[9][0] = Player.WHITE + self.assertEqual((9, 21), board.score()) + + if __name__ == '__main__': unittest.main() diff --git a/tests/test_gameMove.py b/tests/test_gameMove.py new file mode 100644 index 0000000..6569c5b --- /dev/null +++ b/tests/test_gameMove.py @@ -0,0 +1,78 @@ +"""Tests for gameMove module.""" + +import unittest + +from imago.data.enums import Player +from imago.gameLogic.gameBoard import GameBoard +from imago.gameLogic.gameMove import GameMove + +TEST_BOARD_SIZE = 19 + +class TestGameMove(unittest.TestCase): + """Test gameMove module.""" + + def testAddMove(self): + """Test adding new moves to existing moves.""" + board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) + firstMove = GameMove(board) + + self.assertIsNone(firstMove.coords) + + secondMove = firstMove.addMove(1, 2) + + self.assertIsNone(firstMove.coords) + self.assertEqual(secondMove.coords[0], 1) + self.assertEqual(secondMove.coords[1], 2) + + thirdMove = secondMove.addMove(5, 7) + + self.assertIsNone(firstMove.coords) + self.assertIsNone(thirdMove.previousMove.previousMove.coords) + + self.assertEqual(secondMove.coords[0], 1) + self.assertEqual(secondMove.coords[1], 2) + self.assertEqual(thirdMove.previousMove.coords[0], 1) + self.assertEqual(thirdMove.previousMove.coords[1], 2) + + self.assertEqual(thirdMove.coords[0], 5) + self.assertEqual(thirdMove.coords[1], 7) + self.assertEqual(firstMove + .nextMoves[0] + .nextMoves[0] + .coords[0], 5) + self.assertEqual(firstMove + .nextMoves[0] + .nextMoves[0] + .coords[1], 7) + + self.assertEqual(firstMove.board.getBoard()[1][2], Player.EMPTY) + self.assertEqual(secondMove.board.getBoard()[1][2], Player.BLACK) + self.assertEqual(thirdMove.board.getBoard()[1][2], Player.BLACK) + + self.assertEqual(firstMove.board.getBoard()[5][7], Player.EMPTY) + self.assertEqual(secondMove.board.getBoard()[5][7], Player.EMPTY) + self.assertEqual(thirdMove.board.getBoard()[5][7], Player.WHITE) + + def testGetPlayableVertices(self): + """Test getting the set of valid moves.""" + boardSize = 3 + board = GameBoard(boardSize, boardSize) + + firstMove = GameMove(board) + self.assertSetEqual( + firstMove.getPlayableVertices(), + set(((0,0), (0,1), (0,2), + (1,0), (1,1), (1,2), + (2,0), (2,1), (2,2))) + ) + + secondMove = firstMove.addMove(1, 2) + self.assertSetEqual( + secondMove.getPlayableVertices(), + set(((0,0), (0,1), (0,2), + (1,0), (1,1), + (2,0), (2,1), (2,2))) + ) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_monteCarlo.py b/tests/test_monteCarlo.py new file mode 100644 index 0000000..8fe6f47 --- /dev/null +++ b/tests/test_monteCarlo.py @@ -0,0 +1,22 @@ +"""Tests for the MonteCarlo algorithm.""" + +import unittest + +from imago.gameLogic.gameBoard import GameBoard +from imago.gameLogic.gameMove import GameMove +from imago.engine.monteCarlo import MCTSNode + +TEST_BOARD_SIZE = 19 + +class TestMonteCarlo(unittest.TestCase): + """Test MonteCarlo algorithm.""" + + def testSimulation(self): + """Test calculation of group liberties.""" + board = GameBoard(TEST_BOARD_SIZE, TEST_BOARD_SIZE) + move = GameMove(board) + node = MCTSNode(move, None) + node.simulation() + +if __name__ == '__main__': + unittest.main() |