-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyseSingleMove.py
executable file
·233 lines (179 loc) · 6.19 KB
/
analyseSingleMove.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/python3
import sys
import re
import cs50
from helpers import *
# def main():
# # definitions
# mainDatabase="pgn-database.db"
# tableName="moves"
# columnName="move"
# idColumn="game_id"
# # TODO parametric
# selectedRow = 4
# move = getMovesFromDatabase(mainDatabase, tableName, columnName, idColumn, selectedRow)
# print('...move...', move)
# for i in range(2):# TODO not hardcoded here
# print("analysing move",i+1)
# move = db.execute("SELECT "+columnName+" FROM "+tableName+" WHERE "+idColumn+"="+str(i+1))[0]['move']
# # print input
# print("==========")
# print("move: "+move)
# print("...........")
# board = {}#TODO
# output = analyseMove(board, move)
# # print output
# print("promotion: ", output['promotion'])
# print("isCheck: ", output['isCheck'])
# print("takes: ", output['takes'])
# print("previousCell: ", output['previousCell'])
# print("nextCell: ", output['nextCell'])
# print("piece:", output['piece'])
# print("============")
# return array of move objects with coloumnName = 'move' in default
def getMovesFromDatabase(mainDatabase, tableName, columnName, idColumn, selectedRow):
# read a single move from database
with open(f''+mainDatabase, "r"):
db = cs50.SQL("sqlite:///"+mainDatabase)
move = db.execute("SELECT "+columnName+" FROM "+tableName+" WHERE "+idColumn+"="+str(selectedRow))
return move
def analyseMove(board, move, color=colors['WHITE'], isCheck=False, takes=False, promotion=''):
# TODO
# bxc1=Q. dxc8=N+
# check if-else logic
output={}
output['isCheck']=isCheck
output['takes']=takes
output['promotion']=''
#check if it is a check
lastLiteral = move[len(move) - 1]
if lastLiteral == '+':
move = move[:-1]
output['isCheck']=True
if move == "O-O":
output=analyseCastleShort(output, color)
elif move == "O-O-O":
output=analyseCastleLong(output, color)
elif len(move) == 2:
# e.g e4
output=pawnForward(output, move, color)
elif len(move) == 3:
# a piece move to a empty cell, e.g Rb1, Bd7
output = normalPieceMove(output, move)
elif len(move) == 4:
# e.g Qxb5
if move[1] == 'x':
output=pieceTakes(output, move, color)
elif move[2] == '=':
# e.g a8=Q
output = pawnPromote(board, move, color)
else:
# e.g Nbd3
output = moveByLocationHint(output, board, move, color)
elif len(move) == 5:
# e.g Nbxd4
if move[2] != 'x':
print("????!!!??")
print('MOVE: ', move)
print("!!!!!????!!!")
else:
move = move.replace('x', '')
print('move: ', move)
output = analyseMove(board, move, color=color)
output['takes'] = True
else:
# e.g bxc1=Q
if move[-2] != '=':
print("????!!!??")
print('MOVE: ', move)
print("!!!!!????!!!")
else:
promoteTo = move[-1]
move = move[:-2]
output = analyseMove(board, move, color = color)
output['promotion'] = promoteTo
return output
# get output of normal piece move like Nb3
def normalPieceMove(output, move):
output['piece']=move[0]
output['previousCell']="previousCell"
output['nextCell']=move[1:]
return output
# get output of move by location hint like Nbd3
def moveByLocationHint(output, board, move, color):
output['piece'] = move[0]
locationHint = move[1]
if locationHint.isnumeric():
# find the piece in row of locationHint
output['previousCell'] = findCellByRowLocation(board, color+output['piece'], locationHint)
else:
# find the piece in col of locationHint
output['previousCell'] = findCellByColLocation(board, color+output['piece'], locationHint)
output['nextCell']=move[2:]
return output
# get output of pawn promotion move, a1=Q
def pawnPromote(board, move, color):
promoteTo = move[3]
move = move[:2]
output = analyseMove(board, move, color=color)
output['promotion'] = promoteTo
return output
# get outputs of short castling, O-O
def analyseCastleShort(output, color):
output['piece'] = ['K', 'R']
if color == colors['WHITE']:
# white castle short
output['previousCell']=['e1', 'h1']
output['nextCell']=['g1', 'f1']
else:
# black castle short
output['previousCell']=['e8', 'h8']
output['nextCell']=['g8', 'f8']
return output
# get outputs of long castling, O-O-O
def analyseCastleLong(output, color):
output['piece']=['K', 'R']
if color== colors['WHITE']:
# white castle long
output['previousCell']=['e1', 'a1']
output['nextCell']=['c1', 'd1']
else:
# black castle long
output['previousCell']=['e8', 'a8']
output['nextCell']=['c8', 'd8']
return output
# get output of forward pawn move like e4
def pawnForward(output, move, color):
output['nextCell']=move
output['piece']='p'
print('move:', move)
print('color:', color)
if (move[1] == "4" and color == colors['WHITE']):
output['previousCell']=[move[0]+"3", move [0]+"2"]
elif (move[1] == "5" and color == 'B'):
output['previousCell']=[move[0]+"6", move [0]+"7"]
elif (color == 'W'):
output['previousCell']=move[0]+str((int(move[1])-1))
else:
# black
print('i am here')
output['previousCell']=move[0]+str((int(move[1])+1))
return output
# get output of piece takes move like Bxe3
def pieceTakes(output, move, color):
output['takes']=True
if (move[0].isupper()):
# e.g Qxb3
output['piece']=move[0]
output['previousCell']="previousCell"
output['nextCell']=move[2:]
else:
# e.g axb8
output['piece']='p'
if color == colors['WHITE']:
output['previousCell']=move[0]+str((int(move[3])-1))
else:
output['previousCell']=move[0]+str((int(move[3])+1))
output['nextCell']=move[2:]
return output
# main()