-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.py
290 lines (259 loc) · 7.56 KB
/
connect4.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# connect4.py
#
# Copyright 2011 James McClain <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
from copy import deepcopy
class createboard(object):
"""Create a blank connect4 board,\
use player names and optional player peices"""
def __init__(self,player1="player1",player2="player2",player1char="x",player2char="o"):
self.won = False
self.history = []
self.player1char = player1char
self.player2char = player2char
# Check if inputted values are sane
if len(self.player1char) == 1:
pass
else:
self.player1char = "x"
if len(self.player2char) == 1:
pass
else:
self.player2char = "o"
self.player1 = player1
self.player2 = player2
# Check if inputted values are sane
if self.player1:
pass
else:
self.player1 = "player1"
if self.player2:
pass
else:
self.player2 = "player2"
self.turnchar = self.player1char
self.turn = self.player1
self.board = []
for x in range(6):
self.board.append(["|_"])
for i in range(5):
self.board[x].append("|_")
self.board[x].append("|_|")
def printboard(self):
for row in range(6):
print ''.join(self.board[row])+"\n",
print "|=|=|=|=|=|=|=|\n[a","b","c","d","e","f","g]"
def getboard(self):
get = []
for row in range(6):
get.append(''.join(self.board[row])+"\n")
get.append(' '.join(["|=|=|=|=|=|=|=|\n[a","b","c","d","e","f","g]"]))
get = ''.join(get)
return get
def __checkplace__(self,row,column):
outofrange = False
if row > 5:
outofrange = True
elif row < 0:
outofrange = True
if column >6:
outofrange = True
elif column < 0:
outofrange = True
if outofrange:
return False
if "_" in self.board[row][column]:
return True
elif self.player1char in self.board[row][column]:
return self.player1char
elif self.player2char in self.board[row][column]:
return self.player2char
def __turnchar__(self):
if self.turnchar == self.player1char:
self.turnchar = self.player2char
self.turn = self.player2
return self.player1char
elif self.turnchar == self.player2char:
self.turnchar = self.player1char
self.turn = self.player1
return self.player2char
def place(self,row):
returndata = {}
returndata['Victory'] = False
if self.won:
if self.won == ["Cat"]:
returndata['Error'] = "Catsgame! No winner, no more possible"+\
"moves!"
else:
returndata['Error'] = "The game has already been won by: "+self.won\
+"."
else:
returndata['Error'] = None
collet = ["a","b","c","d","e","f","g"]
playerchar = self.__turnchar__()
if row in collet:
colnum = collet.index(row)
rowcheck = 5
while True:
if self.__checkplace__(rowcheck,colnum) is True:
self.board[rowcheck][colnum] = self.board[rowcheck][colnum].\
replace("_",playerchar)
player1vic = self.__checkvictory__(self.player1char)
player2vic = self.__checkvictory__(self.player2char)
if player1vic:
self.won = self.player1
returndata['Victory'] = self.player1
self.board = player1vic
elif player2vic:
self.won = self.player2
returndata['Victory'] = self.player2
self.board = player2vic
elif player1vic is None:
returndata['Victory'] = ["Cat"]
self.won = ["Cat"]
break
elif self.__checkplace__(rowcheck,colnum) is False:
returndata['Error'] = "There is no room in that column."
self.__turnchar__()
break
else:
rowcheck -= 1
else:
returndata['Error'] = "That column does not exist."
self.__turnchar__()
return returndata
def __checkvictory__(self,playerchar):
"""Check all possible wins on the board return board with
winning pices in upper case, or return False if there is no victory"""
victory = False
boardx = deepcopy(self.board)
row = 5
column = 6
starburst_bag = []
cats_game = True
for a in range(row+1):
for b in range(column+1):
starburst = []
starburst.append((a,b))
if self.__checkplace__(a,b) is True:
cats_game = False
continue
elif self.__checkplace__(a,b) == playerchar:
starburst.append(1)
while True:
if a-starburst[1] < 0:
break
if self.__checkplace__(a-starburst[1],b) == playerchar:
starburst[1] += 1
else:
break
starburst.append(1)
while True:
if a-starburst[2] < 0:
break
if b+starburst[2] > 6:
break
if self.__checkplace__(a-starburst[2],b+starburst[2])\
== playerchar:
starburst[2] += 1
else:
break
starburst.append(1)
while True:
if b+starburst[3] > 6:
break
if self.__checkplace__(a,b+starburst[3]) == playerchar:
starburst[3] += 1
else:
break
starburst.append(1)
while True:
if a+starburst[4] > 5:
break
if b+starburst[4] > 6:
break
if self.__checkplace__(a+starburst[4],b+starburst[4])\
== playerchar:
starburst[4] += 1
else:
break
starburst_bag.append(starburst)
for starburst in starburst_bag:
a = starburst[0][0]
b = starburst[0][1]
if starburst[1] > 3:
victory = True
for i in range(starburst[1]):
boardx[a-i][b] = boardx[a-i][b].\
replace(playerchar,playerchar.upper())
if starburst[2] > 3:
victory = True
for i in range(starburst[2]):
boardx[a-i][b+i] = boardx[a-i][b+i].\
replace(playerchar,playerchar.upper())
if starburst[3] > 3:
victory = True
for i in range(starburst[3]):
boardx[a][b+i] = boardx[a][b+i].\
replace(playerchar,playerchar.upper())
if starburst[4] > 3:
victory = True
for i in range(starburst[4]):
boardx[a+i][b+i] = boardx[a+i][b+i].\
replace(playerchar,playerchar.upper())
if cats_game:
return None
if victory:
return boardx
else:
return False
def main():
player1 = raw_input('Enter player 1: ')
player2 = raw_input('Enter player 2: ')
board = createboard(player1,player2)
lasterr = False
while True:
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
if lasterr:
print lasterr
board.printboard()
row = raw_input("Player "+board.turn+"("+board.turnchar+")"+", enter row: ")
a = board.place(row)
if a['Error']:
lasterr = a['Error']
else:
lasterr = False
if a['Victory'] == ["Cat"]:
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
board.printboard()
print "Catsgame! No winners, no more possible moves. Play again."
break
elif a['Victory']:
print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
board.printboard()
print "Congratulations",a['Victory']+", you win!"
break
#~ a = raw_input("play again?(yes/no)\n")
#~ if "n" in a:
#~ exit()
#~ else:
#~ main()
if __name__ == "__main__":
main()