From: Simon Cross Date: Thu, 15 May 2014 21:36:59 +0000 (+0200) Subject: TAB now switches between legal moves on the gameboard. X-Git-Tag: 0.1~249 X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=e4fa85ce8ce05f7272f1fdaf2015649b944c5c5e;p=naja.git TAB now switches between legal moves on the gameboard. --- 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)