-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connect4Game.py
239 lines (179 loc) · 7.34 KB
/
Connect4Game.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
import numpy as np
import pygame
import sys
import math
import random
import GriffinAI
import WillAI
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
ROW_COUNT = 6 # normally 6
COLUMN_COUNT = 7 # normally 7
cont_spaces_to_win = 4
usingAI = True
ai_max_time = 10000
def create_board():
b = np.zeros((ROW_COUNT, COLUMN_COUNT))
return b
def drop_piece(b, r, c, piece):
b[r][c] = piece
def is_valid_location(b, c):
if c >= COLUMN_COUNT or c < 0:
return False
return b[ROW_COUNT - 1][c] == 0
def get_next_open_row(b, c):
for r in range(ROW_COUNT):
if b[r][c] == 0:
return r
def print_board(b):
print(np.flip(b, 0))
def winning_move(b, piece):
# TODO: make method handle games with longer sequences to win
# Check horizontal locations for win
for c in range(COLUMN_COUNT - 3):
for r in range(ROW_COUNT):
if b[r][c] == piece and b[r][c + 1] == piece and b[r][c + 2] == piece and b[r][c + 3] == piece:
return True
# Check vertical locations for win
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT - 3):
if b[r][c] == piece and b[r + 1][c] == piece and b[r + 2][c] == piece and b[r + 3][c] == piece:
return True
# Check positively sloped diagonals
for c in range(COLUMN_COUNT - 3):
for r in range(ROW_COUNT - 3):
if b[r][c] == piece and b[r + 1][c + 1] == piece and b[r + 2][c + 2] == piece and b[r + 3][c + 3] == piece:
return True
# Check negatively sloped diagonals
for c in range(COLUMN_COUNT - 3):
for r in range(3, ROW_COUNT):
if b[r][c] == piece and b[r - 1][c + 1] == piece and b[r - 2][c + 2] == piece and b[r - 3][c + 3] == piece:
return True
def draw_board(b):
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
pygame.draw.rect(screen, BLUE, (c * SQUARE_SIZE, r * SQUARE_SIZE + SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE))
pygame.draw.circle(screen, BLACK, (
int(c * SQUARE_SIZE + SQUARE_SIZE / 2), int(r * SQUARE_SIZE + SQUARE_SIZE + SQUARE_SIZE / 2)), RADIUS)
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
if b[r][c] == 1:
pygame.draw.circle(screen, RED, (
int(c * SQUARE_SIZE + SQUARE_SIZE / 2), height - int(r * SQUARE_SIZE + SQUARE_SIZE / 2)), RADIUS)
elif b[r][c] == 2:
pygame.draw.circle(screen, YELLOW, (
int(c * SQUARE_SIZE + SQUARE_SIZE / 2), height - int(r * SQUARE_SIZE + SQUARE_SIZE / 2)), RADIUS)
pygame.display.update()
board = create_board()
game_over = False
turn = random.randint(0, 1)
ai_turn_time = 750 # in ms
ai_1_owner = "Will"
ai_2_owner = "Griffin"
pygame.init()
scalar = 1
SQUARE_SIZE = int(100 * scalar) # normally 100
width = COLUMN_COUNT * SQUARE_SIZE
height = (ROW_COUNT + 1) * SQUARE_SIZE
num_moves = 0
size = (width, height)
RADIUS = int(SQUARE_SIZE / 2 - 5)
screen = pygame.display.set_mode(size)
draw_board(board)
pygame.display.update()
myFont = pygame.font.SysFont("monospace", int(60 * scalar))
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif not usingAI:
if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARE_SIZE))
posX = event.pos[0]
if turn == 0:
pygame.draw.circle(screen, RED, (posX, int(SQUARE_SIZE / 2)), RADIUS)
else:
pygame.draw.circle(screen, YELLOW, (posX, int(SQUARE_SIZE / 2)), RADIUS)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARE_SIZE))
# Ask for Player 1 Input
if turn == 0:
posX = event.pos[0]
col = int(math.floor(posX / SQUARE_SIZE))
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 1)
num_moves += 1
if winning_move(board, 1):
label = myFont.render("Player 1 wins!!", True, RED)
screen.blit(label, (40, 10))
game_over = True
else:
turn -= 1
# Ask for Player 2 Input
else:
posX = event.pos[0]
col = int(math.floor(posX / SQUARE_SIZE))
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 2)
num_moves += 1
if winning_move(board, 2):
label = myFont.render("Player 2 wins!!", True, YELLOW)
screen.blit(label, (40, 10))
game_over = True
else:
turn -= 1
draw_board(board)
turn += 1
turn = turn % 2
if game_over:
pygame.time.wait(3000)
if usingAI:
if turn == 0:
start_time = pygame.time.get_ticks()
end_time = start_time + ai_turn_time
col = WillAI.return_move(board, cont_spaces_to_win, num_moves)
if pygame.time.get_ticks() < end_time:
pygame.time.wait(end_time - pygame.time.get_ticks())
if pygame.time.get_ticks() > start_time + ai_max_time:
col = random.randint(0, 6)
print(ai_1_owner + "'s AI took too long and got penalized with a random move")
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 1)
num_moves += 1
if winning_move(board, 1):
label = myFont.render(ai_1_owner + "'s AI wins!!", True, RED, BLACK)
screen.blit(label, (40, 10))
game_over = True
else:
turn -= 1
# Ask for Player 2 Input
else:
start_time = pygame.time.get_ticks()
end_time = start_time + ai_turn_time
col = GriffinAI.get_move(board, 2)
if pygame.time.get_ticks() < end_time:
pygame.time.wait(end_time - pygame.time.get_ticks())
if pygame.time.get_ticks() > start_time + ai_max_time:
col = random.randint(0, 6)
print(ai_2_owner + "'s AI took too long and got penalized with a random move")
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 2)
num_moves += 1
if winning_move(board, 2):
label = myFont.render(ai_2_owner + "'s AI wins!!", True, YELLOW, BLACK)
screen.blit(label, (40, 10))
game_over = True
else:
turn -= 1
draw_board(board)
turn += 1
turn = turn % 2
if game_over:
pygame.time.wait(3000)