-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
330 lines (248 loc) · 9.85 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
"""
Recriação do Jogo da Velha
@author: Prof. Daniel Cavalcanti Jeronymo
@Modificação e implementação: Pedro Baleroni
"""
maxCont = 0
player1chance = 0
player2chance = 0
drawCont = 0
cPlayer_ = 1
desligaJogo = 0
cX = -1
cY = -1
import pygame
import sys
import os
import traceback
import random
import numpy as np
import copy
class GameConstants:
# R G B
ColorWhite = (255, 255, 255)
ColorBlack = ( 0, 0, 0)
ColorRed = (255, 0, 0)
ColorGreen = ( 0, 255, 0)
ColorBlue = ( 0, 0, 255)
ColorDarkGreen = ( 0, 155, 0)
ColorDarkGray = ( 40, 40, 40)
BackgroundColor = ColorBlack
screenScale = 1
screenWidth = screenScale*600
screenHeight = screenScale*600
# grid size in units
gridWidth = 3
gridHeight = 3
# grid size in pixels
gridMarginSize = 5
gridCellWidth = screenWidth//gridWidth - 2*gridMarginSize
gridCellHeight = screenHeight//gridHeight - 2*gridMarginSize
randomSeed = 0
FPS = 30
fontSize = 20
class Game:
class GameState:
# 0 empty, 1 X, 2 O
grid = np.zeros((GameConstants.gridHeight, GameConstants.gridWidth))
currentPlayer = 0
def __init__(self, expectUserInputs=True):
self.expectUserInputs = expectUserInputs
# Game state list - stores a state for each time step (initial state)
gs = Game.GameState()
self.states = [gs]
# Determines if simulation is active or not
self.alive = True
self.currentPlayer = 1
# Journal of inputs by users (stack)
self.eventJournal = []
# Implements a game tick
# Each call simulates a world step
def update(self):
# If the game is done or there is no event, do nothing
if not self.alive or not self.eventJournal:
return
# Get the current (last) game state
gs = copy.copy(self.states[-1])
# Switch player turn
if gs.currentPlayer == 0:
gs.currentPlayer = 1
elif gs.currentPlayer == 1:
gs.currentPlayer = 2
elif gs.currentPlayer == 2:
gs.currentPlayer = 1
# Mark the cell clicked by this player if it's an empty cell
x,y = self.eventJournal.pop()
# Check if in bounds
if x < 0 or y < 0 or x >= GameConstants.gridCellHeight or y >= GameConstants.gridCellWidth:
return
# Check if cell is empty
if gs.grid[x][y] == 0:
gs.grid[x][y] = gs.currentPlayer
else: # invalid move
return
global gridTest
gridTest = gs.grid
# Check if end of game
if winnerwinnerchickendinner(gridTest):
pygame.quit()
# Add the new modified state
self.states += [gs]
#Agradecimento ao Arthur Ydalgo pela ajuda com o código abaixo,
# além de resolver um erro já existente, ajudou a prever um erro no handle game.
def winnerwinnerchickendinner(gridTest):
for i in range(3): #linha
w1 = set(gridTest[i, :])
if len(w1) == 1 and min(w1) !=0:
return w1.pop()
for i in range(3):
w1 = set(gridTest[:, i]) #coluna
if len(w1) == 1 and min(w1) !=0:
return w1.pop()
w1= set(gridTest[j,j] for j in range(3)) #diagonal
if len(w1) == 1 and min(w1) !=0:
return w1.pop()
w1= set(gridTest[-j-1,j] for j in range(3)) #contra-diagonal
if len(w1) == 1 and min(w1) !=0:
return w1.pop()
contaCerta = 0 #chance de Dar Velha
for testR in range(3):
for testC in range(3):
if gridTest[testC][testR] !=0:
contaCerta += 1
if contaCerta == 9:
return 3
return 0 #nenhum parametro de vitória
def probability(gridTest, cPlayer):
winnerChance = winnerwinnerchickendinner(gridTest) #if some win, end of recursive function
if winnerChance != 0:
if winnerChance == 1: # probability red one win
global player1chance
player1chance += 1
elif winnerChance == 2: # chance to blue get the cup
global player2chance
player2chance += 1
elif winnerChance == 3: # every guys upstairs sucks
global drawCont
drawCont += 1
return
if cPlayer == 1: #new player to play
cPlayer = 2
elif cPlayer == 2:
cPlayer = 1
# try every next game step possible
for row in range(len(gridTest)):
for collum in range(len(gridTest[row])):
if(gridTest[row][collum] == 0): #only empty spaces
newGrid = copy.deepcopy(gridTest)
newGrid[row][collum] = cPlayer
probability(newGrid, cPlayer)
def drawGrid(screen, game):
rects = []
rects = [screen.fill(GameConstants.BackgroundColor)]
# Get the current game state
gs = game.states[-1]
grid = gs.grid
# Draw the grid
for row in range(GameConstants.gridHeight):
for column in range(GameConstants.gridWidth):
color = GameConstants.ColorWhite
if grid[row][column] == 1:
color = GameConstants.ColorRed
elif grid[row][column] == 2:
color = GameConstants.ColorBlue
m = GameConstants.gridMarginSize
w = GameConstants.gridCellWidth
h = GameConstants.gridCellHeight
rects += [pygame.draw.rect(screen, color, [(2*m+w) * column + m, (2*m+h) * row + m, w, h])]
return rects
def draw(screen, font, game):
rects = []
rects += drawGrid(screen, game)
return rects
def initialize():
random.seed(GameConstants.randomSeed)
pygame.init()
game = Game()
font = pygame.font.SysFont('Courier', GameConstants.fontSize)
fpsClock = pygame.time.Clock()
# Create display surface
screen = pygame.display.set_mode((GameConstants.screenWidth, GameConstants.screenHeight), pygame.DOUBLEBUF)
screen.fill(GameConstants.BackgroundColor)
return screen, font, game, fpsClock
def handleEvents(game):
#gs = game.states[-1]
for event in pygame.event.get():
global maxCont # all plays
global drawCont # draw plays
global cX
global cY
pos = pygame.mouse.get_pos()
global cPlayer_
global player1chance # player red wins
global player2chance # player blue wins
global drawCont # draw cont
if((cX != pos[0] // (GameConstants.screenWidth // GameConstants.gridWidth))or(cY != pos[1] // (GameConstants.screenHeight // GameConstants.gridHeight))):
cX = pos[0] // (GameConstants.screenWidth // GameConstants.gridWidth)
cY = pos[1] // (GameConstants.screenHeight // GameConstants.gridHeight)
gs = game.states[-1]
grid = copy.deepcopy(gs.grid)#copia a grid do jogo atual
player1chance=0
player2chance=0
drawCont = 0
if(grid[cY][cX]==0 and cY!=-1 and cX!=-1):#confere se a posição sendo verificada é válida
jogador = copy.copy(cPlayer_)
grid[cY][cX] = jogador #atribui o jogador atual ao teste, considerando o quadrado que está em cima
probability(grid,jogador) #chama a função recursiva
maxCont = (player2chance + player1chance + drawCont)
#calcula a chance e mostra de acordo com o atual jogador
if(cPlayer_==1):
print("Chance do Vermelho ganhar: {:.2f}%".format(100*player1chance/maxCont))
print("Chance de empatar: {:.2f} %".format(100*drawCont/maxCont))
else:
print("Chance do Azul ganhar: {:.2f}%".format(100*player2chance/maxCont))
print("Chance de empatar: {:.2f} %".format(100*drawCont/maxCont))
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
col = pos[0] // (GameConstants.screenWidth // GameConstants.gridWidth)
row = pos[1] // (GameConstants.screenHeight // GameConstants.gridHeight)
#print('clicked cell: {}, {}'.format(cellX, cellY))
if cPlayer_ == 1:
cPlayer_ = 2
elif cPlayer_ == 2:
cPlayer_ = 1
# send player action to game
game.eventJournal.append((row, col))
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
def mainGamePlayer():
try:
# Initialize pygame and etc.
screen, font, game, fpsClock = initialize()
# Main game loop
while game.alive:
# Handle events
handleEvents(game)
# Update world
game.update()
# Draw this world frame
rects = draw(screen, font, game)
pygame.display.update(rects)
# Delay for required FPS
fpsClock.tick(GameConstants.FPS)
# close up shop
pygame.quit()
except SystemExit:
pass
except Exception as e:
#print("Unexpected error:", sys.exc_info()[0])
traceback.print_exc(file=sys.stdout)
pygame.quit()
#raise Exception from e
if __name__ == "__main__":
# Set the working directory (where we expect to find files) to the same
# directory this .py file is in. You can leave this out of your own
# code, but it is needed to easily run the examples using "python -m"
mainGamePlayer()