1
1
2
2
class ConnectGame :
3
3
4
- directions = [(0 ,1 ), (0 ,- 1 ), (1 ,0 ), (- 1 ,0 ), (1 ,- 1 ), (- 1 ,1 )]
4
+ directions = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 ), (1 , - 1 ), (- 1 , 1 )]
5
5
white = "O"
6
6
black = "X"
7
7
none = ""
8
8
9
9
def __init__ (self , lines ):
10
10
self .board = self .make_board (lines )
11
- assert ( len (self .board ) > 0 )
11
+ assert len (self .board ) > 0
12
12
13
13
self .width = len (self .board [0 ])
14
14
self .height = len (self .board )
15
- assert ( self .width > 0 and self .height > 0 )
15
+ assert self .width > 0 and self .height > 0
16
16
17
17
for l in self .board :
18
- assert ( len (l ) == self .width )
18
+ assert len (l ) == self .width
19
19
20
20
def valid (self , x , y ):
21
21
return x >= 0 and x < self .width and y >= 0 and y < self .height
@@ -40,14 +40,18 @@ def walk_board(self, player, x, y, visited=[]):
40
40
return True
41
41
42
42
for d in self .directions :
43
- if self .walk_board (player , x + d [0 ], y + d [1 ], visited + [(x ,y )]):
43
+ if self .walk_board (player , x + d [0 ], y + d [1 ], visited + [(x , y )]):
44
44
return True
45
45
46
46
def check_player_is_winner (self , player ):
47
47
if player == self .black :
48
- return any ([self .walk_board (player , 0 , y ) for y in range (self .height )])
48
+ for y in range (self .height ):
49
+ if self .walk_board (player , 0 , y ):
50
+ return True
49
51
if player == self .white :
50
- return any ([self .walk_board (player , x , 0 ) for x in range (self .width )])
52
+ for x in range (self .width ):
53
+ if self .walk_board (player , x , 0 ):
54
+ return True
51
55
52
56
def get_winner (self ):
53
57
if self .check_player_is_winner (self .black ):
@@ -56,6 +60,7 @@ def get_winner(self):
56
60
return self .white
57
61
return self .none
58
62
63
+
59
64
def play (board ):
60
65
game = ConnectGame (board )
61
66
return game .get_winner ()
0 commit comments