-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI3_PLAYER(AI1).py
396 lines (317 loc) · 12.6 KB
/
AI3_PLAYER(AI1).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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
# coding: utf-8
# In[5]:
import numpy
import random
COLUMN_COUNT = 7
ROW_COUNT = 6
AI = 2
PLAYER = 1
position_values = numpy.array([[30,40,50,70,50,40,30],[40,60,80,100,80,60,40],[50,80,110,130,110,80,50],[50,80,110,130,110,80,50],[40,60,80,100,80,60,40],[30,40,50,70,50,40,30]])
def create_game_board():
# Create board and fill positions with zeros
board = numpy.zeros((ROW_COUNT, COLUMN_COUNT), dtype=int)
return board
def playable_location_control(board, col):
# if location is 0 this means it is valid
return board[ROW_COUNT-1][col] == 0
def play_piece(board, row, col, piece):
#play game
board[row][col] = piece
def get_next_row(board, col):
# Get next available row
for r in range(ROW_COUNT):
if board[r][col] == 0:
return r
def evaluation_1(board, piece):
three_score = consecutive_three(board, PLAYER) * 1000
two_score = consecutive_two(board, PLAYER) * 10
AI_three_score = consecutive_three(board, AI) * 1000
AI_two_score = consecutive_two(board, AI) * 10
# Calculate final score of AI and PLAYER
score = two_score + three_score - AI_two_score - AI_three_score
return score
def evaluation_3(board, piece):
four_score = consecutive_four(board, PLAYER) * 100000
three_score = consecutive_three(board, PLAYER) * 1000
two_score = consecutive_two(board, PLAYER) * 10
AI_four_score = consecutive_four(board, AI) * 100000
AI_three_score = consecutive_three(board, AI) * 1000
AI_two_score = consecutive_two(board, AI) * 10
# Calculate final score of AI and PLAYER
score = AI_two_score + AI_three_score + AI_four_score - two_score - three_score - four_score
return score
def consecutive_two(board, piece):
# # the number of two in a row piece has
count_two_pieces = 0
for r in range(ROW_COUNT-1):
for c in range(COLUMN_COUNT-1):
if c < COLUMN_COUNT-3:
#horizantal right
if board[r][c] == board[r][c+1] == piece and board[r][c+2] == board[r][c+3] == 0:
count_two_pieces += 1
if r < ROW_COUNT-3:
#diagonal right
if board[r][c] == board[r+1][c+1] == piece and board[r+2][c+2] == board[r+3][c+3] == 0:
count_two_pieces += 1
if c >= 3:
#horizantal left
if board[r][c] == board[r][c-1] == piece and board[r][c-2] == board[r][c-3] == 0:
count_two_pieces += 1
if r < ROW_COUNT-3:
#diagonal left
if board[r][c] == board[r+1][c-1] == piece and board[r+2][c-2] == board[r+3][c-3] == 0:
count_two_pieces += 1
if r < ROW_COUNT-3:
#vertical
if board[r][c] == board[r+1][c] == piece and board[r+2][c] == board[r+3][c] == 0:
count_two_pieces += 1
return count_two_pieces
def consecutive_three(board, piece):
# the number of three in a row piece has
count_three_pieces = 0
for r in range(ROW_COUNT-1):
for c in range(COLUMN_COUNT-1):
if c < COLUMN_COUNT-3:
#horizantal right
if board[r][c] == board[r][c+1] == board[r][c+2] == piece and board[r][c+3] == 0:
count_three_pieces += 1
if r < ROW_COUNT-3:
#diagonal right
if board[r][c] == board[r+1][c+1] == board[r+2][c+2] == piece and board[r+3][c+3] == 0:
count_three_pieces += 1
if c >= 3:
#horizantal left
if board[r][c] == board[r][c-1] == board[r][c-2] == piece and board[r][c-3] == 0:
count_three_pieces += 1
if r < ROW_COUNT-3:
#diagonal left
if board[r][c] == board[r+1][c-1] == board[r+2][c-2] == piece and board[r+3][c-3] == 0:
count_three_pieces += 1
if r < ROW_COUNT-3:
#vertical
if board[r][c] == board[r+1][c] == board[r+2][c] == piece and board[r+3][c] == 0:
count_three_pieces += 1
return count_three_pieces
def consecutive_four(board, piece):
# the number of four in a row piece has
count_four_pieces = 0
for r in range(ROW_COUNT-1):
for c in range(COLUMN_COUNT-1):
if c < COLUMN_COUNT-3:
# horizantal right
if board[r][c] == board[r][c+1] == board[r][c+2] == board[r][c+3] == piece:
count_four_pieces += 1
if r < ROW_COUNT-3:
# diagonal right
if board[r][c] == board[r+1][c+1] == board[r+2][c+2] == board[r+3][c+3] == piece:
count_four_pieces += 1
if c >= 3:
# horizantal left
if board[r][c] == board[r][c-1] == board[r][c-2] == board[r][c-3] == piece:
count_four_pieces += 1
if r < ROW_COUNT-3:
# diagonal left
if board[r][c] == board[r+1][c-1] == board[r+2][c-2] == board[r+3][c-3] == piece:
count_four_pieces += 1
if r < ROW_COUNT-3:
# vertical
if board[r][c] == board[r+1][c] == board[r+2][c] == board[r+3][c] == piece:
count_four_pieces += 1
return count_four_pieces
def minimax_1(board, depth,maximizingPlayer):
# playable locations
playable_locations = find_playable_locations(board)
# check if game is over or not
is_terminal, winner = check_game_over(board)
if depth == 0 or is_terminal:
# if game is over
if is_terminal:
# If AI win return 1000000 score
if winner == PLAYER:
return (None, 1000000)
# If Player win return -1000000 score
elif winner == AI:
return (None, -1000000)
else:
# If no one win ,return 0 score
return (None, 0)
else:
# If depth equals 0 return evaluation score
return (None, evaluation_1(board, PLAYER))
if maximizingPlayer:
value = float("-inf")
column = random.choice(playable_locations)
for col in playable_locations:
row = open_row(board, col)
# Create a temp board
temp_board = board.copy()
# simulation of move
play_piece(temp_board, row, col, PLAYER)
# until terminal or deepest board state
new_score = minimax_1(temp_board, depth-1, False)[1]
if new_score > value:
value = new_score
column = col
return column, value
else:
value = float("inf")
column = random.choice(playable_locations)
for col in playable_locations:
row = open_row(board, col)
# Create a temp board
temp_board = board.copy()
# simulation of move
play_piece(temp_board, row, col, AI)
# until terminal or deepest board state
new_score = minimax_1(temp_board, depth-1, True)[1]
if new_score < value:
value = new_score
column = col
return column, value
def minimax_3(board, depth,maximizingPlayer):
# playable locations
playable_locations = find_playable_locations(board)
# Check if game is over or not
is_terminal, winner = check_game_over(board)
if depth == 0 or is_terminal:
# if game over
if is_terminal:
# If AI win return 1000000 score
if winner == AI:
return (None, 1000000)
# If Player win return -1000000 score
elif winner == PLAYER:
return (None, -1000000)
else:
# If no one win ,return 0 score
return (None, 0)
else:
# If depth equals 0 return evaluation score
return (None, evaluation_3(board, AI))
if maximizingPlayer:
value = float("-inf")
column = random.choice(playable_locations)
for col in playable_locations:
row = open_row(board, col)
# Create a temp board
temp_board = board.copy()
# simulation
play_piece(temp_board, row, col, AI)
# until terminal or deepest board state
new_score = minimax_3(temp_board, depth-1, False)[1]
if new_score > value:
value = new_score
column = col
return column, value
else:
value = float("inf")
column = random.choice(playable_locations)
for col in playable_locations:
row = open_row(board, col)
# Create a temp board
temp_board = board.copy()
# simulation
play_piece(temp_board, row, col, PLAYER)
#until terminal or deepest board state
new_score = minimax_3(temp_board, depth-1, True)[1]
if new_score < value:
value = new_score
column = col
return column, value
def open_row(board, col):
for r in range(ROW_COUNT):
if board[r][col] == 0:
return r
def find_playable_locations(board):
# all playable locations
playable_locations = []
for col in range(COLUMN_COUNT):
# If location is available
if playable_location_control(board, col):
# position is added to playable locations
playable_locations.append(col)
return playable_locations
def game_board(board):
print(numpy.flip(board, 0))
def check_game_over(board):
# Check if game is over
if winner_check(board, PLAYER):
return True, PLAYER
if winner_check(board, AI):
return True, AI
if len(find_playable_locations(board)) <= 0:
return True, 0
return False, -1
def winner_check(board, piece):
# Check for win
# horizontal
for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
return True
# vertical
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT-3):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
return True
# diagonal right
for c in range(COLUMN_COUNT-3):
for r in range(ROW_COUNT-3):
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece:
return True
# diagonal left
for c in range(COLUMN_COUNT-3):
for r in range(3, ROW_COUNT):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece:
return True
game_over = False
board = create_game_board()
turn = 0
# Game
print("Game is start!!!")
game_board(board)
print("-------------------")
while not game_over:
if turn == 0:
print("**********")
print("AI 1 MOVE")
print("**********")
# Take input
col, minimax_score = minimax_1(board, 4, True)
# if input is true
if playable_location_control(board, col):
row = get_next_row(board, col)
play_piece(board, row, col, PLAYER)
turn = 1
game_board(board)
if winner_check(board, PLAYER):
print("**********")
print("**********")
print("AI 1 WINS!!!")
print("**********")
print("**********")
game_over = True
print("-------------------")
else:
print("no column to move")
else:
print("**********")
print("AI 3 MOVE")
print("**********")
col, minimax_score = minimax_3(board, 4, True)
if playable_location_control(board, col):
row = get_next_row(board, col)
play_piece(board, row, col, AI)
turn = 0
game_board(board)
if winner_check(board, AI):
print("**********")
print("**********")
print("AI 3 WINS!!!")
print("**********")
print("**********")
game_over = True
print("-------------------")
else:
print("no column to move")