-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
186 lines (165 loc) · 6.18 KB
/
main.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
from Game import Game, Azione
from appJar import gui
import atlastk as Atlas
def alpha_beta(game, state):
def max_value(state, alpha, beta):
if game.terminal_test(state):
# se in state il gioco e' concluso restituisci il risultato
return game.utility(state)
v = - float('inf') # v e' inizializzato a - infinito
for a in game.actions(state):
v = max(v, min_value(game.result(state, a), alpha, beta))
if v >= beta: # taglio beta
#print('MAX: stato %s - utilita\' %s - TAGLIO BETA (alpha = %s, beta = %s)' % (state, v, alpha, beta))
return v
alpha = max(alpha, v) # aggiorna il migliore per MAX
#print('MAX: stato %s - utilita\' %s - (alpha = %s, beta = %s)' % (state, v, alpha, beta))
return v
def min_value(state, alpha, beta):
if game.terminal_test(state):
# se in state il gioco e' concluso restituisci il risultato
return game.utility(state)
v = float('inf') # v e' inizializzato a + infinito
for a in game.actions(state):
v = min(v, max_value(game.result(state, a), alpha, beta))
if v <= alpha: # taglio alpha
#print('MIN: stato %s - utilita\' %s - TAGLIO ALPHA (alpha = %s, beta = %s)' % (state, v, alpha, beta))
return v
beta = min(beta, v) # aggiorna il migliore per MIN
#print('MIN: stato %s - utilita\' %s - (alpha = %s, beta = %s)' % (state, v, alpha, beta))
return v
# inizializza alpha e beta
alpha = - float('inf')
beta = float('inf')
game.print()
best_action = None
# esegue un ciclo esterno di max_value "controllato"
# memorizzando la mossa migliore
for a in game.actions(state):
v = min_value(game.result(state, a), alpha, beta)
if v > alpha:
# se lo score della mossa a e' il migliore fino a qui
# allora memorizza la mossa a
best_action = a
alpha = v
#print("Lazione selezionata e "+str(best_action.x)+","+str(best_action.y))
return best_action
def minimax_decision(game, state):
def max_value(state):
if game.terminal_test(state):
# se in state il gioco e' concluso restituisci il risultato
val = game.utility(state)
# game.print2(state)
# print(val)
return val
v = - float('inf') # v e' inizializzato a - infinito
for a in game.actions(state):
v = max(v, min_value(game.result(state, a)))
#print('MAX: stato {} - utilita\' {}'.format(state, v))
return v
def min_value(state):
if game.terminal_test(state):
# se in state il gioco e' concluso restituisci il risultato
val = game.utility(state)
# game.print2(state)
# print(val)
return val
v = float('inf') # v e' inizializzato a + infinito
for a in game.actions(state):
v = min(v, max_value(game.result(state, a)))
print('MIN: stato {} - utilita\' {}'.format(state, v))
return v
best_action = max(game.actions(state), key=lambda x: min_value(game.result(state, x)))
# best_action e' l'argomento (l'azione) che massimizza l'output di min_value
print("L\'azione selezionata e\' {}".format(best_action))
return best_action
def pressButton(button):
coord = str(button).split(".")
x = int(coord[0])
y = int(coord[1])
a = Azione(x, y, "o")
game.mettiSimbolo(a)
app.setButton(button, "o")
if game.terminal_test(game.matrix):
if app.questionBox("E' finito", "Il gioco è terminato vuoi rigiocare?", parent=None):
game.nuovaPartita()
pulisciBottoni()
else:
aia = alpha_beta(game, game.matrix)
game.mettiSimbolo(aia)
nuovoID = str(aia.x) + "." + str(aia.y)
app.setButton(nuovoID, "x")
if game.terminal_test(game.matrix):
if app.questionBox("E' finito", "Il gioco è terminato vuoi rigiocare?", parent=None):
game.nuovaPartita()
pulisciBottoni()
game = Game()
app = gui("TicTacToe AI", "500x500")
app.setBg("LightYellow")
app.setSticky("news")
app.setExpand("both")
app.setFont(40)
app.addButton("0.0", pressButton, 0, 0)
app.addButton("0.1", pressButton, 0, 1)
app.addButton("0.2", pressButton, 0, 2)
app.addButton("1.0", pressButton, 1, 0)
app.addButton("1.1", pressButton, 1, 1)
app.addButton("1.2", pressButton, 1, 2)
app.addButton("2.0", pressButton, 2, 0)
app.addButton("2.1", pressButton, 2, 1)
app.addButton("2.2", pressButton, 2, 2)
app.setButton("0.0", " ")
app.setButton("0.1", " ")
app.setButton("0.2", " ")
app.setButton("1.0", " ")
app.setButton("1.1", " ")
app.setButton("1.2", " ")
app.setButton("2.0", " ")
app.setButton("2.1", " ")
app.setButton("2.2", " ")
def pulisciBottoni():
app.setButton("0.0", " ")
app.setButton("0.1", " ")
app.setButton("0.2", " ")
app.setButton("1.0", " ")
app.setButton("1.1", " ")
app.setButton("1.2", " ")
app.setButton("2.0", " ")
app.setButton("2.1", " ")
app.setButton("2.2", " ")
# add labels & entries
# in the correct row & column
if __name__ == '__main__':
app.go()
"""a = Azione(0, 0, "x")
game.mettiSimbolo(a)
a = Azione(0, 1, "o")
game.mettiSimbolo(a)
# a = Azione(0, 2, "x")
# game.mettiSimbolo(a)
a = Azione(1, 1, "o")
game.mettiSimbolo(a)
# a = Azione(2, 1, "o")
# game.mettiSimbolo(a)
# a = Azione(2, 2, "o")
# game.mettiSimbolo(a)
game.print()
# print(game.utility(game.matrix))
aia = minimax_decision(game, game.matrix)
game.mettiSimbolo(aia)
print(str(aia.x) + " " + str(aia.y) + " " + aia.c)
game.print()"""
"""while not game.terminal_test(game.matrix):
num1 = int(input())
num2 = int(input())
a = Azione(num1, num2, "o")
game.mettiSimbolo(a)
game.print()
if game.terminal_test(game.matrix):
break
aia = alpha_beta(game, game.matrix)
game.mettiSimbolo(aia)
print(str(aia.x) + " " + str(aia.y) + " " + aia.c)
game.print()
if game.terminal_test(game.matrix):
break"""