forked from gras64/chess-skill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
86 lines (79 loc) · 3.11 KB
/
__init__.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
from ovos_workshop.skills import OVOSSkill
from ovos_workshop.decorators import intent_handler
import chess
import chess.engine
from stockfish import Stockfish
class Chess(OVOSSkill):
def initialize(self):
self.board = chess.Board()
self.engine = Stockfish
self.characters_value = self.translate_namedvalues('characters.value')
self.color_value = self.translate_namedvalues('color.value')
@intent_handler('chess.intent')
def handle_chess(self, message):
self.log.info(self.color_value["white"])
if not self.settings[
"savegame"] is "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1":
self.speak_dialog('chess')
a = self.ask_yesno("you.have")
if a == "yes":
self.board = chess.Board(self.settings["savegame"])
else:
color = self.get_response("ask.color")
if color in self.color_value["white"]:
self.log.info("color white")
self.board = chess.Board(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
)
elif color in self.color_value["black"]:
self.log.info("color black")
self.board = chess.Board(
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
)
else:
self.log.info("no color")
self.log.info("\n" + str(board))
@intent_handler('turn.characters.intent')
def handle_characters_turn(self, message):
if message.data.get("from"):
begin = message.data.get("from")
else:
self.get_response("from")
if message.data.get("to"):
end = message.data.get("to")
else:
self.get_response("to")
#if not begin or end:
# self.log.info(begin+" "+end)
# self.speak_dialog('no.turn')
begin = begin.replace(" ", "")
end = end.replace(" ", "")
self.log.info(begin + " " + end)
move = chess.Move.from_uci(begin + end)
if move in board.legal_moves:
board.push(move)
self.log.info("\n" + str(board))
self.turn()
else:
self.speak_dialog('wrong.turn')
self.log.info("\n" + str(board))
@intent_handler('save.intent')
def handle_save(self, message):
self.log.info("save game")
self.settings["savegame"] = board.fen()
def turn(self):
result = self.engine.play(self.board, chess.engine.Limit(time=0.1))
self.log.info("result " + str(result))
board.push(result.move)
character = self.characters_value["r"]
begin = result[0]
end = result[1]
self.speak_dialog("turn",
data={
"character": character,
"from": begin,
"to": end
})
def shutdown(self):
self.settings["savegame"] = board.fen()
super(Chess, self).shutdown()