forked from mccuemandan/connectFour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectFour_v005
228 lines (187 loc) · 5.02 KB
/
connectFour_v005
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
#Initial board state
board = [["-"]*6 for i in range(7)]
col1 = board[0]
col2 = board[1]
col3 = board[2]
col4 = board[3]
col5 = board[4]
col6 = board[5]
col7 = board[6]
row1 = [ col1[0],col2[0],col3[0],col4[0],col5[0],col6[0],col7[0] ]
row2 = [ col1[1],col2[1],col3[1],col4[1],col5[1],col6[1],col7[1] ]
row3 = [ col1[2],col2[2],col3[2],col4[2],col5[2],col6[2],col7[2] ]
row4 = [ col1[3],col2[3],col3[3],col4[3],col5[3],col6[3],col7[3] ]
row5 = [ col1[4],col2[4],col3[4],col4[4],col5[4],col6[4],col7[4] ]
row6 = [ col1[5],col2[5],col3[5],col4[5],col5[5],col6[5],col7[5] ]
win = False
# define rows based on columns
def refreshRows():
global col1
global col2
global col3
global col4
global col5
global col6
global col7
col1 = board[0]
col2 = board[1]
col3 = board[2]
col4 = board[3]
col5 = board[4]
col6 = board[5]
col7 = board[6]
global row1
global row2
global row3
global row4
global row5
global row6
row1 = [ col1[0],col2[0],col3[0],col4[0],col5[0],col6[0],col7[0] ]
row2 = [ col1[1],col2[1],col3[1],col4[1],col5[1],col6[1],col7[1] ]
row3 = [ col1[2],col2[2],col3[2],col4[2],col5[2],col6[2],col7[2] ]
row4 = [ col1[3],col2[3],col3[3],col4[3],col5[3],col6[3],col7[3] ]
row5 = [ col1[4],col2[4],col3[4],col4[4],col5[4],col6[4],col7[4] ]
row6 = [ col1[5],col2[5],col3[5],col4[5],col5[5],col6[5],col7[5] ]
# display board
def displayBoard():
refreshRows()
print "1 2 3 4 5 6 7"
print " ".join(row6)
print " ".join(row5)
print " ".join(row4)
print " ".join(row3)
print " ".join(row2)
print " ".join(row1)
# MOVE CONDITIONS
# check a single column
def isColumnFilled(col):
spaces = 0
for i in col:
if i != "-":
spaces += 1
if spaces == 6:
return True
else:
return False
# CHECK FOR WIN CONDITIONS
# vertical
def checkCol(col):
for i in range(3):
if col[i]!= '-' and col[i] == col[i+1] == col[i+2] == col[i+3]:
global win
win = True
break
def checkCols():
cols = [col1,col2,col3,col4,col5,col6,col7]
for i in cols:
checkCol(i)
# horizontal
def checkRow(row):
for i in range(4):
if row[i] != '-' and row[i] == row[i+1] == row[i+2] == row[i+3]:
global win
win = True
break
def checkRows():
rows = [row1,row2,row3,row4,row5,row6]
for i in rows:
checkRow(i)
# diagonal up
def checkDiagUps():
for c in range(4):
for r in range(3):
if (board[c][r] != '-' and
board[c][r] == board[c+1][r+1] == board[c+2][r+2] == board[c+3][r+3]):
global win
win = True
# diagonal down
def checkDiagDowns():
for c in range(4):
for r in range(3):
if (board[c][5-r] != '-' and
board[c][5-r] == board[c+1][4-r] == board[c+2][3-r] == board[c+3][2-r]):
global win
win = True
# check draw
def checkDraw():
cols = [col1, col2, col3, col4, col5, col6, col7]
filledColumns = 0
for i in cols:
if isColumnFilled(i) == True:
filledColumns += 1
if filledColumns == 7:
print "It's a draw! :P"
global win
win = True
#Check all wins
def checkWin():
checkCols()
checkRows()
checkDiagUps()
checkDiagDowns()
# Function for finding the row the open slot is on a given column
def lowestTier(c):
for i in range(len(c)):
if c[i] == "-":
currentTier = i
return currentTier
# Place the x into the column
def moveX():
n = raw_input("Player 1 choose column: ")
global board
if (n == "1" or
n == "2" or
n == "3" or
n == "4" or
n == "5" or
n == "6" or
n == "7"):
y = board[int(n)-1]
if isColumnFilled(y) == False:
y[lowestTier(y)] = 'x'
displayBoard()
checkWin()
else:
print "column is filled, pick again"
moveX()
else:
print "invalid column, pick again 1-7"
moveX()
if win == True:
print "player X wins!"
# Place the y into the column
def moveO():
n = raw_input("Player 2 choose column: ")
global board
if (n == "1" or
n == "2" or
n == "3" or
n == "4" or
n == "5" or
n == "6" or
n == "7"):
y = board[int(n)-1]
if isColumnFilled(y) == False:
y[lowestTier(y)] = 'o'
displayBoard()
checkWin()
else:
print "column is filled, pick again"
moveO()
else:
print "invalid column, pick again 1-7"
moveO()
if win == True:
print "player O wins!"
def play():
if win == False:
moveX()
checkDraw()
if win == False:
moveO()
checkDraw()
play()
else:
pass
displayBoard()
play()