forked from petcomputacaoufrgs/mico-leao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stuff.py
216 lines (192 loc) · 7.14 KB
/
Stuff.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
import PySimpleGUI as sg
def Result(position,gameIc, player):
if player == False:
gameIc.gameBoard[position] = "O"
else:
gameIc.gameBoard[position] = "X"
return gameIc
def Terminal(gameBoard):
for i in range(3):
if gameBoard[(i*3)] == gameBoard[(i*3)+1] == gameBoard[(i*3)+2] != " ":
return True
for i in range(3):
if gameBoard[i] == gameBoard[i+3] == gameBoard[i+6] != " ":
return True
if gameBoard[0] == gameBoard[4] == gameBoard[8] != " ":
return True
elif gameBoard[2] == gameBoard[4] == gameBoard[6] != " ":
return True
elif " " not in gameBoard:
return True
else:
return False
class Game:
def __init__(self):
self.gameBoard = list(" "*9)
def Assing (self, position):
self.gameBoard[position] = "X"
return
def show(self):
for i in range(3):
print(f"|{self.gameBoard[i*3]}|{self.gameBoard[(i*3)+1]}|{self.gameBoard[(i*3)+2]}|")
pass
def Evaluate(self,value):
if value == 1:
print("You Lose")
elif value == -1:
print("You Win")
elif value == 0:
print("Draw")
else:
return 0
def Value(self):
for i in range(3):
if self.gameBoard[(i*3)] == self.gameBoard[(i*3)+1] == self.gameBoard[(i*3)+2] == "O":
return 1
for i in range(3):
if self.gameBoard[i] == self.gameBoard[i+3] == self.gameBoard[i+6] == "O":
return 1
if self.gameBoard[0] == self.gameBoard[4] == self.gameBoard[8] == "O":
return 1
elif self.gameBoard[2] == self.gameBoard[4] == self.gameBoard[6] == "O":
return 1
for i in range(3):
if self.gameBoard[(i*3)] == self.gameBoard[(i*3)+1] == self.gameBoard[(i*3)+2] == "X":
return -1
for i in range(3):
if self.gameBoard[i] == self.gameBoard[i+3] == self.gameBoard[i+6] == "X":
return -1
if self.gameBoard[0] == self.gameBoard[4] == self.gameBoard[8] == "X":
return -1
elif self.gameBoard[2] == self.gameBoard[4] == self.gameBoard[6] == "X":
return -1
elif " " not in self.gameBoard:
return 0
else:
return None
def IsValid(self,position):
if position < 0 or position > 8:
return False
if self.gameBoard[int(position)] == " ":
return True
else:
return False
def teste(self):
self.gameBoard = ["X","X","X",""," "," "," "," "," "]
self.Evaluate(self.Value())
self.gameBoard = ["O","O","O",""," "," "," "," "," "]
self.Evaluate(self.Value())
self.gameBoard = ["X"," "," ","X"," "," ","X"," "," "]
self.Evaluate(self.Value())
self.gameBoard = ["O"," "," ","O"," "," ","O"," "," "]
self.Evaluate(self.Value())
self.gameBoard = ["X"," "," "," ","X"," "," "," ","X"]
self.Evaluate(self.Value())
self.gameBoard = ["O"," "," "," ","O"," "," "," ","O"]
self.Evaluate(self.Value())
self.gameBoard = [" "," ","X"," ","X"," ","X"," "," "]
self.Evaluate(self.Value())
self.gameBoard = [" "," ","O"," ","O"," ","O"," "," "]
self.Evaluate(self.Value())
self.gameBoard = ["X","X","O","O","O","X","X","O","O"]
self.Evaluate(self.Value())
def Reset(self):
self.gameBoard = list(" "*9)
class NodeGame:
def __init__(self,gameBoard):
self.gameBoard = gameBoard
self.children = []
self.value = 0
def Possible(self):
number = 0
possible = []
for game in self.gameBoard:
if game == " ":
possible.append(number)
number +=1
return possible
def generateChildren(self, gameboard):
copy = NodeGame(gameboard)
copy.gameBoard = gameboard
self.children.append(copy)
copy.gameBoard = self.gameBoard
return copy
def count(self):
return self.gameBoard.count("X")
def Value(self):
for i in range(3):
if self.gameBoard[(i*3)] == self.gameBoard[(i*3)+1] == self.gameBoard[(i*3)+2] == "O":
return 1
for i in range(3):
if self.gameBoard[i] == self.gameBoard[i+3] == self.gameBoard[i+6] == "O":
return 1
if self.gameBoard[0] == self.gameBoard[4] == self.gameBoard[8] == "O":
return 1
elif self.gameBoard[2] == self.gameBoard[4] == self.gameBoard[6] == "O":
return 1
for i in range(3):
if self.gameBoard[(i*3)] == self.gameBoard[(i*3)+1] == self.gameBoard[(i*3)+2] == "X":
return -1
for i in range(3):
if self.gameBoard[i] == self.gameBoard[i+3] == self.gameBoard[i+6] == "X":
return -1
if self.gameBoard[0] == self.gameBoard[4] == self.gameBoard[8] == "X":
return -1
elif self.gameBoard[2] == self.gameBoard[4] == self.gameBoard[6] == "X":
return -1
elif " " not in self.gameBoard:
return 0
else:
return None
def show(self):
for i in range(3):
print(f"|{self.gameBoard[i*3]}|{self.gameBoard[(i*3)+1]}|{self.gameBoard[(i*3)+2]}|")
pass
def setValue(self,value):
self.value = value
return
def getValue(self):
return self.value
def Minimax(game, IsMaximizingPlayer):
score = game.Value()
if score == 1:
return score
elif score == -1:
return score
elif score == 0:
return score
if IsMaximizingPlayer:
value = -1000
for action in game.Possible():
board = game.gameBoard[:]
copy = NodeGame(board)
Result(int(action),copy, False)
value = max(value,Minimax(copy, False))
game.children.append(copy)
game.setValue(value)
return value
else:
value = 1000
for action in game.Possible():
board = game.gameBoard[:]
copy = NodeGame(board)
Result(int(action),copy, True)
value = min(value,Minimax(copy, True))
game.children.append(copy)
game.setValue(value)
return value
def TurnToString(value):
if value == -1:
return "You Win \n Try Again?"
elif value == 1:
return "You Lose \n Try Again?"
elif value == 0:
return "Draw \n Try Again?"
def AfterGame(board, window):
if sg.PopupYesNo(TurnToString(board.Value()),title="Game Over") == "Yes":
board.Reset()
for i in range (9):
window[f"{i}"].update(" ")
return True
else:
return False