-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal_logic.py
More file actions
73 lines (58 loc) · 1.97 KB
/
internal_logic.py
File metadata and controls
73 lines (58 loc) · 1.97 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
import json
import client as cl
# Close connection to robot
def close():
cl.close()
# Function to select the appropriate move based on the json data (move_data)
# move_data has the form jsondata["history"][n], where n is the move identifier
def parseJson(move_data, plycount):
print(move_data)
if (move_data==None):
return "Data empty"
_from = move_data["from"]
to = move_data["to"]
rowA, colA = _from[:1].upper(), int(_from[1:])
cellA = (rowA,colA)
rowB, colB = to[:1].upper(), int(to[1:])
cellB = (rowB,colB)
# Make sure client connection is open
if cl.conn_open == False:
cl.open()
# Do take_piece
if(move_data["capture"]["capture"]):
pieceA = move_data['piece']
pieceB = move_data['capture']['piece']
piece_str = move_data["capture"]["initial_pos_piece"]
rowP, colP = piece_str[:1].upper(), int(piece_str[1:])
piece_cell = (rowP,colP)
cl.take_piece(cellA, cellB, piece_cell, pieceA, pieceB)
plycount+=1
return plycount
# Do castling
elif(move_data["castle"]["castle"]):
if (move_data["castle"]["side"]=='k'):
cellC = ('H',cellA[1])
cellD = ('F',cellB[1])
else:
cellC = ('A',cellA[1])
cellD = ('D',cellB[1])
pieceA = 'k'
pieceB = 'r'
cl.perform_castling_at(cellA, cellC, cellB, cellD, pieceA, pieceB)
plycount+=1
return plycount
# Do en_passant
elif(move_data["en_passant"]["en_passant"]):
piece = move_data["en_passant"]["initial_pos_piece"]
rowP, colP = piece[:1].upper(), int(piece[1:])
piece_cell = (rowP,colP)
cellTake = move_data["en_passant"]["square"]
cl.en_passant(cellA, cellB, cellTake, piece_cell)
plycount+=1
return plycount
# Do move_piece
else:
piece = move_data['piece']
cl.move_piece(cellA, cellB, piece)
plycount+=1
return plycount