forked from greghope667/square-off-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
53 lines (43 loc) · 1.67 KB
/
player.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
import typing
import random
import chess
import chess.engine
import moveutils
Player: typing.TypeAlias = typing.Callable[[chess.Board], chess.Move]
def player_random() -> Player:
return lambda b: random.choice(list(b.generate_legal_moves()))
def player_stockfish(skill=None) -> Player:
engine = chess.engine.SimpleEngine.popen_uci("stockfish")
if skill is not None:
engine.configure({"Skill Level": skill})
return lambda b: engine.play(b, chess.engine.Limit(time=0.1)).move
def player_voice() -> Player:
import voice
vc = voice.VoiceControl()
def play(board: chess.Board) -> chess.Move:
for phrase in vc.listen():
candidate = vc.translate_phrase(phrase)
print("Searching", candidate)
candidates = list(moveutils.candidates(board, candidate))
print("Potential moves:", candidates)
if len(candidates) == 1:
move = candidates[0]
print("Playing", move.uci())
return move
else:
print("Move ambiguous, please try again")
return play
def player_cli() -> Player:
def play(board: chess.Board) -> chess.Move:
while True:
text = input("Enter move: ").lower()
candidates = list(moveutils.candidates(board, text))
if len(candidates) == 1:
move = candidates[0]
print("Playing", move.uci())
return move
elif len(candidates) == 0:
print("Unknown/invalid move")
else:
print("Move ambiguous, candidates", [moveutils.encode_precise(board, m) for m in candidates])
return play