1 """BadukSimulation by yong27
2 """
3 import unittest
4
5 class GoMap:
6 def __init__(self, line=19):
7 self.indices = range(line)
8 self.map = [[0]*line for i in self.indices]
9
10 def position(self, (i,j)):
11 assert i in self.indices and j in self.indices, "Index out of range"
12 return self.map[i][j]
13
14 def hand(self, color, (i,j)):
15 assert not self.map[i][j], "Can't select handed position"
16 self.map[i][j] = color
17 self.checkStatus()
18
19 def handBlack(self, (i,j)):
20 self.hand(1, (i,j))
21 def handWhite(self, (i,j)):
22 self.hand(2, (i,j))
23
24 def checkStatus(self):
25 pass
26
27 def getAround(self,(i,j)):
28 return dict(U=(i-1,j), D=(i+1,j), L=(i,j-1), R=(i,j+1))
29
30 def searchAround(self, (i,j)):
31 result = dict()
32 around = self.getAround((i,j))
33 for mark in around:
34 try:
35 result[mark] = self.position(around[mark])
36 except AssertionError:
37 pass
38 return result
39
40 def getSwarm(self, (i,j)):
41 assert self.position((i,j))
42 sw = Swarm(self.position((i,j)), (i,j))
43 inputs = [(i,j),]
44 while inputs:
45 i,j = inputs[0]
46 search = self.searchAround((i,j))
47 for mark in search:
48 linkedPos = self.getAround((i,j))[mark]
49 if (search[mark] and search[mark] == self.position((i,j))
50 and linkedPos not in sw.cells):
51 sw.addCell(linkedPos)
52 inputs.append(linkedPos)
53 inputs.remove((i,j))
54 return sw
55
56 def __repr__(self):
57 pix = lambda p: p==1 and 'X' or p==2 and 'O' or '+'
58 return '\n'.join([' '.join(map(pix,row)) for row in self.map])
59
60
61 class Swarm:
62 def __init__(self, color, (i,j)):
63 self.color = color
64 self.cells = set()
65 self.addCell((i,j))
66
67 def addCell(self, (i,j)):
68 self.cells.add((i,j))
69
70
71 class TestGoMap(unittest.TestCase):
72 def testRepr(self):
73 gm = GoMap()
74 gm.handBlack((3,4))
75 gm.handWhite((6,7))
76 expected="""\
77 + + + + + + + + + + + + + + + + + + +
78 + + + + + + + + + + + + + + + + + + +
79 + + + + + + + + + + + + + + + + + + +
80 + + + + X + + + + + + + + + + + + + +
81 + + + + + + + + + + + + + + + + + + +
82 + + + + + + + + + + + + + + + + + + +
83 + + + + + + + O + + + + + + + + + + +
84 + + + + + + + + + + + + + + + + + + +
85 + + + + + + + + + + + + + + + + + + +
86 + + + + + + + + + + + + + + + + + + +
87 + + + + + + + + + + + + + + + + + + +
88 + + + + + + + + + + + + + + + + + + +
89 + + + + + + + + + + + + + + + + + + +
90 + + + + + + + + + + + + + + + + + + +
91 + + + + + + + + + + + + + + + + + + +
92 + + + + + + + + + + + + + + + + + + +
93 + + + + + + + + + + + + + + + + + + +
94 + + + + + + + + + + + + + + + + + + +
95 + + + + + + + + + + + + + + + + + + +"""
96 self.assertEquals(expected, repr(gm))
97
98 def testRaisedWhenHanded(self):
99 gm = GoMap()
100 gm.handBlack((3,4))
101 self.assertRaises(AssertionError, gm.handWhite, (3,4))
102
103 def testSearchAround(self):
104 gm = GoMap()
105 gm.handBlack((3,4))
106 gm.handWhite((3,3))
107 self.assertEquals(dict(U=0,D=0,L=0,R=1), gm.searchAround((3,3)))
108 self.assertEquals(dict(U=0,D=0,L=2,R=0), gm.searchAround((3,4)))
109 gm.handWhite((0,3))
110 self.assertEquals(dict(D=0,L=0,R=2), gm.searchAround((0,2)))
111 self.assertEquals(dict(D=0,R=0), gm.searchAround((0,0)))
112
113 def testGetSwarm(self):
114 gm = GoMap()
115 gm.handBlack((3,4))
116 self.assertEquals(set([(3,4)]), gm.getSwarm((3,4)).cells)
117 gm.handBlack((3,3))
118 self.assertEquals(set([(3,3),(3,4)]), gm.getSwarm((3,4)).cells)
119 gm.handBlack((3,2))
120 gm.handBlack((2,2))
121 gm.handBlack((1,2))
122 self.assertEquals(set([(3,3),(3,2),(2,2),(1,2),(3,4)]), gm.getSwarm((3,4)).cells)
123 self.assertRaises(AssertionError, gm.getSwarm, (4,4))
124
125 def _testCheckStatus(self):
126 gm = GoMap()
127 gm.handBlack((3,3))
128 gm.handBlack((4,2))
129 gm.handWhite((4,3))
130 gm.handBlack((4,4))
131 gm.handBlack((5,3))
132 self.assertEquals(0, gm.position((4,3)))
133 self.assertEquals(1, gm.whiteDead)
134
135 if __name__=='__main__':
136 unittest.main(argv=('','-v'))