-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstate.py
113 lines (108 loc) · 3.19 KB
/
state.py
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
from board import Board
class State():
def __init__(self, board, player) -> None:
self.state = [0, 0, 0, 0, 0, 0, 0]
self.player = player
self.setState(board)
def getValueCol(self, col) -> list:
return self.state[col]
def setState(self, board: Board) -> None:
for col in range(len(self.state)):
row = board.getHeight(col)
if row == None:
row = 0
self.state[col] = self.getOne(board.state, row, col, self.player)
def winner(self):
for value in self.state:
if value == 4:
return True
return False
def getOne(self, board, row, col, player):
total = 0
# Look right
addition = 1
while True:
if col + addition > 6:
break
if board[row][col + addition] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
# Look left
addition = 1
while True:
if col - addition < 0:
break
if board[row][col - addition] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
# Up Diagonal
# Look right
addition = 1
while True:
if col + addition > 6 or row - addition < 0:
break
if board[row - addition][col + addition] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
# Look left
addition = 1
while True:
if col - addition < 0 or row - addition < 0:
break
if board[row - addition][col - addition] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
# Down Diagonal
# Look right
addition = 1
while True:
if col + addition > 6 or row + addition > 5:
break
if board[row + addition][col + addition] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
# Look left
addition = 1
while True:
if col - addition < 0 or row + addition > 5:
break
if board[row - addition][col - addition] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
# Look down
addition = 1
while True:
if row + addition > 5 or row == 0:
break
if board[row + addition][col] == player:
total += 1
if total >= 4:
return total
addition += 1
else:
break
return total