-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.py
More file actions
181 lines (153 loc) · 4.74 KB
/
Copy pathConnectFour.py
File metadata and controls
181 lines (153 loc) · 4.74 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
## Written by Ben Fritzeen
## 12/10/2017
## Connect-Four game to be used in PiDay
class ConnectFour:
def __init__(self):
self.gameBoard = []
self.spotsUsed = 0
self.winner = -1
self.recentState = -1
self.recentCol = 0
for i in range(6):
self.gameBoard.append([0, 0, 0, 0, 0, 0, 0])
def getSpotState(self, row, col):
return self.gameBoard[row][col]
def setSpotState(self, col, state):
for row in range(5, -1, -1):
if self.gameBoard[row][col] == 0:
self.gameBoard[row][col] = state
self.spotsUsed += 1
return row, col
return "Error"
def getNumSpotsLeft(self):
return 42 - self.spotsUsed
def whoseTurn(self):
if self.getNumSpotsLeft() % 2 == 0:
return 1
else:
return 2
def isWinner(self, col, state):
row = 0
for lastRow in range(6):
if self.gameBoard[lastRow][col] != 0:
row = lastRow
break
if self.checkForFour(row, col, state):
self.winner = state
return True
def checkForFour(self, row, col, state):
if self.checkRightLeft(row, col, state):
return True
elif self.checkUpDown(row, col, state):
return True
elif self.checkDownLeftUpRight(row, col, state):
return True
elif self.checkDownRightUpLeft(row, col, state):
return True
else:
return False
def checkDownRightUpLeft(self, row, col, state):
connectCtr = 1
ctr = 1
while row + ctr != 6 and col + ctr != 7:
if connectCtr == 4:
return True
if self.gameBoard[row + ctr][col + ctr] == state:
connectCtr += 1
ctr += 1
else:
break
ctr = 1
while row - ctr != -1 and col - ctr != -1:
if connectCtr == 4:
return True
if self.gameBoard[row - ctr][col - ctr] == state:
connectCtr += 1
ctr += 1
else:
break
if connectCtr == 4:
return True
else:
return False
def checkDownLeftUpRight(self, row, col, state):
connectCtr = 1
ctr = 1
while row + ctr != 6 and col - ctr != -1:
if connectCtr == 4:
return True
if self.gameBoard[row + ctr][col - ctr] == state:
connectCtr += 1
ctr += 1
else:
break
ctr = 1
while row - ctr != -1 and col + ctr != 7:
if connectCtr == 4:
return True
if self.gameBoard[row - ctr][col + ctr] == state:
connectCtr += 1
ctr += 1
else:
break
if connectCtr == 4:
return True
else:
return False
def checkUpDown(self, row, col, state):
connectCtr = 1
for downRow in range(row + 1, 6):
if connectCtr == 4:
return True
if self.gameBoard[downRow][col] == state:
connectCtr += 1
else:
break
for upRow in range(row - 1, -1, -1):
if connectCtr == 4:
return True
if self.gameBoard[upRow][col] == state:
connectCtr += 1
else:
break
if connectCtr == 4:
return True
else:
return False
def checkRightLeft(self, row, col, state):
connectCtr = 1
for rightCol in range(col + 1, 7):
if connectCtr == 4:
return True
if self.gameBoard[row][rightCol] == state:
connectCtr += 1
else:
break
for leftCol in range(col - 1, -1, -1):
if connectCtr == 4:
return True
if self.gameBoard[row][leftCol] == state:
connectCtr += 1
else:
break
if connectCtr == 4:
return True
else:
return False
def playerMove(self, col, state):
self.setMostRecent(col, state)
return self.setSpotState(col, state)
def displayBoard(self):
for i in range(6):
print(self.gameBoard[i])
def setMostRecent(self, col, state):
self.recentState = state
self.recentCol = col
def reset(self):
for row in range(6):
for col in range(7):
self.gameBoard[row][col] = 0
self.spotsUsed = 0
self.winner = -1
self.recentState = -1
self.recentCol = 0