-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai_player_undo.py
111 lines (95 loc) · 3.45 KB
/
ai_player_undo.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
from player import Player
from utils import longest
from mesure_time import timeit
import copy
import random
class AIPlayer(Player):
"""This player should implement a heuristic along with a min-max or alpha
beta search to """
def __init__(self):
super().__init__()
self.name = "Gravlax-AI"
def heuristique(self, board):
cols = board.num_cols
rows = board.num_rows
score=0
L = []
for i in range(cols):
L.append(board.getCol(i))
for j in range(rows):
L.append(board.getRow(j))
for k in range(cols):
L.append(board.getDiagonal(up=False, shift=k))
L.append(board.getDiagonal(up=True, shift=k))
for element in L:
long_elem,long_seq = longest(element)
score += (long_seq==4 and long_elem==self.color) - (long_seq==4 and long_elem==-self.color)
score += 0.1*(long_seq==3 and long_elem==self.color)- 0.1*(long_seq==3 and long_elem==-self.color)
return score
def win(self, board):
cols = board.num_cols
rows = board.num_rows
L = []
for i in range(cols):
L.append(board.getCol(i))
for j in range(rows):
L.append(board.getRow(j))
for k in range(cols):
L.append(board.getDiagonal(up=False, shift=k))
L.append(board.getDiagonal(up=True, shift=k))
for element in L:
long_elem, long_seq = longest(element)
if long_seq == 4:
return long_elem
return 0
def MaxScore(self, board, alpha, beta, profondeur, max_profondeur=6):
if profondeur == max_profondeur:
score = self.heuristique(board)
return score
winner = self.win(board)
if winner == self.color:
return 1000
if winner == -self.color:
return -1000
columns = board.getPossibleColumns()
profondeur += 1
for i in columns:
board.play(self.color, i)
alpha = max(alpha, self.MinScore(board, alpha, beta, profondeur))
board.unplay(i)
if alpha >= beta:
return beta
return alpha
def MinScore(self, board, alpha, beta, profondeur, max_profondeur=6):
if profondeur == max_profondeur:
score = self.heuristique(board)
return score
winner = self.win(board)
if winner == self.color:
return 1000
if winner == -self.color:
return -1000
columns = board.getPossibleColumns()
profondeur += 1
for i in columns:
board.play(-self.color, i)
beta = min(beta, self.MaxScore(board, alpha, beta, profondeur))
board.unplay(i)
if alpha >= beta:
return alpha
return beta
@timeit
def getColumn(self, board):
columns = board.getPossibleColumns()
L_scores = []
for i in columns:
board.play(self.color, i)
score = self.MinScore(board, -1000, 1000, 0)
board.unplay(i)
L_scores.append(score)
max_index = [i for i in range(len(L_scores)) if L_scores[i] == max(L_scores)]
if len(max_index) > 1:
#return columns[max_index[len(max_index)//2]]
return columns[random.choice(max_index)]
else:
return columns[max_index[0]]