-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_evaluator.py
109 lines (99 loc) · 3.83 KB
/
point_evaluator.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
import numpy as np
from chess import ChessGame
from chess_position import ChessPosition
from evaluator_MLP import MLPChessEvaluator
class PointEvaluator:
def evaluate(position : ChessPosition):
board = position.board
evaluation = 0
for i in range(0, 8):
for j in range(0, 8):
if board[i, j] == b'K':
evaluation += 1000
elif board[i, j] == b'Q':
evaluation += 9
elif board[i, j] == b'R':
evaluation += 5
elif board[i, j] == b'B':
evaluation += 3
elif board[i, j] == b'N':
evaluation += 3
elif board[i, j] == b'P':
evaluation += 1
elif board[i, j] == b'k':
evaluation -= 1000
elif board[i, j] == b'q':
evaluation -= 9
elif board[i, j] == b'r':
evaluation -= 5
elif board[i, j] == b'b':
evaluation -= 3
elif board[i, j] == b'n':
evaluation -= 3
elif board[i, j] == b'p':
evaluation -= 1
return evaluation
def rmini(position : ChessPosition, depth : int):
if depth > 0:
min = 2000
for move in ChessGame.possible_moves(position):
(a, b) = move
new_position = position.unrestricted_move(a, b)
new_position.white_turn = not new_position.white_turn
points = PointEvaluator.rmax(new_position, depth - 1)
if points < min:
min = points
return min
else:
return PointEvaluator.evaluate(position)
def rmax(position : ChessPosition, depth : int):
if depth > 0:
max = 0
for move in ChessGame.possible_moves(position):
(a, b) = move
new_position = position.unrestricted_move(a, b)
new_position.white_turn = not new_position.white_turn
points = PointEvaluator.rmini(new_position, depth - 1)
if points > max:
max = points
return max
else:
return PointEvaluator.evaluate(position)
def white_best_move(position : ChessPosition, depth : int):
best_move = None
max = 0
for move in ChessGame.possible_moves(position):
(a, b) = move
new_position = position.unrestricted_move(a, b)
new_position.white_turn = not new_position.white_turn
points = PointEvaluator.rmax(new_position, depth - 1)
if points > max:
max = points
best_move = move
return best_move
def black_best_move(position : ChessPosition, depth : int):
best_move = None
max = 0
for move in ChessGame.possible_moves(position):
(a, b) = move
new_position = position.unrestricted_move(a, b)
new_position.white_turn = not new_position.white_turn
points = PointEvaluator.rmax(new_position, depth - 1)
if points > max:
max = points
best_move = move
return best_move
board = np.matrix([
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
], dtype=np.character)
chessPosition = ChessPosition()
chessPosition.board = board
eva = PointEvaluator.white_best_move(chessPosition, 4)
print(eva)