-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
226 lines (202 loc) · 8.54 KB
/
runner.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import pygame
import sys
import time
from tictactoe import TicTacToe
pygame.init()
size = width, height = 400, 600
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Tic Tac Toe vs AI")
logo_file = "./assets/img/logo.png"
logo_image = pygame.image.load(logo_file)
logo_image = pygame.transform.scale(logo_image, (32, 32))
pygame.display.set_icon(logo_image)
bg_file = "./assets/img/bg_image.jpg"
bg_image = pygame.image.load(bg_file)
bg_image = pygame.transform.scale(bg_image, size)
font_file = "./assets/font/Catskin.otf"
mediumFont = pygame.font.Font(font_file, 35)
smallFont = pygame.font.Font(font_file, 25)
largeFont = pygame.font.Font(font_file, 80)
llargeFont = pygame.font.Font(font_file, 55)
moveFont = pygame.font.Font(font_file, 60)
user = None
game_board = TicTacToe()
ai_turn = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(bg_image, bg_image.get_rect())
# Player memilih
if user is None:
# Title
title = largeFont.render("Tic-Tac-Toe", True, white)
titleRect = title.get_rect()
titleRect.center = ((width/ 2), 180)
screen.blit(title, titleRect)
# Button X
playXButton = pygame.Rect(5*(width / 8), (height / 2), width / 2.5, 50)
playX = mediumFont.render("Play as X", True, black)
playXRect = playX.get_rect()
playXButton.center=((width / 2), 300)
playXRect.center = playXButton.center
pygame.draw.rect(screen, white, playXButton,border_top_right_radius=20,
border_bottom_right_radius=20,border_bottom_left_radius=20,
border_top_left_radius=20)
screen.blit(playX, playXRect)
# Button O
playOButton = pygame.Rect(5 * (width / 2), (height / 2), width / 2.5, 50)
playO = mediumFont.render("Play as O", True, black)
playORect = playO.get_rect()
playOButton.center=((width / 2), 370)
playORect.center = playOButton.center
pygame.draw.rect(screen, white, playOButton,border_top_right_radius=20,
border_bottom_right_radius=20,border_bottom_left_radius=20,
border_top_left_radius=20)
screen.blit(playO, playORect)
#Exit Button
exitButton = pygame.Rect(5 * (width / 2), (height / 2), width / 2.5, 50)
exit = mediumFont.render("Exit", True, black)
exitRect = exit.get_rect()
exitButton.center=((width / 2), 440)
exitRect.center = exitButton.center
pygame.draw.rect(screen, white, exitButton,border_top_right_radius=20,
border_bottom_right_radius=20,border_bottom_left_radius=20,
border_top_left_radius=20)
screen.blit(exit, exitRect)
# Cek jika button diklik
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if playXButton.collidepoint(mouse):
time.sleep(0.2)
user = TicTacToe.X
elif playOButton.collidepoint(mouse):
time.sleep(0.2)
user = TicTacToe.O
elif exitButton.collidepoint(mouse):
time.sleep(0.2)
pygame.quit()
# tictactoe board
else:
# Game board
tile_size = 80
tile_origin = (width / 2 - (1.5 * tile_size),
height / 3 - (1.5 * tile_size))
tiles = []
for i in range(3):
row = []
for j in range(3):
rect = pygame.Rect(
tile_origin[0] + j * tile_size,
tile_origin[1] + i * tile_size,
tile_size, tile_size
)
pygame.draw.rect(screen, white, rect, 3)
if game_board.state[i][j] != TicTacToe.EMPTY:
move = moveFont.render(game_board.state[i][j], True, white)
moveRect = move.get_rect()
moveRect.center = rect.center
screen.blit(move, moveRect)
row.append(rect)
tiles.append(row)
game_over = game_board.terminal(game_board.state)
player = game_board.player(game_board.state)
# Tunjukkan tittle
if game_over:
winner = game_board.winner(game_board.state)
if winner is None:
title = f"Game Over : Seri"
else:
player_winner = "AI" if not ai_turn else "You"
title = f"Game Over : {player_winner} Win!"
elif user == player:
title = f" Current player: {user} "
turn = mediumFont.render("Your Turn", True, white)
titlesRect = turn.get_rect()
titlesRect.center = ((width / 2), 420)
screen.blit(turn, titlesRect)
else:
title = f" Current player: {player} "
turn = mediumFont.render("Wait for Your Turn", True, white)
titlesRect = turn.get_rect()
titlesRect.center = ((width / 2), 420)
screen.blit(turn, titlesRect)
title = mediumFont.render(title, True, white)
titleRect = title.get_rect()
titleRect.center = ((width / 2), 380)
screen.blit(title, titleRect)
# Cek pergerakkan AI
if user != player and not game_over:
if ai_turn:
time.sleep(0.5)
move = game_board.alpha_beta_search()
game_board.state = game_board.result(game_board.state, move)
ai_turn = False
else:
ai_turn = True
# Cek pergerakkan user
click, _, _ = pygame.mouse.get_pressed()
if click == 1 and user == player and not game_over:
mouse = pygame.mouse.get_pos()
for i in range(3):
for j in range(3):
if (game_board.state[i][j] == TicTacToe.EMPTY and tiles[i][j].collidepoint(mouse)):
game_board.state = game_board.result(game_board.state, (i, j))
if game_over:
#play again
againButton = pygame.Rect(5 * (width / 2), (height / 2), width / 2.5, 50)
again = smallFont.render("Play Again", True, black)
againRect = again.get_rect()
againButton.center=((width / 2), 480)
againRect.center = againButton.center
pygame.draw.rect(screen, white, againButton,border_top_right_radius=20,
border_bottom_right_radius=20,border_bottom_left_radius=20,
border_top_left_radius=20)
screen.blit(again, againRect)
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if againButton.collidepoint(mouse):
time.sleep(0.2)
user = None
game_board.clear()
ai_turn = False
#exit
exitButton = pygame.Rect(5 * (width / 2), (height / 2), width / 2.5, 50)
exit = smallFont.render("Exit", True, black)
exitRect = exit.get_rect()
exitButton.center=((width / 2), 540)
exitRect.center = exitButton.center
pygame.draw.rect(screen, white, exitButton,border_top_right_radius=20,
border_bottom_right_radius=20,border_bottom_left_radius=20,
border_top_left_radius=20)
screen.blit(exit, exitRect)
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if exitButton.collidepoint(mouse):
time.sleep(0.2)
pygame.quit()
else:
againButton = pygame.Rect(5 * (width / 2), (height / 2), width / 2.5, 50)
again = smallFont.render("Back to Menu", True, black)
againRect = again.get_rect()
againButton.center=((width / 2), 520)
againRect.center = againButton.center
pygame.draw.rect(screen, white, againButton,border_top_right_radius=20,
border_bottom_right_radius=20,border_bottom_left_radius=20,
border_top_left_radius=20)
screen.blit(again, againRect)
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if againButton.collidepoint(mouse):
time.sleep(0.2)
user = None
game_board.clear()
ai_turn = False
pygame.display.flip()