-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket_tic-tac-toe.py
480 lines (425 loc) · 14.8 KB
/
socket_tic-tac-toe.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# MODULES
import numpy as np
import pygame
import pygame.locals
import pygame_menu
import random
import select
import socket
import sys
import time
# CONSTANTS
BG_COLOR = (81, 194, 255)
LINE_COLOR = (62, 149, 195)
CIRCLE_COLOR = (157, 97, 208)
CROSS_COLOR = (66, 66, 66)
PORT = 48763
# GLOBAL VARIABLES
screen_size = 800
grid_size = 3
space_size = screen_size // grid_size
line_width = space_size // 9
win_line_width = space_size // 10
win_line_padding = space_size // 12
circle_radius = space_size // 3
circle_width = space_size // 12
cross_width = space_size // 9
cross_padding = space_size // 4
grid = np.zeros((grid_size, grid_size))
s = socket.socket()
conn = socket.socket()
order = 0
# PYGAME STUFF
pygame.init()
pygame.display.set_caption('Tic-Tac-Toe')
screen = pygame.display.set_mode((screen_size, screen_size), pygame.RESIZABLE)
font = pygame.font.SysFont('Arial', 40)
# FUNCTIONS FOR MAIN MENU
def set_grid_size(value, size):
global grid_size, grid
grid_size = size
grid = np.zeros((grid_size, grid_size))
update_ui_vars()
def update_ui_vars():
global grid_size, space_size, line_width, win_line_width, win_line_padding, circle_radius, circle_width, cross_width, cross_padding
space_size = screen_size // grid_size
line_width = space_size // 9
win_line_width = space_size // 10
win_line_padding = space_size // 12
circle_radius = space_size // 3
circle_width = space_size // 12
cross_width = space_size // 9
cross_padding = space_size // 4
# FUNCTIONS FOR GAME
def draw_lines():
# horizontal
for row in range(1, grid_size):
pygame.draw.line(screen, LINE_COLOR, (0, row * space_size),
(screen_size, row * space_size), line_width)
# vertical
for col in range(1, grid_size):
pygame.draw.line(screen, LINE_COLOR, (col * space_size, 0),
(col * space_size, screen_size), line_width)
def draw_figures():
for row in range(grid_size):
for col in range(grid_size):
if grid[row][col] == 1: # draw X for player 1
pygame.draw.line(screen, CROSS_COLOR, (col * space_size + cross_padding, row * space_size + cross_padding),
((col + 1) * space_size - cross_padding, (row + 1) * space_size - cross_padding), cross_width)
pygame.draw.line(screen, CROSS_COLOR, (col * space_size + cross_padding, (row + 1) * space_size -
cross_padding), ((col + 1) * space_size - cross_padding, row * space_size + cross_padding), cross_width)
elif grid[row][col] == 2: # draw O for player 2
pygame.draw.circle(screen, CIRCLE_COLOR, (col * space_size + space_size //
2, row * space_size + space_size // 2), circle_radius, circle_width)
def mark_space(row, col, player):
grid[row][col] = player
def is_space_available(row, col):
return grid[row][col] == 0
def is_winner(player):
# vertical win check
for col in range(grid_size):
if np.all(grid.T[col] == player):
draw_vertical_winning_line(col, player)
return True
# horizontal win check
for row in range(grid_size):
if np.all(grid[row] == player):
draw_horizontal_winning_line(row, player)
return True
# diagonal win check
if np.all(grid.diagonal() == player):
draw_diagonal_winning_line(player)
return True
# antidiagonal win chek
if np.all(np.fliplr(grid).diagonal() == player):
draw_antidiagonal_winning_line(player)
return True
return False
def draw_vertical_winning_line(col, player):
if player == 1:
color = CROSS_COLOR
elif player == 2:
color = CIRCLE_COLOR
pos_x = col * space_size + space_size // 2
pygame.draw.line(screen, color, (pos_x, win_line_padding),
(pos_x, screen_size - win_line_padding), win_line_width)
def draw_horizontal_winning_line(row, player):
if player == 1:
color = CROSS_COLOR
elif player == 2:
color = CIRCLE_COLOR
pos_y = row * space_size + space_size // 2
pygame.draw.line(screen, color, (win_line_padding, pos_y),
(screen_size - win_line_padding, pos_y), win_line_width)
def draw_diagonal_winning_line(player):
if player == 1:
color = CROSS_COLOR
elif player == 2:
color = CIRCLE_COLOR
pygame.draw.line(screen, color, (win_line_padding, win_line_padding),
(screen_size - win_line_padding, screen_size - win_line_padding), win_line_width)
def draw_antidiagonal_winning_line(player):
if player == 1:
color = CROSS_COLOR
elif player == 2:
color = CIRCLE_COLOR
pygame.draw.line(screen, color, (win_line_padding, screen_size - win_line_padding),
(screen_size - win_line_padding, win_line_padding), win_line_width)
def game_restart():
screen.fill(BG_COLOR)
draw_lines()
grid.fill(0)
# WAIT SERVER LOOP
def wait_server():
global screen_size, screen, s, conn, order
order = random.randint(1, 2) # random select order
text1 = font.render(
'Waiting for connection, press Esc to cancel.', True, (255, 255, 255))
text2 = font.render('You will be the X!', True, (255, 255, 255))
text3 = font.render('You will be the O!', True, (255, 255, 255))
text1_rect = text1.get_rect(center=(screen_size // 2, screen_size // 2 - 100))
text2_rect = text2.get_rect(center=(screen_size // 2, screen_size // 2 + 100))
screen.fill(BG_COLOR)
screen.blit(text1, text1_rect)
if order == 1:
screen.blit(text2, text2_rect)
else:
screen.blit(text3, text2_rect)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', PORT))
s.listen()
print('[server] welcoming socket start at: %s:%s' % ('*', PORT))
s.setblocking(False)
inputs = [s]
is_connected = False
while not is_connected:
readable, _, _ = select.select(inputs, [], [], 0.1)
for sck in readable:
if sck is s:
conn, addr = sck.accept()
conn.setblocking(True)
is_connected = True
for event in pygame.event.get():
# when player closed the screen
if event.type == pygame.QUIT:
s.close()
sys.exit()
# when player changed screen size
if event.type == pygame.VIDEORESIZE:
screen_size = min(event.w, event.h)
screen = pygame.display.set_mode(
(screen_size, screen_size), pygame.RESIZABLE)
update_ui_vars()
main_menu.resize(screen_size, screen_size)
help_menu.resize(screen_size, screen_size)
text1_rect = text1.get_rect(center=(screen_size // 2, screen_size // 2 - 100))
text2_rect = text2.get_rect(center=(screen_size // 2, screen_size // 2 + 100))
screen.fill(BG_COLOR)
screen.blit(text1, text1_rect)
if order == 1:
screen.blit(text2, text2_rect)
else:
screen.blit(text3, text2_rect)
# when player press Esc key
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
print('[server] connection canceled.')
s.close()
return
pygame.display.update()
s.close() # close welcoming socket to prevent other connection
print('[server] connected by ' + str(addr))
outdata = str(grid_size) + ' ' + str(order % 2 + 1)
conn.sendall(outdata.encode())
game_server()
conn.close()
# WAIT CLIENT LOOP
def wait_client(value):
global s, order
text1 = font.render('Waiting for connection...', True, (255, 255, 255))
text2 = font.render('You will be the X!', True, (255, 255, 255))
text3 = font.render('You will be the O!', True, (255, 255, 255))
text4 = font.render(
'Can\'t find the game or it\'s already full.', True, (255, 255, 255))
text1_rect = text1.get_rect(
center=(screen_size // 2, screen_size // 2 - 100))
text2_rect = text2.get_rect(
center=(screen_size // 2, screen_size // 2 + 100))
text4_rect = text4.get_rect(
center=(screen_size // 2, screen_size // 2 + 100))
screen.fill(BG_COLOR)
screen.blit(text1, text1_rect)
pygame.display.update()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
try:
s.connect((value, PORT))
except socket.error as exc:
print('[client] ' + str(exc))
screen.blit(text4, text4_rect)
pygame.display.update()
time.sleep(2)
s.close()
return
print('[client] connected to (\'%s\', %s)' % (value, PORT))
indata = s.recv(1024)
if not indata:
print('[client] server closed connection.')
else:
info = indata.decode().split()
set_grid_size('', int(info[0]))
order = int(info[1])
if order == 1:
screen.blit(text2, text2_rect)
else:
screen.blit(text3, text2_rect)
pygame.display.update()
time.sleep(1)
game_client()
s.close()
# GAME SERVER LOOP
def game_server():
global screen_size, screen, conn
is_game_running = True
is_my_turn = True
is_game_over = False
game_restart()
if order == 2:
is_my_turn = False
conn.setblocking(False)
inputs = [conn]
while is_game_running:
readable, _, _ = select.select(inputs, [], [], 0.1)
for sck in readable:
if sck is conn:
indata = sck.recv(1024)
if not indata:
print('[server] client closed connection.')
return
else:
clicked = indata.decode().split()
mark_space(int(clicked[0]), int(clicked[1]), order % 2 + 1)
if is_winner(order % 2 + 1):
is_game_over = True
draw_figures()
is_my_turn = True
for event in pygame.event.get():
# when player closed the screen
if event.type == pygame.QUIT:
conn.close()
sys.exit()
# when player changed screen size
if event.type == pygame.VIDEORESIZE:
screen_size = min(event.w, event.h)
screen = pygame.display.set_mode(
(screen_size, screen_size), pygame.RESIZABLE)
update_ui_vars()
main_menu.resize(screen_size, screen_size)
help_menu.resize(screen_size, screen_size)
screen.fill(BG_COLOR)
draw_lines()
draw_figures()
if is_game_over:
is_winner(1)
is_winner(2)
# when player clicked the screen
if event.type == pygame.MOUSEBUTTONDOWN and is_my_turn and not is_game_over:
# get mouse coordinates
pos_x, pos_y = event.pos
# transform screen coordinates to grid coordinates
clicked_col = min(pos_x // space_size, grid_size - 1)
clicked_row = min(pos_y // space_size, grid_size - 1)
if is_space_available(clicked_row, clicked_col):
mark_space(clicked_row, clicked_col, order)
if is_winner(order):
is_game_over = True
draw_figures()
outdata = str(clicked_row) + ' ' + str(clicked_col)
conn.sendall(outdata.encode())
is_my_turn = False
# when player press R key
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
outdata = 'Restart'
conn.sendall(outdata.encode())
is_game_over = False
game_restart()
if order == 1:
is_my_turn = True
else:
is_my_turn = False
# when player press Esc key
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
print('[server] connection closed.')
is_game_running = False
pygame.display.update()
# GAME CLIENT LOOP
def game_client():
global screen_size, screen, s
is_game_running = True
is_my_turn = True
is_game_over = False
game_restart()
if order == 2:
is_my_turn = False
s.setblocking(False)
inputs = [s]
while is_game_running:
readable, _, _ = select.select(inputs, [], [], 0.1)
for sck in readable:
if sck is s:
indata = sck.recv(1024)
if not indata:
print('[client] server closed connection.')
return
elif indata.decode() == 'Restart':
is_game_over = False
game_restart()
if order == 1:
is_my_turn = True
else:
is_my_turn = False
else:
clicked = indata.decode().split()
mark_space(int(clicked[0]), int(clicked[1]), order % 2 + 1)
if is_winner(order % 2 + 1):
is_game_over = True
draw_figures()
is_my_turn = True
for event in pygame.event.get():
# when player closed the screen
if event.type == pygame.QUIT:
s.close()
sys.exit()
# when player changed screen size
if event.type == pygame.VIDEORESIZE:
screen_size = min(event.w, event.h)
screen = pygame.display.set_mode(
(screen_size, screen_size), pygame.RESIZABLE)
update_ui_vars()
main_menu.resize(screen_size, screen_size)
help_menu.resize(screen_size, screen_size)
screen.fill(BG_COLOR)
draw_lines()
draw_figures()
if is_game_over:
is_winner(1)
is_winner(2)
# when player clicked the screen
if event.type == pygame.MOUSEBUTTONDOWN and is_my_turn and not is_game_over:
# get mouse coordinates
pos_x, pos_y = event.pos
# transform screen coordinates to grid coordinates
clicked_col = min(pos_x // space_size, grid_size - 1)
clicked_row = min(pos_y // space_size, grid_size - 1)
if is_space_available(clicked_row, clicked_col):
mark_space(clicked_row, clicked_col, order)
if is_winner(order):
is_game_over = True
draw_figures()
outdata = str(clicked_row) + ' ' + str(clicked_col)
s.sendall(outdata.encode())
is_my_turn = False
# when player press Esc key
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
print('[client] connection closed.')
is_game_running = False
pygame.display.update()
# PYGAME_MENU STUFF
main_menu = pygame_menu.Menu('Main Menu', screen_size, screen_size, theme=pygame_menu.themes.THEME_BLUE)
help_menu = pygame_menu.Menu('Help', screen_size, screen_size, theme=pygame_menu.themes.THEME_BLUE)
main_menu.add.selector(
'Grid Size: ',
[('2x2', 2), ('3x3', 3), ('4x4', 4), ('5x5', 5), ('6x6', 6), ('7x7', 7), ('8x8', 8), ('9x9', 9)],
onchange=set_grid_size
)
main_menu.add.button('Start a New Game', wait_server)
main_menu.add.text_input('Join a Game: ', default='IP Address', maxchar=16, input_underline='_', onreturn=wait_client)
main_menu.add.button(help_menu.get_title(), help_menu)
main_menu.add.button('Quit', pygame_menu.events.EXIT)
main_menu.enable()
help_menu.add.label('1. Player orders are randomly decided (X first).', max_char=-1, font_size=30)
help_menu.add.label('2. Only server can set the grid size.', max_char=-1, font_size=30)
help_menu.add.label('3. Only server can restart the game with R key.', max_char=-1, font_size=30)
help_menu.add.label('4. Both players can leave the game with Esc key.', max_char=-1, font_size=30)
help_menu.add.label('5. The window is resizable but only in square.', max_char=-1, font_size=30)
help_menu.add.button('Return to Menu', pygame_menu.events.BACK)
# PYGAME MAINLOOP
if __name__ == '__main__':
while True:
events = pygame.event.get()
for event in events:
# when player closed the screen
if event.type == pygame.QUIT:
pygame.quit()
break
# when player changed screen size
if event.type == pygame.VIDEORESIZE:
screen_size = min(event.w, event.h)
screen = pygame.display.set_mode(
(screen_size, screen_size), pygame.RESIZABLE)
update_ui_vars()
main_menu.resize(screen_size, screen_size)
help_menu.resize(screen_size, screen_size)
main_menu.update(events)
main_menu.draw(screen)
pygame.display.update()