-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminmax.py
executable file
·162 lines (128 loc) · 5.46 KB
/
minmax.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
from math import log
import tkinter as tk
import numpy as np
from json import loads
from timeit import timeit
import math
from functools import partial
from pynput import keyboard
from pynput.keyboard import Key, KeyCode
from enums.type_attack import TypeAttack
from copy import deepcopy, copy
from main import Game
import random
class Minmax():
def avance(self):
game = Game()
for i in range(1000):
coups = game.get_game_available_moves()
if len(coups) == 0 or game.winner != -1:
break
coup_choisi = None
if game.player_turn == 0:
for coup in coups:
(y,x),(dy, dx) = coup
if dy < 0:
coup_choisi = coup
break
if game.player_turn == 1:
for coup in coups:
(y, x),(dy, dx) = coup
if dy > 0:
coup_choisi = coup
break
if not coup_choisi:
break
game.play_move(coup_choisi)
game.show_game()
def play_game(self) -> None:
self.game = Game()
for i in range(2000):
#print(i)
coups = self.game.get_game_available_moves()
if len(coups) == 0:
break
#game.play_move(coups[random.randint(0, len(coups)-1)])
points, moves = self.min_max(self.game, 1)
best_move = moves[-1]
self.game.play_move(best_move)
def dif(self, tab, index):
return tab[index] - tab[1-index]
def basic_evaluate(self, game: Game):
rates = game.piece_rate()
progress = [game.progress(0), game.progress(1)]
return self.dif(rates, 0)*1000 + self.dif(progress, 0) #le second paramètre de dif servait avant
def evaluate(self, game:Game, eval_function, player_turn):
multiplier = 1 if player_turn == 0 else -1
if game.winner != -1:
winner = 1 if player_turn == game.winner else -1
print("Detects win")
return winner * 100000000
return eval_function(game) * multiplier
#min max
def minmax_min(self, game:Game, root_player_turn, eval_function, profondeur, alpha, beta):
if profondeur == 0 :
eval = self.evaluate(game, eval_function, root_player_turn)
return (eval, [])
moves = game.get_game_available_moves()
if moves == []:
eval = self.evaluate(game, eval_function, root_player_turn)
return (eval, [])
worst_sub_point, worst_moves_to_go_sub_game = None, None
for index, m in enumerate(moves):
sub_game = deepcopy(game)
sub_game.play_move(moves[index])
sub_point, moves_to_go_sub_game = self.minmax_max(sub_game, root_player_turn, eval_function, profondeur-1, alpha, beta)
if sub_point <= alpha: #elagage alpha
return (-math.inf, []) # pour ne pas qu'il soit considéré par le max
moves_to_go_sub_game.append(m)
if index == 0 or worst_sub_point > sub_point:
worst_sub_point = sub_point
beta = min(beta, sub_point)
#on pourrait le modifier avant, ça revient au même
worst_moves_to_go_sub_game = moves_to_go_sub_game
return worst_sub_point, worst_moves_to_go_sub_game
def minmax_max(self, game:Game, root_player_turn, eval_function, profondeur, alpha, beta):
if profondeur == 0 :
eval = self.evaluate(game, eval_function, root_player_turn)
return (eval, []) #un max ne modifie que beta
moves = game.get_game_available_moves()
if moves == []:
eval = self.evaluate(game, eval_function, root_player_turn)
return (eval, [])
best_sub_point, best_moves_to_go_sub_game = None, None
for index in range(len(moves)):
sub_game = deepcopy(game)
sub_game.play_move(moves[index])
sub_point, moves_to_go_sub_game = self.minmax_min(sub_game, root_player_turn, eval_function, profondeur-1, alpha, beta)
if sub_point >= beta:
return (math.inf, [])
moves_to_go_sub_game.append(moves[index])
#print(sub_point, best_sub_point)
if index == 0 or best_sub_point < sub_point:
best_sub_point = sub_point
alpha = max(alpha, sub_point) #vient d'un min donc alpha est modifié
#on pourrait modifier le alpha avant
best_moves_to_go_sub_game = moves_to_go_sub_game
return best_sub_point, best_moves_to_go_sub_game
def min_max(self, game:Game, profondeur, eval_function = None):
if eval_function == None:
eval_function = self.basic_evaluate
points, moves = self.minmax_max(game, game.player_turn, eval_function, profondeur, -math.inf, math.inf)
return (points, moves, moves[-1])
if __name__ == "__main__":
minmax = Minmax()
game = Game()
for i in range(20):
_,_, bm = minmax.min_max(game, 1, eval_function=None)
game.play_move(bm)
print("ah")
print(game.piece_number())
print(game.piece_rate())
print(game.isobarycenter(0))
print(game.isobarycenter(1))
print(game.dispersion(0))
print(game.dispersion(1))
print(game.progress(0))
print(game.progress(1))
game.show_game()