started implementing rotation
authoradrianna <adrianna.pinska@gmail.com>
Fri, 16 May 2014 14:41:17 +0000 (16:41 +0200)
committeradrianna <adrianna.pinska@gmail.com>
Fri, 16 May 2014 15:47:54 +0000 (17:47 +0200)
naja/actions.py
naja/constants.py
naja/gameboard.py

index 0626b34d6d1eb40b62af76c7796f19eb59920c21..cfa1011cd2d4b76d27322b039b6bf36533d96732 100644 (file)
@@ -140,6 +140,14 @@ class ShiftLocations(LocationAction):
         board.shift_locations(self.data['direction'])
 
 
+class RotateLocations(LocationAction):
+    TEXT = "Rotate adjacent locations %(rot_direction)s."
+    GLYPHS = (ACTION_GLYPHS.CHANGE_BOARD,)
+
+    def perform_action(self, board, location):
+        board.rotate_locations(self.data['rot_direction'])
+
+
 class AllowChessMove(LocationAction):
     TEXT = "Move like a %(chesspiece_name)s for one turn."
     GLYPHS = (ACTION_GLYPHS.MOVEMENT,)
index 440a2d5441a48ab8ed5d07c103afca03d1c234c9..424f65a0ec661563db51a6b8dd33f625b3d6ffb0 100644 (file)
@@ -51,6 +51,11 @@ MOVES = AttrDict({
 })
 CHESS_PIECES = AttrDict((k, v) for k, v in MOVES.items() if v > 0)
 
+ROTATION = AttrDict({
+    'CLOCKWISE': 0,
+    'ANTICLOCKWISE': 1,
+})
+
 # Player defaults
 PLAYER_DEFAULTS = AttrDict({
     'INITIAL_BITS': 0x0f,
index 367a9e15118b4cbd6b9fe3248a691c141f66831d..2d138b2dbe74fceb3bf313fca5eaf05755b2f22b 100644 (file)
@@ -2,7 +2,7 @@ from random import choice
 
 from naja.constants import(
     BITS, DIRECTION_BITS, CONDITION_BITS, PLAYER_DEFAULTS,
-    ACT, EXAMINE)
+    ACT, EXAMINE, ROTATION)
 from naja.player import Player
 from naja import actions
 
@@ -117,7 +117,6 @@ class GameBoard(object):
                 new_i = (new_i + change) % 5
             shifted_locations[mkpos(new_i)] = self.board_locations[mkpos(i)]
 
-        print change, is_vertical, shifted_locations
         self.board_locations.update(shifted_locations)
 
     def shift_locations(self, direction):
@@ -130,6 +129,35 @@ class GameBoard(object):
         elif BITS[direction] == BITS.WEST:
             self.shift_location_row(-1, is_vertical=False)
 
+    def rotate_locations(self, direction):
+        px, py = self.player.position
+        locations_to_rotate = []
+        rotated_locations = {}
+
+        if py > 0:
+            for i in range(max(0, px -1), min(5, px + 2)):
+                locations_to_rotate.append((i, py - 1))
+
+        if px > 0:
+            locations_to_rotate.append((px - 1, py))
+
+        if px < 4:
+            locations_to_rotate.append((px + 1, py))
+
+        if py < 4:
+            for i in range(max(0, px -1), min(5, px + 2)):
+                locations_to_rotate.append((i, py + 1))
+
+        if direction == ROTATION.CLOCKWISE:
+            new_positions = locations_to_rotate[1:] + locations_to_rotate[0]
+        elif direction == ROTATION.ANTICLOCKWISE:
+            new_positions = locations_to_rotate[-1] + locations_to_rotate[:-1]
+
+        for old, new in zip(locations_to_rotate, new_positions):
+            rotated_locations[old] = self.board_locations[new]
+
+        self.board_locations.update(rotated_locations)
+
     def allow_chess_move(self, chesspiece):
         self.player.allow_chess_move(chesspiece)