import pygame as pg
import numpy as np
pg.init()
WIDTH = 600
HEIGHT = 600
LINE_COLOR = (10, 52, 95)
BACKGROUND_COLOR = (18, 212, 199)
CIRCLE_COLOR = (255, 255, 255)
X_COLOR = (1, 11, 10)
BOARD_ROWs = 3
BOARD_COLUMNs = 3
CIRCLE_RADIUS = 60
CIRCLE_WIDTH = 15
X_WIDTH = 20
SPACE = 43
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Tic Tac Toe")
screen.fill(BACKGROUND_COLOR)
board = np.zeros((BOARD_ROWs, BOARD_COLUMNs))
def draw_lines_on_board(line_width):
pg.draw.line(screen, LINE_COLOR, (200, 0), (200, 600), line_width)
pg.draw.line(screen, LINE_COLOR, (400, 0), (400, 600), line_width)
pg.draw.line(screen, LINE_COLOR, (0, 200), (600, 200), line_width)
pg.draw.line(screen, LINE_COLOR, (0, 400), (600, 400), line_width)
def draw_figures():
for i in range(0, BOARD_ROWs):
for j in range(0, BOARD_COLUMNs):
if board[i][j] == 1:
pg.draw.line(screen, X_COLOR, (j * 200 + SPACE, i * 200 + 200 - SPACE), (j * 200 + 200 - SPACE, i * 200 + SPACE), X_WIDTH)
pg.draw.line(screen, X_COLOR, (j * 200 + SPACE, i * 200 + SPACE), (j * 200 + 200 - SPACE, i * 200 + 200 - SPACE), X_WIDTH)
if board[i][j] == 2:
pg.draw.circle(screen, CIRCLE_COLOR, (int(j * 200 + 100), int(i * 200 + 100)), CIRCLE_RADIUS, CIRCLE_WIDTH)
def mark_cell(i, j, player):
board[i][j] = player
def is_cell_available(i, j):
return board[i][j] == 0
def is_full():
for i in range(0, BOARD_ROWs):
for j in range(0, BOARD_COLUMNs):
if board[i][j] == 0:
return False
return True
def check_win(player):
for j in range(0, BOARD_COLUMNs):
if board[0][j] == player and board[1][j] == player and board[2][j] == player:
draw_vertical_winning_line(j, player)
return True
for i in range(0, BOARD_ROWs):
if board[i][0] == player and board[i][1] == player and board[i][2] == player:
draw_horizontal_winning_line(i, player)
return True
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
draw_asc_diagonal(player)
return True
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
draw_desc_diagonal(player)
return True
return False
def draw_vertical_winning_line(j, player):
pos = j * 200 + 100
if player == 1:
color = X_COLOR
else:
color = CIRCLE_COLOR
pg.draw.line(screen, color, (pos, 15), (pos, HEIGHT - 15), 15)
def draw_horizontal_winning_line(i, player):
pos = i * 200 + 100
if player == 1:
color = X_COLOR
else:
color = CIRCLE_COLOR
pg.draw.line(screen, color, (15, pos), (WIDTH - 15, pos), 15)
def draw_desc_diagonal(player):
if player == 1:
color = X_COLOR
else:
color = CIRCLE_COLOR
pg.draw.line(screen, color, (15, HEIGHT - 15), (WIDTH - 15, 15), 15)
def draw_asc_diagonal(player):
if player == 1:
color = X_COLOR
else:
color = CIRCLE_COLOR
pg.draw.line(screen, color, (15, 15), (WIDTH - 15, HEIGHT - 15), 15)
draw_lines_on_board(15)
player = 1
game_over = False
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
exit(0)
if event.type == pg.MOUSEBUTTONDOWN and not game_over:
mouse_x = event.pos[0]
mouse_y = event.pos[1]
clicked_row = int(mouse_y // 200)
clicked_col = int(mouse_x // 200)
if is_cell_available(clicked_row, clicked_col):
mark_cell(clicked_row, clicked_col, player)
if check_win(player):
game_over = True
player = 3 - player
print(board)
draw_figures()
pg.display.update()