|
@ -7,19 +7,11 @@ 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 Datatypes.Game (Game(..)) |
|
|
|
|
|
|
|
|
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(..)) |
|
|
import qualified Datatypes.Unit as Unit |
|
|
import qualified Datatypes.Unit as Unit |
|
|
|
|
|
|
|
|
data Command = MoveW |
|
|
|
|
|
| MoveE |
|
|
|
|
|
| MoveSW |
|
|
|
|
|
| MoveSE |
|
|
|
|
|
| RotateClockwise |
|
|
|
|
|
| RotateCounterclockwise |
|
|
|
|
|
deriving (Show,Eq) |
|
|
|
|
|
|
|
|
|
|
|
data Notes = OK |
|
|
data Notes = OK |
|
|
| GameOver |
|
|
| GameOver |
|
|
| Lock { rowsCleaned :: Int } |
|
|
| Lock { rowsCleaned :: Int } |
|
@ -66,11 +58,11 @@ lockUnit game = game { |
|
|
|
|
|
|
|
|
testStep = let unit = Unit (0, 0) (Set.fromList [(2,9)]) |
|
|
testStep = let unit = Unit (0, 0) (Set.fromList [(2,9)]) |
|
|
board = Board 5 10 (Set.fromList [(0,8),(1,8),(0,9),(1,9),(4,8),(3,9),(4,9)]) |
|
|
board = Board 5 10 (Set.fromList [(0,8),(1,8),(0,9),(1,9),(4,8),(3,9),(4,9)]) |
|
|
in step (Game board [unit] (Set.fromList []) 0 0) MoveSW |
|
|
|
|
|
|
|
|
in step (Game board [unit] (Set.fromList []) 0 0 []) MoveSW |
|
|
|
|
|
|
|
|
testStep2 = let unit = Unit (2, 4) (Set.fromList [(0,3),(1,3),(2,3),(3,3),(1,4),(2,4),(3,4), (1,5),(2,5),(2,6)]) |
|
|
testStep2 = let unit = Unit (2, 4) (Set.fromList [(0,3),(1,3),(2,3),(3,3),(1,4),(2,4),(3,4), (1,5),(2,5),(2,6)]) |
|
|
board = Board 6 10 (Set.fromList [(0,8),(0,9),(4,6),(3,7),(4,7),(3,8),(4,8),(2,9),(3,9),(4,9),(5,8),(5,9)]) |
|
|
board = Board 6 10 (Set.fromList [(0,8),(0,9),(4,6),(3,7),(4,7),(3,8),(4,8),(2,9),(3,9),(4,9),(5,8),(5,9)]) |
|
|
(g1, n1) = step (Game board [unit] (Set.fromList []) 2 0) MoveSW |
|
|
|
|
|
|
|
|
(g1, n1) = step (Game board [unit] (Set.fromList []) 2 0 []) MoveSW |
|
|
(g2, n2) = step g1 MoveSE |
|
|
(g2, n2) = step g1 MoveSE |
|
|
(g3, n3) = step g2 MoveSW |
|
|
(g3, n3) = step g2 MoveSW |
|
|
(g4, n4) = step g3 MoveSW |
|
|
(g4, n4) = step g3 MoveSW |
|
@ -78,27 +70,27 @@ testStep2 = let unit = Unit (2, 4) (Set.fromList [(0,3),(1,3),(2,3),(3,3),(1,4), |
|
|
|
|
|
|
|
|
testStep3 = let unit = Unit (0, 0) (Set.fromList [(2,9)]) |
|
|
testStep3 = let unit = Unit (0, 0) (Set.fromList [(2,9)]) |
|
|
board = Board 5 10 (Set.fromList [(0,8),(1,8),(0,9),(1,9),(4,8),(3,9),(4,9)]) |
|
|
board = Board 5 10 (Set.fromList [(0,8),(1,8),(0,9),(1,9),(4,8),(3,9),(4,9)]) |
|
|
in step (Game board [unit] (Set.fromList []) 0 0) MoveSW |
|
|
|
|
|
|
|
|
in step (Game board [unit] (Set.fromList []) 0 0 []) MoveSW |
|
|
|
|
|
|
|
|
step :: Game -> Command -> (Game, Notes) |
|
|
step :: Game -> Command -> (Game, Notes) |
|
|
step game@(Game { units = [] }) command = (game, ErrorGameEnded) |
|
|
step game@(Game { units = [] }) command = (game, ErrorGameEnded) |
|
|
step game command = |
|
|
step game command = |
|
|
if shouldLock |
|
|
|
|
|
then |
|
|
|
|
|
if Game.completed updatedGame |
|
|
|
|
|
then (updatedGame, GameOver) |
|
|
|
|
|
else (updatedGame, Lock (Game.oldLines updatedGame)) |
|
|
|
|
|
else |
|
|
|
|
|
if Set.member (hash newUnit) (Game.visitedUnits game) |
|
|
|
|
|
then (game { units = newUnit:otherUnits }, ErrorSamePosition) |
|
|
|
|
|
else (game { units = newUnit:otherUnits, visitedUnits = newVisitedUnits }, OK) |
|
|
|
|
|
where |
|
|
|
|
|
(unit:otherUnits) = Game.units game |
|
|
|
|
|
|
|
|
let (unit:otherUnits) = Game.units game |
|
|
newUnit = applyCommand unit command |
|
|
newUnit = applyCommand unit command |
|
|
board = Game.board game |
|
|
board = Game.board game |
|
|
shouldLock = newUnit `Unit.collidesWith` board || newUnit `Unit.isOutsideOf` board |
|
|
shouldLock = newUnit `Unit.collidesWith` board || newUnit `Unit.isOutsideOf` board |
|
|
newVisitedUnits = Set.insert (hash newUnit) (Game.visitedUnits game) |
|
|
newVisitedUnits = Set.insert (hash newUnit) (Game.visitedUnits game) |
|
|
updatedGame = checkSpawn $ lockUnit game |
|
|
|
|
|
|
|
|
updatedGame = (checkSpawn $ lockUnit game) { history = command:(Game.history game) } |
|
|
|
|
|
in |
|
|
|
|
|
if shouldLock |
|
|
|
|
|
then |
|
|
|
|
|
if Game.completed updatedGame |
|
|
|
|
|
then (updatedGame, GameOver) |
|
|
|
|
|
else (updatedGame, Lock (Game.oldLines updatedGame)) |
|
|
|
|
|
else |
|
|
|
|
|
if Set.member (hash newUnit) (Game.visitedUnits game) |
|
|
|
|
|
then (game { units = newUnit:otherUnits }, ErrorSamePosition) |
|
|
|
|
|
else (game { units = newUnit:otherUnits, visitedUnits = newVisitedUnits, history = command:(Game.history game) }, OK) |
|
|
|
|
|
|
|
|
applyCommand :: Unit -> Command -> Unit |
|
|
applyCommand :: Unit -> Command -> Unit |
|
|
applyCommand unit MoveW = Unit.map move unit where |
|
|
applyCommand unit MoveW = Unit.map move unit where |
|
|