-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.py
145 lines (128 loc) · 5.03 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
137
138
139
140
141
142
143
144
from ctypes.wintypes import PINT
from pickle import TRUE
from numpy import flip, source
import pygame
import game_config as gc
from process import TicTacToe as T
from pygame import display, event, image
from time import sleep
import Images
def initial() -> pygame.Surface:
pygame.init()
display.set_caption('Tic-Tac-Toe')
screen = display.set_mode((gc.SCREEN_SIZE, gc.SCREEN_SIZE))
print( type(screen) )
return screen
def find_xy(x, y):
row = y // gc.IMAGE_SIZE
col = x // gc.IMAGE_SIZE
return row, col
def update_board_display(screen : pygame.Surface, Game : T ):#Update only
screen.blit(image.load('assets/blank.png'), (0, 0))
#sleep(1)
screen.fill((0, 0, 0))
for i in range(gc.NUM_TILES_SIDE):
for j in range(gc.NUM_TILES_SIDE):
tile = Images.Image(Game.board[i][j])
screen.blit(tile.image, (j * gc.IMAGE_SIZE + gc.MARGIN, i * gc.IMAGE_SIZE + gc.MARGIN))
display.flip()
#sleep(1)
def GameOver(Game : T,screen : pygame.Surface) -> bool:
val = Game.CheckWin()
if val == 1:
display.flip()
update_board_display(screen,Game=Game)
sleep(1.5)
screen.blit(image.load('assets/win.png'), (0, 0))
display.flip()
sleep(2.3)
return True
if val == 0:
display.flip()
update_board_display(screen,Game=Game)
sleep(1.5)
screen.blit(image.load('assets/lose.png'), (0, 0))
display.flip()
sleep(2.3)
return TRUE
return False
def run(Game : T,screen : pygame.Surface, running : bool):
update_board_display(screen,Game)
display.flip()
while running:
current_events = event.get()
for e in current_events:
if e.type == pygame.QUIT:# clicked X
running = False
if e.type == pygame.KEYDOWN: #keyboard
if e.key == pygame.K_ESCAPE:# Esc to end it
running = False
if e.type == pygame.MOUSEBUTTONDOWN: #clickd
mouse_x, mouse_y = pygame.mouse.get_pos()# got position
row, col = find_xy(mouse_x, mouse_y)#location of click
if row >= gc.NUM_TILES_SIDE or col >= gc.NUM_TILES_SIDE:# if it's on screen
continue
if Game.MoveRecord(row,col) == True:#if the move is possible
if GameOver(Game,screen):#if game ends with it
running = False
break
#game is still on
a,b = Game.NextMove()# computer makes the move
print(a,b, " here ")
if a == -1 and b == -1: #special condition for tie
running = False #end the game
screen.blit(image.load('assets/tie.png'), (0, 0))
display.flip()
sleep(2.3)
break;
else:#not a tie
if GameOver(Game,screen):#if game ends with it
running = False
break
update_board_display(screen=screen,Game=Game)
display.flip()
sleep(2.1)
#screen.blit(tile.image, (j * gc.IMAGE_SIZE + gc.MARGIN, i * gc.IMAGE_SIZE + gc.MARGIN))
def X_or_O(screen : pygame.Surface) -> str:
X = Images.Image("x")
O = Images.Image("o")
#pygame.transform.scale()
# Surface, (width, height) -> Surface
X.image = pygame.transform.scale(X.image,(gc.SCREEN_SIZE//2,gc.SCREEN_SIZE))
O.image = pygame.transform.scale(O.image,(gc.SCREEN_SIZE//2,gc.SCREEN_SIZE))
screen.blit(X.image, (gc.MARGIN, gc.MARGIN))
screen.blit(O.image, (gc.MARGIN +gc.SCREEN_SIZE//2 , gc.MARGIN))
#screen.blits( blit_sequence=[ (X.image , (0,0) , pygame.Rect(0,0,gc.SCREEN_SIZE//2,gc.SCREEN_SIZE//2)), (O.image , (gc.SCREEN_SIZE//2,gc.SCREEN_SIZE//2) , pygame.Rect(gc.SCREEN_SIZE//2,gc.SCREEN_SIZE//2,gc.SCREEN_SIZE,gc.SCREEN_SIZE)) ] )
display.flip()
pick = "-1"
while True:
current_events = event.get()
for e in current_events:
if e.type == pygame.QUIT:# clicked X
return pick
if e.type == pygame.KEYDOWN: #keyboard
if e.key == pygame.K_ESCAPE:# Esc to end it
return pick
if e.type == pygame.MOUSEBUTTONDOWN:
x,y = pygame.mouse.get_pos()
if (x < gc.SCREEN_SIZE and x >= 0) or (y < gc.SCREEN_SIZE and y > 0):# if it's on screen
if x > gc.SCREEN_SIZE//2:
pick = "O"
else: pick = "X"
screen.fill((0,0,0))
display.flip()
sleep(1.1)
return pick
return pick
def play():
screen = initial()
running = True
pick = X_or_O(screen=screen)
if pick == "-1":
return
Game = T(pick,gc.NUM_TILES_SIDE)
run(Game,screen,running)
if __name__ == "__main__":
print("Hello World!")
play()
print('Goodbye!')