From e4fa85ce8ce05f7272f1fdaf2015649b944c5c5e Mon Sep 17 00:00:00 2001 From: Simon Cross Date: Thu, 15 May 2014 23:36:59 +0200 Subject: [PATCH] TAB now switches between legal moves on the gameboard. --- naja/widgets/board.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/naja/widgets/board.py b/naja/widgets/board.py index 222da6c..f9bb80c 100644 --- a/naja/widgets/board.py +++ b/naja/widgets/board.py @@ -39,15 +39,27 @@ class BoardWidget(Widget): for tile in self._tiles: tile.draw(surface) + def update_card_pos(self, card_pos): + self.card_pos = card_pos + self.info.set_position(self.card_pos) + self.legal = self.card_pos in self.state.player.legal_moves() + def change_pos(self, offset): tpos = (offset[0] + self.card_pos[0], offset[1] + self.card_pos[1]) if tpos[0] < 0 or tpos[1] < 0 or tpos[0] > 4 or tpos[1] > 4: return False - self.card_pos = tpos - self.info.set_position(self.card_pos) - self.legal = self.card_pos in self.state.player.legal_moves() + self.update_card_pos(tpos) return True + def next_viable_pos(self): + moves = self.state.player.legal_moves() + try: + idx = moves.index(self.card_pos) + idx = (idx + 1) % len(moves) + except ValueError: + idx = 0 + self.update_card_pos(moves[idx]) + def handle_event(self, ev): if self.state.gameboard.player_mode == ACT: return super(BoardWidget, self).handle_event(ev) @@ -64,6 +76,9 @@ class BoardWidget(Widget): elif ev.key in KEYS.RIGHT: if self.change_pos((+1, 0)): return finish_event() + elif ev.key in KEYS.SWITCH: + self.next_viable_pos() + return finish_event() elif ev.key in KEYS.SELECT: if self.state.player.set_position(self.card_pos): self.state.gameboard.change_mode(ACT) -- 2.34.1