-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchessbyte.py
162 lines (126 loc) · 3.83 KB
/
chessbyte.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
debug = False
pieces = "pnbrqk"
def xy_to_i(xy: tuple) -> int:
return xy[0] + 8 * xy[1]
def i_to_xy(i: int) -> tuple:
return (i % 8, i // 8)
def add_chunk(result: list, offset: int, chunk: int):
if not offset:
result[-1] |= chunk
else:
result.append(chunk << 4)
def encode(data: dict) -> bytes:
result = []
offset = 1
last = None
for (x, y) in sorted(filter(lambda key: isinstance(key, tuple), data), key=xy_to_i) + ([(7, 7)] * ((7, 7) not in data)):
if last:
distance = xy_to_i((x, y)) - xy_to_i(last) - 1
if distance:
skip = 0xC0 + distance
# print("writing a skip with {distance} distance. Skipdata: {skip:08b}".format(distance=distance, skip=skip))
if not offset:
add_chunk(
result,
offset,
skip >> 4
)
add_chunk(
result,
1 - offset,
skip & 0xF
)
else:
result.append(skip)
if (x, y) not in data:
break
piece = (data[x, y][0] == "b") | (pieces.index(data[x, y][1]) << 1)
last = (x, y)
# print("writing chunk {chunk:04b}: PIECE-{piece} at {coords}.".format(chunk=piece, piece=data[x, y], coords=(x, y)))
add_chunk(
result,
offset,
piece
)
offset = 1 - offset
side = (data.get("side", "w") == "b")
en_passant = "abcdefgh".index(data.get("en_passant", "a"))
en_passant_possible = "en_passant" in data
add_chunk(
result,
offset,
en_passant | (en_passant_possible << 3)
)
offset = 1 - offset
add_chunk(
result,
offset,
(side << 3)
)
offset = 1 - offset
add_chunk(
result,
offset,
sum([
(1 << x) for x in map(
lambda i: (["bk", "bq", "wk", "wq"][3 - i] in data.get("castling", ["bk", "bq", "wk", "wq"])) * i,
range(4)
)
])
)
offset = 1 - offset
if not offset:
add_chunk(
result,
offset,
0xF
)
return bytes(result)
def decode(chss_bytes: bytes) -> dict:
data = list(chss_bytes)
result = {}
i = 0
is_aligned = (data[-1] & 0xF == 0xF)
skip_buf = 0
mode = 0
for j in range(len(data) * 2):
if j % 2:
chunk = data[j // 2] & 0xF
else:
chunk = data[j // 2] >> 4
if not mode:
if chunk > 11 and not skip_buf:
skip_buf = chunk
continue
if skip_buf:
skip = ((skip_buf << 4) | chunk) ^ 0xC0
i += skip
skip_buf = 0
continue
(x, y) = i_to_xy(i)
side = "wb"[chunk % 2]
piece = pieces[chunk >> 1]
# print(f"Reading PIECE-{side}{piece} at {(x, y)}, j: {j}, i: {i}")
result[x, y] = side + piece
if i == 63:
mode += 1
i += 1
continue
if mode == 1:
en_passant_possible = chunk >> 3
result["en_passant"] = "abcdef"[chunk & 0x7]
if not en_passant_possible:
result.pop("en_passant")
elif mode == 2:
result["side"] = "wb"[chunk >> 3]
elif mode == 3:
castling = []
possible = ["bk", "bq", "wk", "wq"]
for i in range(4):
if chunk & (1 << i):
castling.append(possible[3 - i])
result["castling"] = castling
else:
break
mode += 1
return result