-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (94 loc) · 3 KB
/
main.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
import pygame
import random
pygame.init()
screen_width = 600
screen_height = 600
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
font = pygame.font.Font(None, 36)
# game screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pygame tic tac toe!")
# board
board = [[' ' for _ in range(3)] for _ in range(3)]
player = 'X'
game_over = False
# drawing on board
def draw_board():
for i in range(1, 3):
pygame.draw.line(screen, black, (i * 200, 0), (i * 200, 600), 2)
pygame.draw.line(screen, black, (0, i * 200), (600, i * 200), 2)
# draw symbols
def draw_symbols():
for row in range(3):
for col in range(3):
if board[row][col] == 'X':
pygame.draw.line(screen, red, (col * 200 + 20, row * 200 + 20), (col * 200 + 180, row * 200 + 180), 5)
pygame.draw.line(screen, red, (col * 200 + 180, row * 200 + 20), (col * 200 + 20, row * 200 + 180), 5)
elif board[row][col] == 'O':
pygame.draw.circle(screen, blue, (col * 200 + 100, row * 200 + 100), 80, 5)
# check who won
def check_win(player):
# rows
for i in range(3):
if board[i][0] == player and board[i][1] == player and board[i][2] == player:
return True
# columns
for i in range(3):
if board[0][i] == player and board[1][i] == player and board[2][i] == player:
return True
# diagonals
if board[0][0] == player and board[1][1] == player and board[2][2] == player:
return True
if board[0][2] == player and board[1][1] == player and board[2][0] == player:
return True
return False
# check for a draw
def check_draw():
for row in range(3):
for col in range(3):
if board[row][col] == ' ':
return False
return True
# ai playing
def ai_move():
empty_cells = []
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
empty_cells.append((i, j))
if empty_cells:
row, col = random.choice(empty_cells)
board[row][col] = 'O'
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and not game_over and player == 'X':
x, y = pygame.mouse.get_pos()
row = y // 200
col = x // 200
if board[row][col] == ' ':
board[row][col] = 'X'
player = 'O'
if not game_over and player == 'O':
ai_move()
player = 'X'
screen.fill(white)
draw_board()
draw_symbols()
if check_win('X'):
print('X(You) won! Congratulations!')
game_over = True
elif check_win('O'):
print('O(Bot) won! Better luck next time.')
game_over = True
elif check_draw():
print('Draw! Better luck next time.')
game_over = True
pygame.display.update()
pygame.quit()