-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.py
113 lines (95 loc) · 3.84 KB
/
Board.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
class Board:
def __init__(self):
self.plot = [["Empty" for y in range(6)] for x in range(7)]
self.won = False
def set_plot(self, location, color):
self.plot[location[0]][location[1]] = color
self.won = self.horizontal_check(location, color)
def available_plot(self, location):
if -1 < location < 7:
for i in range(6):
if self.plot[location][i] == "Empty":
return True
return False
def get_available_plot(self, x):
for i, plo in enumerate(self.plot[x]):
if self.plot[x][i] == "Empty":
return i
def draw(self):
for i in range(7):
for j in range(6):
if self.plot[i][j] == "Empty":
return False
return True
def horizontal_check(self, location, color):
empty = [False, False]
consecutive = 1
for i in range(3):
if -1 < location[0] + 1 + i < 7 and not empty[0]:
if self.plot[location[0] + 1 + i][location[1]] == color:
consecutive += 1
else:
empty[0] = True
if -1 < location[0] - 1 - i < 7 and not empty[1]:
if self.plot[location[0] - 1 - i][location[1]] == color:
consecutive += 1
else:
empty[1] = True
if consecutive >= 4:
return True
else:
return self.vertical_check(location, color)
def vertical_check(self, location, color):
empty = [False, False]
consecutive = 1
for i in range(3):
if -1 < location[1] + 1 + i < 6 and not empty[0]:
if self.plot[location[0]][location[1] + 1 + i] == color:
consecutive += 1
else:
empty[0] = True
if -1 < location[1] - 1 - i < 6 and not empty[1]:
if self.plot[location[0]][location[1] - 1 - i] == color:
consecutive += 1
else:
empty[1] = True
if consecutive >= 4:
return True
else:
return self.forward_diagonal_check(location, color)
def forward_diagonal_check(self, location, color):
empty = [False, False]
consecutive = 1
for i in range(3):
if -1 < location[0] + 1 + i < 7 and -1 < location[1] + 1 + i < 6 and not empty[0]:
if self.plot[location[0]+ 1 + i][location[1] + 1 + i] == color:
consecutive += 1
else:
empty[0] = True
if -1 < location[0] - 1 - i < 7 and -1 < location[1] - 1 - i < 6 and not empty[1]:
if self.plot[location[0] - 1 - i][location[1] - 1 - i] == color:
consecutive += 1
else:
empty[1] = True
if consecutive >= 4:
return True
else:
return self.backward_diagonal_check(location, color)
def backward_diagonal_check(self, location, color):
empty = [False, False]
consecutive = 1
for i in range(3):
if -1 < location[0] - 1 - i < 7 and -1 < location[1] + 1 + i < 6 and not empty[0]:
if self.plot[location[0] - 1 - i][location[1] + 1 + i] == color:
consecutive += 1
else:
empty[0] = True
if -1 < location[0] + 1 + i < 7 and -1 < location[1] - 1 - i < 6 and not empty[1]:
if self.plot[location[0] + 1 + i][location[1] - 1 - i] == color:
consecutive += 1
else:
empty[1] = True
if consecutive >= 4:
return True
else:
return False