-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.py
More file actions
91 lines (76 loc) · 2.77 KB
/
ai.py
File metadata and controls
91 lines (76 loc) · 2.77 KB
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
import math
import chess
def make_move(board: chess.Board, depth: int):
"""Does the best possible move for the AI"""
move = get_move(board, depth)
board.move(move.piece, move.end)
def get_move(board: chess.Board, depth: int):
"""Returns the best move for the AI"""
zobristTable = {}
def minimax(color: bool, depth: int, alpha: float = -math.inf, beta: float = math.inf):
zobristKey = board.get_zobrist_key()
if zobristTable.get(zobristKey) != None:
return zobristTable[zobristKey], board.lastMove
if depth == 0 or board.result != None:
return evaluate(board), board.lastMove
if color == True:
maxEvaluation = -math.inf
else:
maxEvaluation = math.inf
bestMove = None
previousMove = board.lastMove
# Loop through all the possible moves
for piece, positions in board.get_valid_moves(color).items():
for position in positions:
move = board.move(piece, position)
evaluation = minimax(not color, depth - 1, alpha, beta)[0]
board.revert(move, previousMove)
zobristTable[zobristKey] = evaluation
# White
if color == True:
if evaluation > maxEvaluation:
maxEvaluation = evaluation
bestMove = move
alpha = max(alpha, maxEvaluation)
if alpha >= beta:
break
# Black
else:
if evaluation < maxEvaluation:
maxEvaluation = evaluation
bestMove = move
beta = min(beta, maxEvaluation)
if beta <= alpha:
break
return maxEvaluation, bestMove
if not board.lastMove:
color = True
else:
color = not board.lastMove.piece.color
# Returns the best found move
return minimax(color, depth)[1]
def evaluate(board: chess.Board) -> float:
"""Returns the evaluation of the board for the AI"""
# The game is allready over
if board.result != None:
# White wins
if board.result == 0:
return 104
# Black wins
elif board.result == 1:
return -104
# Draw
else:
return 0
# The game is still going
else:
# We sum up all the points of the pieces on the board
evaluation = 0
for piece in board.get_pieces():
# White pieces are positive
if piece.color:
evaluation += piece.points
# Black pieces are negative
else:
evaluation -= piece.points
return evaluation