-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
115 lines (87 loc) · 3.24 KB
/
Copy pathmain.py
File metadata and controls
115 lines (87 loc) · 3.24 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
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
import time
import json
import os
from detector import BoardDetector
from listener import VoiceListener
from executor import MoveExecutor
save_file = "calibration.json"
GREEN = "\033[92m"
YELLOW = "\033[93m"
RED = "\033[91m"
CYAN = "\033[96m"
RESET = "\033[0m"
def load():
if os.path.exists(save_file):
with open(save_file) as f:
data = json.load(f)
return data["top_left"], data["bottom_right"]
return None, None
def save(top_left, bottom_right):
with open(save_file, "w") as f:
json.dump({"top_left": top_left, "bottom_right": bottom_right}, f)
def main():
print(f"""
{CYAN}╔═══════════════════════════════════════╗
║ ♟ Chess Voice Controller ♟ ║
║ say a move and it plays on screen ║
╚═══════════════════════════════════════╝{RESET}
""")
detector = BoardDetector()
listener = VoiceListener()
executor = MoveExecutor()
print(f"{YELLOW}opening chess.com...{RESET}")
detector.open_browser()
time.sleep(4)
top_left, bottom_right = load()
if top_left is None:
print(f"{YELLOW}looking for board...{RESET}")
top_left, bottom_right = detector.find_board()
if top_left is None:
print(f"{YELLOW}could not find board automatically{RESET}")
top_left, bottom_right = detector.manual_setup()
save(top_left, bottom_right)
executor.set_board(top_left, bottom_right)
print(f"\n{GREEN}board ready{RESET}")
print(f" top left: {top_left}")
print(f" bottom right: {bottom_right}")
print(f"\n{YELLOW}say 'e2 to e4' to move")
print(f"say 'flip' to switch sides")
print(f"say 'recalibrate' to redo board setup")
print(f"say 'quit' to exit{RESET}\n")
while True:
try:
print(f"{CYAN}listening...{RESET}")
text = listener.listen()
if not text:
print(f"{RED}didn't catch that{RESET}")
continue
print(f"heard: '{text}'")
if "quit" in text or "exit" in text:
print(f"\n{GREEN}bye!{RESET}")
break
if "recalibrate" in text:
top_left, bottom_right = detector.manual_setup()
executor.set_board(top_left, bottom_right)
save(top_left, bottom_right)
continue
if "flip" in text:
executor.flip()
side = "black" if executor.flipped else "white"
print(f"{GREEN}switched to {side} side{RESET}")
continue
move = listener.parse(text)
if not move:
print(f"{RED}couldn't understand that as a move{RESET}")
continue
src, dst = move
print(f"moving {GREEN}{src} → {dst}{RESET}")
executor.move(src, dst)
print(f"{GREEN}done{RESET}\n")
time.sleep(0.5)
except KeyboardInterrupt:
print(f"\n{GREEN}bye!{RESET}")
break
except Exception as e:
print(f"{RED}error: {e}{RESET}")
if __name__ == "__main__":
main()