-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectFour.py
313 lines (255 loc) · 11.4 KB
/
connectFour.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import copy
def main():
columns = 7
rows = 6
connect4 = 4
again = True
while again:
print("Choose level of difficulty(depth) from 1 to 10 ")
depth = int(input())
while (depth > 10):
print("Enter difficulty level(depth) less than 10\nEnter new number")
depth = int(input())
board = [['0' for c in range(columns)] for r in range(rows)]
print("Who plays first? Enter 1 for Computer or 2 for Player")
Turn = int(input())
while (Turn > 2 or Turn < 1):
print("Enter 1 for Computer or 2 for Player\nEnter new number")
Turn = int(input())
if Turn==1:
computerTurn = True
else:
computerTurn = False
while winner(board, columns, rows, connect4) is False and isfull(board, columns, rows) is False:
table = {}
alphabeta(board, not computerTurn, columns, rows, connect4, table, depth)
movesarray = []
moves(board, not computerTurn, columns, rows, movesarray)
maxmove = -100000000000
for item in movesarray:
x = str(item)
if x in table:
if table[x] > maxmove:
maxmove = table[x]
bestmove = x
b = item
if computerTurn == True:
board = b
printboard(board, columns, rows)
else:
addcoin(board, computerTurn, columns, rows)
computerTurn = not computerTurn
if winner(board, columns, rows, connect4) is True or isfull(board, columns, rows) is True:
print("Play again? y/n" )
playagain = input()
if playagain =='y':
again = True
else:
again = False
def moves(board, computerTurn, columns, rows, movesarray):
if computerTurn == True:
for columnchoice in range(columns):
state = copy.deepcopy(board)
if board[0][columnchoice - 1] == 'C' or board[0][columnchoice - 1] == 'P':
continue
else:
i = rows - 1
while (board[i][columnchoice - 1] == 'C' or board[i][columnchoice - 1] == 'P'):
i = i - 1
state[i][columnchoice - 1] = 'P'
movesarray.append(state)
else:
for columnchoice in range(columns):
state = copy.deepcopy(board)
if board[0][columnchoice - 1] == 'C' or board[0][columnchoice - 1] == 'P':
continue
else:
i = rows - 1
while (board[i][columnchoice - 1] == 'C' or board[i][columnchoice - 1] == 'P'):
i = i - 1
state[i][columnchoice - 1] = 'C'
movesarray.append(state)
return movesarray
def printboard(board, columns, rows):
print(" 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n")
for r in range(rows):
for c in range(columns):
print(' %s |' % board[r][c], end='')
print("\n")
print(" 1 | 2 | 3 | 4 | 5 | 6 | 7 |\n")
def isfull(board, columns, rows):
for r in range(rows):
for c in range(columns):
if board[r][c] == '0':
return False
return True
def addcoin(board, computerTurn, columns, rows):
if computerTurn == True:
print("In which column would computer like to insert coin?")
columnchoice = int(input())
while board[0][columnchoice - 1] == 'C' or board[0][columnchoice - 1] == 'P':
print("Can't put coin there. Enter new column: ")
columnchoice = int(input())
i = rows - 1
while (board[i][columnchoice - 1] == 'C' or board[i][columnchoice - 1] == 'P'):
i = i - 1
board[i][columnchoice - 1] = 'C'
else:
print("In which column would player like to insert coin?")
columnchoice = int(input())
while (columnchoice < 1 or columnchoice > columns):
print("Can't put coin there. Enter new column: ")
columnchoice = int(input())
while board[0][columnchoice - 1] == 'C' or board[0][columnchoice - 1] == 'P':
print("Can't put coin there. Enter new column: ")
columnchoice = int(input())
i = rows - 1
while (board[i][columnchoice - 1] == 'C' or board[i][columnchoice - 1] == 'P'):
i = i - 1
board[i][columnchoice - 1] = 'P'
def alphabeta(board, computerTurn, columns, rows, connect4, table, depth):
if str(board) in table:
return table[str(board)]
score = maxvalue(board, computerTurn, columns, rows, connect4, table, -100000000, 100000000, depth)
return score
def maxvalue(board, computerTurn, columns, rows, connect4, table, alpha, beta, depth):
if str(board) in table:
return table[str(board)]
if winner(board, columns, rows, connect4) is True and computerTurn == True:
table[str(board)] = 10000
return 10000
elif winner(board, columns, rows, connect4) is True and computerTurn == False:
table[str(board)] = -10000
return -10000
elif isfull(board, columns, rows) is True:
table[str(board)] = 0
return 0
elif depth == 0:
table[str(board)] = cost(board, columns, rows)
return cost(board, columns, rows)
score = -100000000
movesarray = []
moves(board, computerTurn, columns, rows, movesarray)
for item in movesarray:
score = max(score, minvalue(item, not computerTurn, columns, rows, connect4, table, alpha, beta, depth - 1))
if score >= beta:
table[str(board)] = score
return score
a = max(alpha, score)
table[str(board)] = score
return score
def minvalue(board, computerTurn, columns, rows, connect4, table, alpha, beta, depth):
if str(board) in table:
return table[str(board)]
if winner(board, columns, rows, connect4) is True and computerTurn == True:
table[str(board)] = 10000
return 10000
elif winner(board, columns, rows, connect4) is True and computerTurn == False:
table[str(board)] = -10000
return -10000
elif isfull(board, columns, rows) is True:
table[str(board)] = 0
return 0
elif depth == 0:
table[str(board)] = cost(board, columns, rows)
return cost(board, columns, rows)
score = 100000000
movesarray = []
moves(board, computerTurn, columns, rows, movesarray)
for item in movesarray:
score = min(score, maxvalue(item, not computerTurn, columns, rows, connect4, table, alpha, beta, depth - 1))
if score <= alpha:
return score
beta = min(beta, score)
table[str(board)] = score
return score
def winner(board, columns, rows, connect4):
for r in range(rows):
for c in range(columns):
if c + 3 < columns:
if board[r][c] == 'C' and board[r][c + 1] == 'C' and board[r][c + 2] == 'C' and board[r][c + 3] == 'C':
return True
if board[r][c] == 'P' and board[r][c + 1] == 'P' and board[r][c + 2] == 'P' and board[r][c + 3] == 'P':
return True
for r in range(rows):
for c in range(columns):
if r + 3 < rows:
if board[r][c] == 'C' and board[r + 1][c] == 'C' and board[r + 2][c] == 'C' and board[r + 3][c] == 'C':
return True
if board[r][c] == 'P' and board[r + 1][c] == 'P' and board[r + 2][c] == 'P' and board[r + 3][c] == 'P':
return True
for r in range(rows):
for c in range(columns):
if r + 3 < rows and c - 3 >= 0:
if board[r][c] == 'C' and board[r + 1][c - 1] == 'C' and board[r + 2][c - 2] == 'C' and board[r + 3][c - 3] == 'C':
return True
if board[r][c] == 'P' and board[r + 1][c - 1] == 'P' and board[r + 2][c - 2] == 'P' and board[r + 3][c - 3] == 'P':
return True
for r in range(rows):
for c in range(columns):
if c + 3 < columns and r + 3 < rows:
if board[r][c] == 'C' and board[r + 1][c + 1] == 'C' and board[r + 2][c + 2] == 'C' and board[r + 3][c + 3] == 'C':
return True
if board[r][c] == 'P' and board[r + 1][c + 1] == 'P' and board[r + 2][c + 2] == 'P' and board[r + 3][c + 3] == 'P':
return True
return False
def cost(board, columns, rows):
num= 0
for r in range(rows):
for c in range(columns):
if c + 1 < columns:
if board[r][c] == 'C' and board[r][c + 1] == 'C':
num= num+ 3
if board[r][c] == 'P' and board[r][c + 1] == 'P':
num= num- 3
for r in range(rows):
for c in range(columns):
if r + 1 < rows:
if board[r][c] == 'C' and board[r + 1][c] == 'C':
num= num+ 3
if board[r][c] == 'P' and board[r + 1][c] == 'P':
num= num- 3
for r in range(rows):
for c in range(columns):
if r + 1 < rows and c - 1 >= 0:
if board[r][c] == 'C' and board[r + 1][c - 1] == 'C':
num= num+ 3
if board[r][c] == 'P' and board[r + 1][c - 1] == 'P':
num= num- 3
for r in range(rows):
for c in range(columns):
if c + 1 < columns and r + 1 < rows:
if board[r][c] == 'C' and board[r + 1][c + 1] == 'C':
num= num+ 3
if board[r][c] == 'P' and board[r + 1][c + 1] == 'P':
num= num- 3
for r in range(rows):
for c in range(columns):
if c + 2 < columns:
if board[r][c] == 'C' and board[r][c + 1] == 'C' and board[r][c + 2] == 'C':
num= num+ 10
if board[r][c] == 'P' and board[r][c + 1] == 'P' and board[r][c + 2] == 'P':
num= num- 10
for r in range(rows):
for c in range(columns):
if r + 2 < rows:
if board[r][c] == 'C' and board[r + 1][c] == 'C' and board[r + 2][c] == 'C':
num= num+ 10
if board[r][c] == 'P' and board[r + 1][c] == 'P' and board[r + 2][c] == 'P':
num= num- 10
for r in range(rows):
for c in range(columns):
if r + 2 < rows and c - 2 >= 0:
if board[r][c] == 'C' and board[r + 1][c - 1] == 'C' and board[r + 2][c - 2] == 'C':
num= num+ 10
if board[r][c] == 'P' and board[r + 1][c - 1] == 'P' and board[r + 2][c - 2] == 'P':
num= num- 10
for r in range(rows):
for c in range(columns):
if c + 2 < columns and r + 2 < rows:
if board[r][c] == 'C' and board[r + 1][c + 1] == 'C' and board[r + 2][c + 2] == 'C':
num= num+ 10
if board[r][c] == 'P' and board[r + 1][c + 1] == 'P' and board[r + 2][c + 2] == 'P':
num= num- 10
return num
main()