-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
136 lines (136 loc) · 5.96 KB
/
tictactoe.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
def reset():
import pygame,sys,time
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0,0,255)
XO = 'x' #Who goes first?
FPS = 120
WIDTH = 28
HEIGHT = 28
MARGIN = 2
rownum = 33
colnum = 64
grid = []
for row in range(rownum):
grid.append([])
for column in range(colnum):
grid[row].append(0)
pygame.init()
WINDOW_SIZE = [1920,990]
screen = pygame.display.set_mode(WINDOW_SIZE,pygame.RESIZABLE)
x_img = pygame.transform.smoothscale(pygame.image.load("X_modified-100x100.png").convert(),(28,28))
o_img = pygame.transform.smoothscale(pygame.image.load("o_modified-100x100.png").convert(),(28,28))
pygame.display.set_caption("Caro")
def checkwin(board):
indices = [i for i,x in enumerate(board) if 'x' in x]
for index in indices:
xrowindices = [i for i, x in enumerate(board[index]) if x == "x"]
for xs in xrowindices:
if xs<=len(board[0])-5:
if board[index][xs] == board[index][xs+1] == board[index][xs+2] == board[index][xs+3] == board[index][xs+4]:
return 1
if index<=len(board)-5:
if board[index][xs] == board[index+1][xs] == board[index+2][xs] == board[index+3][xs] == board[index+4][xs]:
return 1
if xs<=len(board[0])-5:
if board[index][xs] == board[index+1][xs+1] == board[index+2][xs+2] == board[index+3][xs+3] == board[index+4][xs+4]:
return 1
if board[index][xs] == board[index+1][xs-1] == board[index+2][xs-2] == board[index+3][xs-3] == board[index+4][xs-4]:
return 1
indices1 = [i for i,x in enumerate(board) if 'o' in x]
for index1 in indices1:
orowindices = [i for i, x in enumerate(board[index1]) if x == "o"]
for os in orowindices:
if os<=len(board[0])-5:
if board[index1][os] == board[index1][os+1] == board[index1][os+2] == board[index1][os+3] == board[index1][os+4]:
return 2
if index1<=len(board)-5:
if board[index1][os] == board[index1+1][os] == board[index1+2][os] == board[index1+3][os] == board[index1+4][os]:
return 2
if os<=len(board[0])-5:
if board[index1][os] == board[index1+1][os+1] == board[index1+2][os+2] == board[index1+3][os+3] == board[index1+4][os+4]:
return 2
if board[index1][os] == board[index1+1][os-1] == board[index1+2][os-2] == board[index1+3][os-3] == board[index1+4][os-4]:
return 2
for rows in board:
for cells in rows:
count = 0
if cells == 'x' or cells == 'o':
count+=1
if count == rownum*colnum:
return 3
done = False
status = None
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
col = pos[0] // (WIDTH + MARGIN)
row= pos[1] // (HEIGHT + MARGIN)
if grid[row][col] == 0:
if XO == 'x':
grid[row][col] = XO
XO = 'o'
else:
grid[row][col] = XO
XO = 'x'
if checkwin(grid) == 1:
status = 1
if checkwin(grid) == 2:
status = 2
if checkwin(grid) == 3:
status = 3
screen.fill(BLACK)
for row in range(rownum):
for column in range(colnum):
color = WHITE
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
if grid[row][column] == 'x':
screen.blit(x_img,((WIDTH + MARGIN)*column+2,(HEIGHT + MARGIN)*row+2))
if grid[row][column] == 'o':
screen.blit(o_img,((WIDTH + MARGIN)*column+2,(HEIGHT + MARGIN)*row+2))
if status == 0:
font = pygame.font.Font('freesansbold.ttf', 100)
text = font.render('Draw', True, GREEN, BLUE)
textRect = text.get_rect()
textRect.center = (WINDOW_SIZE[0]/2,WINDOW_SIZE[1]/2)
screen.blit(text,textRect)
pygame.display.update()
time.sleep(2)
reset()
if status == 1:
font = pygame.font.Font('freesansbold.ttf', 100)
text = font.render('X wins', True, GREEN, BLUE)
textRect = text.get_rect()
textRect.center = (WINDOW_SIZE[0]/2,WINDOW_SIZE[1]/2)
screen.blit(text,textRect)
pygame.display.update()
time.sleep(2)
reset()
if status == 2:
font = pygame.font.Font('freesansbold.ttf', 100)
text = font.render('O wins', True, GREEN, BLUE)
textRect = text.get_rect()
textRect.center = (WINDOW_SIZE[0]/2,WINDOW_SIZE[1]/2)
screen.blit(text,textRect)
pygame.display.update()
time.sleep(2)
reset()
#Limit to 99999999 FPS
clock.tick(FPS)
pygame.display.update()
quit()
pygame.quit()
sys.exit()
if __name__ == "__main__":
reset()