summaryrefslogtreecommitdiff
path: root/chessconf.py
blob: 6b1d2966c11efac85eaaa92f600e1d6819812a0e (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# _________ .__                         _________                _____.__        
# \_   ___ \|  |__   ____   ______ _____\_   ___ \  ____   _____/ ____\__| ____  
# /    \  \/|  |  \_/ __ \ /  ___//  ___/    \  \/ /  _ \ /    \   __\|  |/ ___\ 
# \     \___|   Y  \  ___/ \___ \ \___ \\     \___(  <_> )   |  \  |  |  / /_/  >
#  \______  /___|  /\___  >____  >____  >\______  /\____/|___|  /__|  |__\___  / 
#         \/     \/     \/     \/     \/        \/            \/        /_____/  
'''
cool ascii >                http://patorjk.com/software/taag
chess-ICON >                https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode
more cool unicode ICON >    https://www.unicode.org/charts/
color codes cheatsheet >    https://gist.github.com/ConnerWill/d4b6c776b509add763e17f9f113fd25b'''

import typing

# ------------ BOARD -----------------
'''
black >   '\x1b[30m'
red >     '\x1b[31m'
green >   '\x1b[32m'
yellow >  '\x1b[33m'
blue >    '\x1b[34m'
magenta > '\x1b[35m'
cyan >    '\x1b[36m'
white >   '\x1b[37m' '''

WHITECOLOR: typing.Final[str] = '\x1b[36m'
BLACKCOLOR: typing.Final[str] = '\x1b[31m'
SPACECOLOR: typing.Final[str] = ''

WHITE_KING_ICON: typing.Final[str] = '\u265A'
WHITE_QUEEN_ICON: typing.Final[str] = '\u265B'
WHITE_ROOK_ICON: typing.Final[str] = '\u265C'
WHITE_BISHOP_ICON: typing.Final[str] = '\u265D'
WHITE_KNIGHT_ICON: typing.Final[str] = '\u265E'
WHITE_PAWN_ICON: typing.Final[str] = '\u265F'

SPACE_ICON: str = ' '

BLACK_KING_ICON: str = '\u265A'
BLACK_QUEEN_ICON: str = '\u265B'
BLACK_ROOK_ICON: str = '\u265C'
BLACK_BISHOP_ICON: str = '\u265D'
BLACK_KNIGHT_ICON: str = '\u265E'
BLACK_PAWN_ICON: str = '\u265F'

STOCKFISH_PATH: str = '/opt/homebrew/bin/stockfish'     # path to installed stockfish bin
stockfish_depth: int = 10    # default calculation depth for stockfish engine
minimax_depth: int = 4       # default calculation depth for minimax algorithm

# ------------ PARSING --------------

PIECE_MAP: typing.Final[dict[str, str]] = {                           # add other languages ('Springer': 'N')
    'rook': 'R', 'Rook': 'R', 'ROOK': 'R', 'Turm': 'R', 'T': 'R',
    'knight': 'N', 'Knight': 'N', 'KNIGHT': 'N', 'Springer': 'N', 'S': 'N', 
    'bishop': 'B', 'Bishop': 'B', 'BISHOP': 'B', 'L': 'B', 'Läufer': 'B',
    'queen': 'Q', 'Queen': 'Q', 'QUEEN': 'Q', 'Dame': 'Q', 'D': 'Q',
    'king': 'K', 'King': 'K', 'KING': 'K',
    'pawn': 'P', 'Pawn': 'P', 'PAWN': 'P'}

# value = True if capture else False
ACTION_MAP: typing.Final[dict[str, bool]] = {' captures ': True, ' takes ': True, ' goes to ': False, ' goes ': False, ' to ': False, ' x ': True, ' - ': False, ' / ': False, 'x': True, '-': False, '/': False, '_': False, ' ': False}    # put longer strings to the front so they match before ' ' does!

PROMOTION_MAP: typing.Final[dict[str, str]] = {
    ' queen': 'Q', ' = q': 'Q', ' =q': 'Q', ' /q': 'Q', '=q': 'Q', '/q': 'Q', ' q': 'Q', 'q': 'Q', ' QUEEN': 'Q', ' Queen': 'Q', ' = Q': 'Q', ' =Q': 'Q', ' /Q': 'Q', '=Q': 'Q', '/Q': 'Q', ' Q': 'Q', 'Q': 'Q', 
    ' rook': 'R', ' = r': 'R', ' =r': 'R', ' /r': 'R', '=r': 'R', '/r': 'R', ' r': 'R', 'r': 'R', ' ROOK': 'R', ' Rook': 'R', ' = R': 'R', ' =R': 'R', ' /R': 'R', '=R': 'R', '/R': 'R', ' R': 'R', 'R': 'R', 
    ' knight': 'N', ' = n': 'N', ' =n': 'N', ' /n': 'N', '=n': 'N', '/n': 'N', ' n': 'N', 'n': 'N', ' KNIGHT': 'N', ' Knight': 'N', ' = N': 'N', ' =N': 'N', ' /N': 'N', '=N': 'N', '/N': 'N', ' N': 'N', 'N': 'N', 
    ' bishop': 'B', ' = b': 'B', ' =b': 'B', ' /b': 'B', '=b': 'B', '/b': 'B', ' b': 'B', 'b': 'B', ' BISHOP': 'B', ' Bishop': 'B', ' = B': 'B', ' =B': 'B', ' /B': 'B', '=B': 'B', '/B': 'B', ' B': 'B', 'B': 'B'}

# value = True if short castling else False
CASTLING_MAP: typing.Final[dict[str, bool]] = {
    'short castling': True, 'castle short': True, 'kingside castling': True, 'castle kingside': True, 'O-O': True, '0-0': True, '00': True, 'o-o': True, 'oo': True, 'OO': True, 
    'long castling': False, 'castle long': False, 'queenside castling': False, 'castle queenside': False, 'O-O-O': False, '0-0-0': False, '000': False, 'o-o-o': False, 'ooo': False, 'OOO': False}

# value = True if check else False (checkmate)
CHECK_OR_CHECKMATE_MAP: typing.Final[dict[str, str]] = {
    ' with check': '+', ' CHECK': '+', ' check': '+', ' +': '+', '+': '+', 
    ' with checkmate': '#', ' CHECKMATE': '#', ' checkmate': '#', ' ++': '#', ' #': '#', '++': '#', '#': '#'}

# ------------ EVAL FUNCTION --------------

PIECEVALUE: typing.Final[dict[str, int]] = {'K': 20_000, 'Q': 900, 'R': 500, 'B': 330, 'N': 320, 'P': 100,'k': -20_000, 'q': -900, 'r': -500, 'b': -330, 'n': -320, 'p': -100, '.': 0}

postables_struct = dict[str, tuple[
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int], 
    tuple[int, int, int, int, int, int, int, int]]]

POSTABLES: typing.Final[postables_struct] = {

    'R': ((0, 0, 0, 0, 0, 0, 0, 0), 
          (5, 10, 10, 10, 10, 10, 10, 5), 
          (-5, 0, 0, 0, 0, 0, 0, -5), 
          (-5, 0, 0, 0, 0, 0, 0, -5), 
          (-5, 0, 0, 0, 0, 0, 0, -5), 
          (-5, 0, 0, 0, 0, 0, 0, -5), 
          (-5, 0, 0, 0, 0, 0, 0, -5), 
          (0, 0, 0, 5, 5, 0, 0, 0)), 

    'N': ((-50, -40, -30, -30, -30, -30, -40, -50), 
          (-40, -20, 0, 0, 0, 0, -20, -40), 
          (-30, 0, 10, 15, 15, 10, 0, -30), 
          (-30, 5, 15, 20, 20, 15, 5, -30), 
          (-30, 0, 15, 20, 20, 15, 0, -30), 
          (-30, 5, 10, 15, 15, 10, 5, -30), 
          (-40, -20, 0, 5, 5, 0, -20, -40), 
          (-50, -40, -30, -30, -30, -30, -40, -50)), 

    'B': ((-20, -10, -10, -10, -10, -10, -10, -20), 
          (-10, 0, 0, 0, 0, 0, 0, -10), 
          (-10, 0, 5, 10, 10, 5, 0, -10), 
          (-10, 5, 5, 10, 10, 5, 5, -10), 
          (-10, 0, 10, 10, 10, 10, 0, -10), 
          (-10, 10, 10, 10, 10, 10, 10, -10), 
          (-10, 5, 0, 0, 0, 0, 5, -10), 
          (-20, -10, -10, -10, -10, -10, -10, -20)), 

    'Q': ((-20, -10, -10, -5, -5, -10, -10, -20), 
          (-10, 0, 0, 0, 0, 0, 0, -10), 
          (-10, 0, 5, 5, 5, 5, 0, -10), 
          (-5, 0, 5, 5, 5, 5, 0, -5), 
          (0, 0, 5, 5, 5, 5, 0, -5), 
          (-10, 5, 5, 5, 5, 5, 0, -10), 
          (-10, 0, 5, 0, 0, 0, 0, -10), 
          (-20, -10, -10, -5, -5, -10, -10, -20)), 

    'P': ((0, 0, 0, 0, 0, 0, 0, 0), 
          (50, 50, 50, 50, 50, 50, 50, 50), 
          (10, 10, 20, 30, 30, 20, 10, 10), 
          (5, 5, 10, 25, 25, 10, 5, 5), 
          (0, 0, 0, 20, 20, 0, 0, 0), 
          (5, -5, -10, 0, 0, -10, -5, 5), 
          (5, 10, 10, -20, -20, 10, 10, 5), 
          (0, 0, 0, 0, 0, 0, 0, 0)),

    'p': ((0, 0, 0, 0, 0, 0, 0, 0), 
          (-5, -10, -10, 20, 20, -10, -10, -5), 
          (-5, 5, 10, 0, 0, 10, 5, -5), 
          (0, 0, 0, -20, -20, 0, 0, 0), 
          (-5, -5, -10, -25, -25, -10, -5, -5), 
          (-10, -10, -20, -30, -30, -20, -10, -10), 
          (-50, -50, -50, -50, -50, -50, -50, -50), 
          (0, 0, 0, 0, 0, 0, 0, 0)), 

    'K_end': ((-50, -40, -30, -20, -20, -30, -40, -50),         # end game
              (-30, -20, -10, 0, 0, -10, -20, -30), 
              (-30, -10, 20, 30, 30, 20, -10, -30), 
              (-30, -10, 30, 40, 40, 30, -10, -30), 
              (-30, -10, 30, 40, 40, 30, -10, -30), 
              (-30, -10, 20, 30, 30, 20, -10, -30), 
              (-30, -30, 0, 0, 0, 0, -30, -30), 
              (-50, -30, -30, -30, -30, -30, -30, -50)), 

    'K_middle': ((-30, -40, -40, -50, -50, -40, -40, -30),      # middle game
                 (-30, -40, -40, -50, -50, -40, -40, -30), 
                 (-30, -40, -40, -50, -50, -40, -40, -30), 
                 (-30, -40, -40, -50, -50, -40, -40, -30), 
                 (-20, -30, -30, -40, -40, -30, -30, -20), 
                 (-10, -20, -20, -20, -20, -20, -20, -10), 
                 (20, 20, 0, 0, 0, 0, 20, 20), 
                 (20, 30, 10, 0, 0, 10, 30, 20), 
    ), 
    'r': ((0, 0, 0, -5, -5, 0, 0, 0), 
          (5, 0, 0, 0, 0, 0, 0, 5), 
          (5, 0, 0, 0, 0, 0, 0, 5), 
          (5, 0, 0, 0, 0, 0, 0, 5), 
          (5, 0, 0, 0, 0, 0, 0, 5), 
          (5, 0, 0, 0, 0, 0, 0, 5), 
          (-5, -10, -10, -10, -10, -10, -10, -5), 
          (0, 0, 0, 0, 0, 0, 0, 0)), 

    'n': ((50, 40, 30, 30, 30, 30, 40, 50), 
          (40, 20, 0, -5, -5, 0, 20, 10), 
          (30, -5, -10, -15, -15, -10, -5, 30), 
          (30, 0, -15, -20, -20, -15, 0, 30), 
          (30, -5, -15, -20, -20, -15, -5, 30), 
          (30, 0, -10, -15, -15, -10, 0, 30), 
          (40, 20, 0, 0, 0, 0, 20, 40), 
          (50, 40, 30, 30, 30, 30, 40, 50)), 

    'b': ((20, 10, 10, 10, 10, 10, 10, 20), 
          (10, -5, 0, 0, 0, 0, 0, 0, -5, 10), 
          (10, -10, -10, -10, -10, -10, -10, 10), 
          (10, 0, -10, -10, -10, -10, 0, 10), 
          (10, -5, -5, -5, -5, -5, -5, 10), 
          (10, 0, -5, -10, -10, -5, 0, 10), 
          (10, 0, 0, 0, 0, 0, 0, 10), 
          (20, 10, 10, 10, 10, 10, 10, 20)), 

    'k_end': ((50, 30, 30, 30, 30, 30, 30, 50),             # end game
              (30, 30, 0, 0, 0, 0, 30, 30), 
              (30, 10, -20, -30, -30, -20, 10, 30), 
              (30, 10, -30, -40, -40, -30, 10, 30), 
              (30, 10, -30, -40, -40, -30, 10, 30), 
              (30, 10, -20, -30, -30, -20, 10, 30), 
              (30, 20, 10, 0, 0, 10, 20, 30), 
              (50, 40, 30, 20, 20, 30, 40, 50)),

    'k_middle': ((-20, -30, -10, 0, 0, -10, -30, -20),      # middle game
                 (-20, -20, 0, 0, 0, 0, -20, -20), 
                 (10, 20, 20, 20, 20, 20, 20, 10), 
                 (20, 30, 30, 40, 40, 30, 30, 20), 
                 (30, 40, 40, 50, 50, 40, 40, 30), 
                 (30, 40, 40, 50, 50, 40, 40, 30), 
                 (30, 40, 40, 50, 50, 40, 40, 30), 
                 (30, 40, 40, 50, 50, 40, 40, 30)), 

    'q': ((20, 10, 10, -5, -5, 10, 10, 20), 
          (10, 0, -5, 0, 0, 0, 0, 10), 
          (10, -5, -5, -5, -5, -5, 0, 10), 
          (0, 0, -5, -5, -5, -5, 0, 5), 
          (5, 0, -5, -5, -5, -5, 0, 5), 
          (10, 0, -5, -5, -5, -5, 0, 10), 
          (10, 0, 0, 0, 0, 0, 0, 10), 
          (20, 10, 10, 5, 5, 10, 10, 20))
    }