aboutsummaryrefslogtreecommitdiff
path: root/tests/test_gameBoard.py
blob: c7808ac410bdbfb7c9c9cf41d13af46cf8bae03e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""Tests for gameBoard module."""

import unittest

from imago.data.enums import Player
from imago.gameLogic.gameBoard import GameBoard

TEST_BOARD_SIZE = 19

class TestGameBoard(unittest.TestCase):
    """Test gameBoard module."""

    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
        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)

        # 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())

    def testToString(self):
        """Test formatting of the board as a string."""

        board = GameBoard(9, 9)
        self.assertEqual('   A B C D E F G H J \n\
9  · · · · · · · · · \n\
8  · · · · · · · · · \n\
7  · · · · · · · · · \n\
6  · · · · · · · · · \n\
5  · · · · · · · · · \n\
4  · · · · · · · · · \n\
3  · · · · · · · · · \n\
2  · · · · · · · · · \n\
1  · · · · · · · · · ', board.toString())

        board.moveAndCapture(2, 6, Player.BLACK)
        board.moveAndCapture(5, 4, Player.WHITE)
        self.assertEqual('   A B C D E F G H J \n\
9  · · · · · · · · · \n\
8  · · · · · · · · · \n\
7  · · · · · · B · · \n\
6  · · · · · · · · · \n\
5  · · · · · · · · · \n\
4  · · · · W · · · · \n\
3  · · · · · · · · · \n\
2  · · · · · · · · · \n\
1  · · · · · · · · · ', board.toString())

if __name__ == '__main__':
    unittest.main()