-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
103 lines (78 loc) · 3.07 KB
/
logic.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
import pygame
import numpy as np
from constants import *
from draws import *
def avaibale_square(board, row, col):
return board[row][col] == 0
def is_board_full(board):
for r in range(BOARD_ROWS):
for c in range(BOARD_COLS):
if board[r][c]==0:
return False
return True
def check_win(screen, board, player):
# ver check
for col in range(BOARD_COLS):
if board[0][col]==player and board[1][col]==player and board[2][col]==player:
draw_ver_win_line(screen, col, player)
return True
# hoz check
for row in range(BOARD_ROWS):
if board[row][0]==player and board[row][1]==player and board[row][2]==player:
draw_hoz_win_line(screen, row, player)
return True
# desc check
if board[0][0]==player and board[1][1]==player and board[2][2]==player:
draw_desc_diagonal(screen, player)
return True
# asc check
if board[2][0]==player and board[1][1]==player and board[0][2]==player:
draw_asc_diagonal(screen, player)
return True
return False
def draw_ver_win_line(screen, col, player):
posX = col * SQUARE_SIZE + SQUARE_SIZE//2
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (posX, 15), (posX, HEIGHT-15), 15)
def draw_hoz_win_line(screen, row, player):
posY = row * SQUARE_SIZE + SQUARE_SIZE//2
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (15, posY), (WIDTH-15, posY), 15)
def draw_asc_diagonal(screen, player):
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (15, HEIGHT-15), (WIDTH-15, 15), 15)
def draw_desc_diagonal(screen, player):
if player == 1:
color = CIRCLE_COLOR
elif player == 2:
color = CROSS_COLOR
pygame.draw.line(screen, color, (15, 15), (WIDTH-15, HEIGHT-15), 15)
def restart(screen):
screen.fill(BG_COLOR)
draw_lines(screen)
player = 1
board = np.zeros((BOARD_ROWS, BOARD_COLS))
return player, board
def disp_winner(screen, board, player, font, font2):
draw_figures(screen, board)
s = pygame.Surface((WIDTH, HEIGHT))
s.set_alpha(220)
s.fill(BG_COLOR)
screen.blit(s, (0,0))
screen.blit(font.render('WINNER!', False, TEXT_COLOR), (WIDTH//6, WIDTH//6))
screen.blit(font2.render('Press "r" key to restart', False, TEXT_COLOR), (SPACE//6, WIDTH-SPACE//2))
if player==1:
pygame.draw.circle(screen, CIRCLE_COLOR, (SQUARE_SIZE+SQUARE_SIZE//2, SQUARE_SIZE+SQUARE_SIZE//2), 3*CIRCLE_RADIUS//2, CIRCLE_WIDTH)
elif player==2:
pygame.draw.line(screen, CROSS_COLOR, (SQUARE_SIZE, SQUARE_SIZE+SQUARE_SIZE), (SQUARE_SIZE+SQUARE_SIZE, SQUARE_SIZE), CROSS_WIDTH)
pygame.draw.line(screen, CROSS_COLOR, (SQUARE_SIZE, SQUARE_SIZE), (SQUARE_SIZE+SQUARE_SIZE, SQUARE_SIZE+SQUARE_SIZE), CROSS_WIDTH)
pygame.mixer.Sound.play(win_sound)