summaryrefslogtreecommitdiff
path: root/qml/python/tests/test_game.py
blob: 31ff35558a254cb51cf134742c41f3416107cb45 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import unittest
from python import sgfparser, game

def create_board(fic):
   with open("python/tests/%s" % fic) as f:
       cursor = sgfparser.Cursor(f.read())

   return game.Game(cursor)

class TestBoard(unittest.TestCase):
    """ Test for the board management.
    """

    def testConvCoord(self):
        """ Test the conversion in the coordinates system.
        """

        self.assertEqual((1,2),game.Game.conv_coord("ab"))
        self.assertEqual((1,1),game.Game.conv_coord("aa"))

    def test_createTree(self):

        def fun(pos, action):
            return pos

        with open("python/tests/test.sgf") as f:
            cursor = sgfparser.Cursor(f.read())

        cursor.next()

        expected_tee = \
            [{'AB': ['or', 'pr', 'qr', 'rr', 'sr'], 'AW': ['ro', 'nq', 'oq', 'pq', 'qq', 'rq', 'mr']},
                [
                    [{'W': ['os']},
                        [
                            [{'B': ['ps']}, {'W': ['rs']}, {'B': ['ns']}, {'W': ['nr']}],
                            [{'B': ['ns']}, {'W': ['nr']}, {'B': ['rs']}, {'W': ['ps']}, {'B': ['qs']}, {'W': ['os']}]
                        ]
                    ],
                    [{'W': ['rs']},
                        [
                            [{'B': ['os']}, {'W': ['qs']}],
                            [{'B': ['qs']}, {'W': ['os']}, {'B': ['nr']}, {'W': ['ns']}]
                        ]
                    ]
                ]
            ]
        self.assertEqual(expected_tee, game.Game.create_tree(cursor, fun))

    def test_init(self):

        def fun(pos):
            return (0,0)
        currentgame = create_board("test.sgf")

        self.assertEqual(11, currentgame.min_x)
        self.assertEqual(13, currentgame.min_y)
        self.assertEqual(19, currentgame.max_x)
        self.assertEqual(19, currentgame.max_y)

        self.assertEqual((9, 7), currentgame.get_size())

        # There is only 2 state : initial board, and 2 possibilities.
        self.assertEqual(2, len(currentgame.tree))

        self.assertFalse(currentgame.side['TOP'])
        self.assertFalse(currentgame.side['LEFT'])
        self.assertTrue(currentgame.side['BOTTOM'])
        self.assertTrue(currentgame.side['RIGHT'])

        currentgame.normalize()

        self.assertEqual('W', currentgame.current_player)