-
Notifications
You must be signed in to change notification settings - Fork 1
/
extras.py
166 lines (125 loc) · 5.14 KB
/
extras.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
from copy import deepcopy
# This configuration was taken from the page we used to learn and practice chess.
# It shows the improved evaluations per position of a piece
# Link to the website is = https://www.freecodecamp.org/news/simple-chess-ai-step-by-step-1d55a9266977/
piece_square_tables = {
"king": [
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-2.0, -3.0, -3.0, -4.0, -4.0, -3.0, -3.0, -2.0],
[-1.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -1.0],
[2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0],
[2.0, 3.0, 1.0, 0.0, 0.0, 1.0, 3.0, 2.0]
],
"queen": [
[-2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0],
[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
[-1.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[-0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[0.0, 0.0,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, 0.0, 1.5, 2.0, 2.0, 1.5, 0.0, -3.0],
[-3.0, 0.5, 1.0, 1.5, 1.5, 1.0, 0.5, -3.0],
[-4.0, -2.0, 0.0, 0.5, 0.5, 0.0, -2.0, -4.0],
[-5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0]
],
"pawn": [
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
[1.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 1.0],
[0.5, 0.5, 1.0, 2.5, 2.5, 1.0, 0.5, 0.5],
[0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, 0.0],
[0.5, -0.5, -1.0, 0.0, 0.0, -1.0, -0.5, 0.5],
[0.5, 1.0, 1.0, -2.0, -2.0, 1.0, 1.0, 0.5],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
],
}
# invert a table by reversing then negating
def get_opposite_table(table):
table = deepcopy(table)
table.reverse()
for i in range(len(table)):
for j in range(len(table[i])):
table[i][j] = 0 - table[i][j]
return table
# get the value of a piece
def get_piece_value(_piece):
if _piece == ' ':
return 0
return _piece.points
# get the evaluations for a piece at a position according to our piece square tables
def get_piece_evaluation(_piece):
if _piece == ' ':
return 0
row = _piece.row
col = _piece.col
piece_table = piece_square_tables[_piece.name]
if _piece.team == 'white':
return piece_table[row][col]
# for black, the evaluations have to be reversed (board reversed)
else:
piece_table = get_opposite_table(piece_table)
return piece_table[row][col]
# get the total value of a board
def get_board_evaluation(_board):
val = 0
for row in _board.board:
for _piece in row:
val += get_piece_value(_piece) + get_piece_evaluation(_piece)
return val
# takes in string input and returns appropriate indexes
def parse_input(move: str) -> (int, int, int, int):
if len(move) != 4:
return None, None, None, None
move_piece = move[0] + move[1]
move_to = move[2] + move[3]
curr_row = int(move_piece[0]) - 1
curr_col = ord(move_piece[1]) - ord('a')
new_row = int(move_to[0]) - 1
new_col = ord(move_to[1]) - ord('a')
return curr_row, curr_col, new_row, new_col
# takes in string location and returns the index
def parse_position(pos: str) -> (int, int):
row = int(pos[0]) - 1
col = ord(pos[1]) - ord('a')
return row, col
# takes indexes and returns string input
def un_parse_input(curr_row: int, curr_col: int, new_row: int, new_col: int):
move_piece = str(curr_row + 1) + chr(curr_col + 97)
move_to = str(new_row + 1) + chr(new_col + 97)
return move_piece + move_to
def get_opposite_team(team):
if team == 'white':
return 'black'
if team == 'black':
return 'white'