forked from zaphodgjd/class-chess-123
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveTranslator.py
50 lines (40 loc) · 1.28 KB
/
MoveTranslator.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
import sys
def indexToPos(i):
row = i // 8
col = i % 8
return chr(ord('a') + col) + str(row + 1)
def flagData(flag):
# Define the flag codes as a dictionary
flag_codes = {
"EnCapture": 0b00000001,
"DoublePush": 0b00000010,
"Castling": 0b00001100,
"QCastle": 0b00000100,
"KCastle": 0b00001000,
"Promotion": 0b11110000,
"ToQueen": 0b00010000,
"ToKnight": 0b00100000,
"ToRook": 0b01000000,
"ToBishop": 0b10000000,
}
# Initialize an empty string to hold matching flag names
matched_flags = []
# Check which flags match
for name, code in flag_codes.items():
if flag & code == code: # Match if all bits in the code are set in flag
matched_flags.append(name)
return ("\n" + ", ".join(matched_flags)) if matched_flags else ""
def main():
if len(sys.argv) != 2:
print("Usage: python MoveTranslator.py <number>")
return
try:
move = int(sys.argv[1])
flags = flagData(move >> 12)
fpos = indexToPos((move >> 6) & 63)
tpos = indexToPos(move & 63)
print(f"{fpos} -> {tpos}{flags}")
except ValueError:
print("Error: Please provide valid Integer input.")
if __name__ == "__main__":
main()