diff --git a/src/Datatypes/Cell.hs b/src/Datatypes/Cell.hs index 1239af4..0cb3914 100644 --- a/src/Datatypes/Cell.hs +++ b/src/Datatypes/Cell.hs @@ -1,3 +1,48 @@ -module Datatypes.Cell where +module Datatypes.Cell (Cell(..), rotateClockwise, rotateCounterclockwise ) where type Cell = (Int, Int) + +rotateClockwise :: Cell -> Cell -> Cell +rotateClockwise pivot cell = let cubeCell = toCube cell + cubePivot = toCube pivot + cubeZero = cubeCell `cubeDiff` cubePivot + rotatedZero = rotateCubeClockwise cubeZero + rotatedCube = rotatedZero `cubeSum` cubePivot + resultCell = fromCube rotatedCube + in resultCell + +rotateCounterclockwise :: Cell -> Cell -> Cell +rotateCounterclockwise pivot cell = let cubeCell = toCube cell + cubePivot = toCube pivot + cubeZero = cubeCell `cubeDiff` cubePivot + rotatedZero = rotateCubeCounterclockwise cubeZero + rotatedCube = rotatedZero `cubeSum` cubePivot + resultCell = fromCube rotatedCube + in resultCell + +-- PRIVATE + +type CubeCell = (Int, Int, Int) + +toCube :: Cell -> CubeCell +toCube (x, y) = (xx, yy, zz) where + xx = x - (y - (y `mod` 2)) `div` 2 + zz = y + yy = -(xx + zz) + +rotateCubeClockwise :: CubeCell -> CubeCell +rotateCubeClockwise (xx, yy, zz) = (-zz, -xx, -yy) + +rotateCubeCounterclockwise :: CubeCell -> CubeCell +rotateCubeCounterclockwise (xx, yy, zz) = (-yy, -zz, -xx) + +fromCube :: CubeCell -> Cell +fromCube (xx, yy, zz) = (x, y) where + x = xx + (zz - (zz `mod` 2)) `div` 2 + y = zz + +cubeDiff :: CubeCell -> CubeCell -> CubeCell +cubeDiff (x1, y1, z1) (x2, y2, z2) = (x1 - x2, y1 - y2, z1 - z2) + +cubeSum :: CubeCell -> CubeCell -> CubeCell +cubeSum (x1, y1, z1) (x2, y2, z2) = (x1 + x2, y1 + y2, z1 + z2) diff --git a/src/VM.hs b/src/VM.hs index 92ec62c..345c3cc 100644 --- a/src/VM.hs +++ b/src/VM.hs @@ -7,6 +7,7 @@ import qualified Data.Set as Set import Datatypes.Board (Board(..)) import qualified Datatypes.Board as Board import Datatypes.Cell (Cell(..)) +import qualified Datatypes.Cell as Cell import Datatypes.Game (Game(..), Command(..)) import qualified Datatypes.Game as Game import Datatypes.Unit (Unit(..)) @@ -102,22 +103,6 @@ applyCommand unit MoveSW = Unit.map move unit where applyCommand unit MoveSE = Unit.map move unit where move (x, y) = (x + (y `mod` 2), y + 1) applyCommand (Unit pivot members) RotateClockwise = Unit pivot (Set.map transform members) where - transform cell = rotateCell pivot cell + transform cell = Cell.rotateClockwise pivot cell applyCommand (Unit pivot members) RotateCounterclockwise = Unit pivot (Set.map transform members) where - transform cell = counterRotateCell pivot cell - -rotateCell :: Cell -> Cell -> Cell -rotateCell (px, py) (x, y) = (px - ddiag + (doriz + py `mod` 2) `div` 2, py + doriz) where - (ddiag, doriz) = relativePosition (px, py) (x, y) - -counterRotateCell :: Cell -> Cell -> Cell -counterRotateCell pivot = rp . rp . rp . rp . rp where - rp = rotateCell pivot -counterRotateCell2 :: Cell -> Cell -> Cell -counterRotateCell2 (px, py) (x, y) = (px + (ddiag + doriz + py `mod` 2) `div` 2, py + ddiag - doriz) where - (ddiag, doriz) = relativePosition (px, py) (x, y) - -relativePosition :: Cell -> Cell -> (Int, Int) -relativePosition (px, py) (x, y) = (diagDir, horizDir) where - diagDir = y - py - horizDir = x - px + (if py `mod` 2 == 0 then diagDir + diagDir `mod` 2 else diagDir - diagDir `mod` 2) `div` 2 + transform cell = Cell.rotateCounterclockwise pivot cell