TAB now switches between legal moves on the gameboard.
authorSimon Cross <hodgestar@gmail.com>
Thu, 15 May 2014 21:36:59 +0000 (23:36 +0200)
committerSimon Cross <hodgestar@gmail.com>
Thu, 15 May 2014 21:37:15 +0000 (23:37 +0200)
naja/widgets/board.py

index 222da6cea9c1122270a45f1be823baa165e4e949..f9bb80c189cb32baa1bf4f8296707db38c871909 100644 (file)
@@ -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)