-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathataxx_tester.py
448 lines (371 loc) · 12.6 KB
/
ataxx_tester.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import numpy
import copy
import random
SIZE = 600
class gamestate:
N = numpy.unsignedinteger
sq = numpy.unsignedinteger
tabuleiro = []
tipo = 3
ai1diff = numpy.unsignedinteger
ai2diff = numpy.unsignedinteger
nMovs = 1
vencedor = 0
class movimento:
xi = 0
yi = 0
yf = 0
xf = 0
jog = 0
tipo = 0
class totalmov:
xi = 0
yi = 0
yf = 0
xf = 0
tipo = 0
class bestmov:
xi = 0
yi = 0
yf = 0
xf = 0
class minmaxmov():
xi = 0
yi = 0
yf = 0
xf = 0
min = 0
max = 0
class save:
game = []
def escolhe_tabul():
print("Tabuleiros:")
print("1) Original")
print("2) Sem paredes")
print("3) Circular")
print("4) Coração")
print("5) Alvo")
print("6) Xadrez")
print("7) Foxy")
numtabul = input()
tabuleiro = "tabuleiros/tab"+numtabul+".txt"
return tabuleiro
def carrega_tabul(ficheiro):
f = open(ficheiro)
gamestate.N = int(f.readline())
gamestate.sq = SIZE / gamestate.N
tabuleiro = []
for i in range(gamestate.N):
tabuleiro.append(list(map(int, f.readline().split())))
f.close()
gamestate.tabuleiro = tabuleiro
def dificuldade():
print("Algoritmo da AI 1:")
print("1) Random")
print("2) Greedy")
print("3) Center Control")
print("4) Minmax")
gamestate.ai1diff = int(input())
print("Algoritmo da AI 2:")
print("1) Random")
print("2) Greedy")
print("3) Center Control")
print("4) Minmax")
gamestate.ai2diff = int(input())
return
def copia():
save.game = copy.deepcopy(gamestate.tabuleiro)
def restaura():
gamestate.tabuleiro = save.game
def troca_jog(jog):
if jog == 1:
return 2
else:
return 1
def comer():
dx = -1
dy = -1
for dx in range(dx, 2):
for dy in range(dy, 2):
try:
if movimento.yf + dy == -1 and movimento.xf + dx == -1:
if gamestate.tabuleiro[0][0] == troca_jog(movimento.jog):
gamestate.tabuleiro[movimento.yf + dy +
1][movimento.xf + dx+1] = movimento.jog
elif movimento.yf + dy == -1:
if gamestate.tabuleiro[0][movimento.xf + dx] == troca_jog(movimento.jog):
gamestate.tabuleiro[movimento.yf + dy +
1][movimento.xf + dx] = movimento.jog
elif movimento.xf + dx == -1:
if gamestate.tabuleiro[movimento.yf + dy][0] == troca_jog(movimento.jog):
# Trabalho Realizado por Pedro Sousa e Inês Cardoso
gamestate.tabuleiro[movimento.yf + dy][movimento.xf +
dx+1] = movimento.jog
elif gamestate.tabuleiro[movimento.yf + dy][movimento.xf + dx] == troca_jog(movimento.jog):
gamestate.tabuleiro[movimento.yf +
dy][movimento.xf + dx] = movimento.jog
except IndexError:
pass
dy = -1
def executa_movimento():
gamestate.tabuleiro[movimento.yf][movimento.xf] = movimento.jog
if movimento.tipo == 1:
gamestate.tabuleiro[movimento.yi][movimento.xi] = 0
comer()
def adjacente(dist, classe):
return(
abs(classe.xi - classe.xf) == dist and abs(classe.yi - classe.yf) <= dist or
abs(classe.yi - classe.yf) == dist and abs(classe.xi - classe.xf) <= dist)
def dentro(x, y):
return (x >= 0 and x <= gamestate.N-1 and y >= 0 and y <= gamestate.N-1)
def movimento_valido(classe):
if abs(classe.yf - classe.yi) == 2 and abs(classe.xf - classe.xi) == 1 or abs(classe.xf - classe.xi) == 2 and abs(classe.yf - classe.yi) == 1:
return False
if not dentro(classe.xi, classe.yi) or not dentro(classe.xf, classe.yf):
return False
if gamestate.tabuleiro[classe.yi][classe.xi] == movimento.jog and gamestate.tabuleiro[classe.yf][classe.xf] == 0 and adjacente(1, classe):
classe.tipo = 0
return True
if gamestate.tabuleiro[classe.yi][classe.xi] == movimento.jog and gamestate.tabuleiro[classe.yf][classe.xf] == 0 and adjacente(2, classe):
classe.tipo = 1
return True
def tipo_jogo():
print("Jogo de Ataxx")
print("Escolha o modo de jogo:")
print("1 - Humano vs. Humano ")
print("2 - Humano vs. Computador ")
print("3 - Computador vs. Computador ")
tipo = input()
return tipo
def jogadas_validas_total(jog):
nmovs = 0
for y in range(gamestate.N):
for x in range(gamestate.N):
if gamestate.tabuleiro[y][x] == jog:
for k in range(gamestate.N):
for l in range(gamestate.N):
movimento.jog = jog
totalmov.yi = y
totalmov.xi = x
totalmov.yf = k
totalmov.xf = l
if movimento_valido(totalmov):
nmovs += 1
return nmovs
def conta_pecas(jog):
pecas = 0
for i in range(gamestate.N):
for j in range(gamestate.N):
if gamestate.tabuleiro[i][j] == jog:
pecas += 1
return pecas
def quad_validos():
nmovs = 0
for i in range(gamestate.N):
for j in range(gamestate.N):
if gamestate.tabuleiro[i][j] == 0:
nmovs += 1
return nmovs
def fim_jogo():
n = quad_validos()
if conta_pecas(1) == 0 or conta_pecas(2) == 0:
if conta_pecas(1) == 0:
gamestate.vencedor = 1
return -1
else:
gamestate.vencedor = -1
return -1
if n == 0:
if (conta_pecas(1) - conta_pecas(2) >= 0):
gamestate.vencedor = -1
return -1
if (conta_pecas(1) - conta_pecas(2) < 0):
gamestate.vencedor = 1
return -1
else:
gamestate.vencedor = 0
return -1
else:
return 0
def finaliza():
if gamestate.vencedor != 0:
if gamestate.vencedor == -1:
resultados.vermelho += 1
else:
resultados.azul += 1
else:
resultados.empate += 1
resultados.jogadas.append(gamestate.nMovs)
def jogada_PC():
bestav = -1000
for yi in range(gamestate.N):
for xi in range(gamestate.N):
if gamestate.tabuleiro[yi][xi] == movimento.jog:
for k in range(0, gamestate.N):
for l in range(0, gamestate.N):
movimento.yi = yi
movimento.xi = xi
movimento.yf = l
movimento.xf = k
if movimento_valido(movimento):
copia()
executa_movimento()
if gamestate.nMovs % 2 != 1:
av = avalia(gamestate.ai2diff)
else:
av = avalia(gamestate.ai1diff)
restaura()
if av >= bestav:
bestav = av
bestmov.yi = movimento.yi
bestmov.xi = movimento.xi
bestmov.yf = movimento.yf
bestmov.xf = movimento.xf
movimento.yi = bestmov.yi
movimento.xi = bestmov.xi
movimento.yf = bestmov.yf
movimento.xf = bestmov.xf
if movimento_valido(movimento):
executa_movimento()
def avalia(tipo):
tipo = int(tipo)
score = 0
if tipo == 1:
score = algo_random()
elif tipo == 2:
score = algo_greedy()
elif tipo == 3:
score = algo_centercontrol()
elif tipo == 4:
if gamestate.nMovs % 2 != 1:
movimento.jog = 1
minmaxmov.min = 1
minmaxmov.max = 2
else:
movimento.jog = 2
minmaxmov.min = 2
minmaxmov.max = 1
minmaxmov.yi = movimento.yi
minmaxmov.xi = movimento.xi
minmaxmov.yf = movimento.yf
minmaxmov.xf = movimento.xf
alfa = -100000
beta = 100000
score = algo_minmax(0, True, alfa, beta)
movimento.yi = minmaxmov.yi
movimento.xi = minmaxmov.xi
movimento.yf = minmaxmov.yf
movimento.xf = minmaxmov.xf
elif tipo > 4:
score = random.random()
return score
def algo_random():
return (random.randint(1, 10))
def algo_greedy():
salt = random.random()
return (conta_pecas(movimento.jog) - conta_pecas(troca_jog(movimento.jog))+salt)
def algo_centercontrol():
salt = random.random()
yc = abs(gamestate.N / 2 - movimento.yf)
xc = abs(gamestate.N / 2 - movimento. xf)
score = 100 - (yc + xc) + 2*conta_pecas(movimento.jog) + salt
return score
def algo_minmax(depth, minimizer, alfa, beta):
if depth == 3 or fim_jogo == -1:
return (algo_greedy() * (-1))
if minimizer:
movimento.jog = minmaxmov.min
value = +1000
for yi in range(gamestate.N):
for xi in range(gamestate.N):
if gamestate.tabuleiro[yi][xi] == movimento.jog:
for k in range(0, gamestate.N):
for l in range(0, gamestate.N):
movimento.yi = yi
movimento.xi = xi
movimento.yf = l
movimento.xf = k
if movimento_valido(movimento):
temp = copy.deepcopy(gamestate.tabuleiro)
executa_movimento()
evaluation = algo_minmax(
depth + 1, False, alfa, beta)
gamestate.tabuleiro = temp
value = min(value, evaluation)
beta = min(beta, evaluation)
if beta <= alfa:
break
movimento.jog = minmaxmov.max
return value
else:
movimento.jog = minmaxmov.max
value = -1000
for yi in range(gamestate.N):
for xi in range(gamestate.N):
if gamestate.tabuleiro[yi][xi] == movimento.jog:
for k in range(0, gamestate.N):
for l in range(0, gamestate.N):
movimento.yi = yi
movimento.xi = xi
movimento.yf = l
movimento.xf = k
if movimento_valido(movimento):
temp = copy.deepcopy(gamestate.tabuleiro)
executa_movimento()
evaluation = algo_minmax(
depth + 1, True, alfa, beta)
gamestate.tabuleiro = temp
value = max(value, evaluation)
alfa = max(alfa, evaluation)
if beta <= alfa:
break
movimento.jog = minmaxmov.min
return value
def main():
fim = 0
movimento.jog = 1
carrega_tabul(tabuleiro)
running = True
while running:
if jogadas_validas_total(movimento.jog) == 0:
gamestate.nMovs += 1
movimento.jog = troca_jog(movimento.jog)
if gamestate.nMovs % 2 != 1:
jogada_PC()
gamestate.nMovs += 1
movimento.jog = troca_jog(movimento.jog)
else:
jogada_PC()
gamestate.nMovs += 1
movimento.jog = troca_jog(movimento.jog)
fim = fim_jogo()
if fim == -1:
resultados.diff.append(conta_pecas(1) - conta_pecas(2))
finaliza()
running = False
class resultados:
vermelho = 0
azul = 0
empate = 0
jogadas = []
diff = []
media = 0
def calculos():
resultados.media = numpy.median(resultados.diff)
tabuleiro = escolhe_tabul()
carrega_tabul(tabuleiro)
dificuldade()
total = int(input("Quantos testes quer realizar?: "))
for i in range(total):
main()
print("Teste", i + 1, "realizado.")
calculos()
print("\n")
print("Vitórias do Vermelho: ", resultados.vermelho)
print("Vitórias do Azul: ", resultados.azul)
print("Empates: ", resultados.empate)
print("Diferença de peças média: ", resultados.media)
print("\n")
print("Trabalho realizado por:")
print("Pedro Sousa")
print("Inês Cardoso")