Browse Source

new rotation functions

adaptedStrategy0
Slash 9 years ago
parent
commit
0b757376ab
2 changed files with 49 additions and 19 deletions
  1. +46
    -1
      src/Datatypes/Cell.hs
  2. +3
    -18
      src/VM.hs

+ 46
- 1
src/Datatypes/Cell.hs View File

@ -1,3 +1,48 @@
module Datatypes.Cell where
module Datatypes.Cell (Cell(..), rotateClockwise, rotateCounterclockwise ) where
type Cell = (Int, Int) 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)

+ 3
- 18
src/VM.hs View File

@ -7,6 +7,7 @@ import qualified Data.Set as Set
import Datatypes.Board (Board(..)) import Datatypes.Board (Board(..))
import qualified Datatypes.Board as Board import qualified Datatypes.Board as Board
import Datatypes.Cell (Cell(..)) import Datatypes.Cell (Cell(..))
import qualified Datatypes.Cell as Cell
import Datatypes.Game (Game(..), Command(..)) import Datatypes.Game (Game(..), Command(..))
import qualified Datatypes.Game as Game import qualified Datatypes.Game as Game
import Datatypes.Unit (Unit(..)) import Datatypes.Unit (Unit(..))
@ -102,22 +103,6 @@ applyCommand unit MoveSW = Unit.map move unit where
applyCommand unit MoveSE = Unit.map move unit where applyCommand unit MoveSE = Unit.map move unit where
move (x, y) = (x + (y `mod` 2), y + 1) move (x, y) = (x + (y `mod` 2), y + 1)
applyCommand (Unit pivot members) RotateClockwise = Unit pivot (Set.map transform members) where 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 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

Loading…
Cancel
Save