-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnect_4.py
240 lines (204 loc) · 6.81 KB
/
Connect_4.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
# october / november 2019
import pygame
pygame.init()
windowWidth = 1200
windowHeight = 700
window = pygame.display.set_mode((windowWidth, windowHeight))
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
board = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]
]
def stall(how_long):
clock = pygame.time.Clock()
time = 0
while True:
time += clock.tick()
for e in pygame.event.get():
if e.type == pygame.QUIT:
quit()
if time >= how_long:
return
pygame.display.update()
def list_in(sub, super):
for start_index in range(len(super)-len(sub) + 1):
slice = []
for count in range(len(sub)):
slice.append(super[start_index+count])
if slice == sub:
return True
return False
def draw_board():
window.fill(WHITE)
pygame.draw.rect(window, BLUE, [100, 100, 1000, 550])
for row_count, row in enumerate(board):
for col, spot in enumerate(row):
if spot == 0: color = WHITE
elif spot == 1: color = RED
else: color = YELLOW
pygame.draw.circle(window, color, (225 + 125 * col, 150 + row_count * 80), 30)
pygame.display.update()
def move(a_board, col, player):
if a_board[0][col] != 0:
print("invalid move")
return a_board
for count, row in enumerate(a_board[::-1]):
if row[col] == 0:
a_board[len(a_board) - count - 1][col] = player
break
return a_board
def board_state(board, player):
if player == 1:
not_player = 2
else:
not_player = 1
# check horizontal wins
for row in board:
if list_in([player, player, player, player], row):
return 1
elif list_in([not_player, not_player, not_player, not_player], row):
return -1
# check for vertical wins
for col_count in range(7):
col = [row[col_count] for row in board]
if list_in([player, player, player, player], col):
return 1
elif list_in([not_player, not_player, not_player, not_player], col):
return -1
# check pos diagonals
for row_index in range(3, 6):
for col_index in range(4):
diagonal = [board[row_index-i][col_index+i] for i in range(4)]
if list_in([player, player, player, player], diagonal):
return 1
elif list_in([not_player, not_player, not_player, not_player], diagonal):
return -1
# check neg diagonals
for row_index in range(3):
for col_index in range(4):
diagonal = [board[row_index+i][col_index+i] for i in range(4)]
if list_in([player, player, player, player], diagonal):
return 1
elif list_in([not_player, not_player, not_player, not_player], diagonal):
return -1
return 0
def game_over(board):
if board_state(board, 1) != 0:
return True
for spot in board[0]:
if spot == 0:
return False
return True
def copy(board):
new_board = []
for row in board:
new_row = []
for col in row:
new_row.append(col)
new_board.append(new_row)
return new_board
def evaluate_board(board, player):
state = board_state(board, player)
if state != 0:
return state * 500
score = 0
board_values = [
[3, 4, 5, 7, 5, 4, 3],
[4, 6, 8, 10, 8, 6, 4],
[5, 8, 11, 13, 11, 8, 5],
[5, 8, 11, 13, 11, 8, 5],
[4, 6, 8, 10, 8, 6, 4],
[3, 4, 5, 7, 5, 4, 3]
]
for row in range(len(board)):
for col in range(len(board[0])):
if board[row][col] == player:
score += board_values[row][col]
elif board[row][col] != 0:
score -= board_values[row][col]
return score
def Mini_Max(the_board, player, current_ply, max_ply, alpha, beta, og):
if current_ply == max_ply or game_over(the_board):
return evaluate_board(the_board, og)
other = [2, 1][player-1]
if current_ply % 2 == 0: # MAX
current_max = -100000
best_play = -1
for col in range(7):
if the_board[0][col] == 0:
modified = copy(the_board)
modified = move(modified, col, player)
value = Mini_Max(modified, other, current_ply + 1, max_ply, alpha, beta, og)
if value > current_max:
current_max = value
best_play = col
if alpha < current_max:
alpha = current_max
if beta <= alpha:
break
if current_ply == 0:
global board
board = move(board, best_play, og)
return current_max
else: # MINI
current_min = 100000
for col in range(7):
if the_board[0][col] == 0:
modified = copy(the_board)
modified = move(modified, col, player)
value = Mini_Max(modified, other, current_ply + 1, max_ply, alpha, beta, og)
if value < current_min:
current_min = value
if beta > current_min:
beta = current_min
if beta <= alpha:
break
return current_min
def main():
global board
player = 1
while not game_over(board):
if player == 1: color = RED
else: color = YELLOW
clicked = None
while clicked is None:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.MOUSEBUTTONDOWN:
clicked = (pygame.mouse.get_pos()[0] - 100) // 140
if clicked < 0 or clicked > 6:
clicked = None
mouse_location = pygame.mouse.get_pos()[0]
draw_board()
pygame.draw.circle(window, color, (mouse_location, 50), 30)
pygame.display.update()
board = move(board, clicked, player)
status = board_state(board, player)
#Mini_Max(board, 1, 0, 5, -100, 100, 1)
draw_board()
stall(1)
if game_over(board):
break
# go to next player
Mini_Max(board, 2, 0, 5, -100, 100, 2)
draw_board()
stall(1000)
answers = ["draw", "you win", "you lose"]
print(answers[status])
if __name__ == '__main__':
main()
# this is so the app doesn't quit when over
pygame.display.update()
while True:
pygame.time.delay(1000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()