Skip to content

Commit

Permalink
#15 Add downshifting of cells after a plane is removed.
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasWen committed Aug 30, 2019
1 parent 0d7ea96 commit 259e311
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
28 changes: 27 additions & 1 deletion src/Grid.elm
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,32 @@ filterOutPlanes grid planes =
grid


shiftRemainingGrid : Grid -> List Float -> Grid
shiftRemainingGrid grid removedPlanes =
let
shiftCellByOne : Cell -> Cell
shiftCellByOne c =
{ color = c.color, position = { x = c.position.x, y = c.position.y + 1, z = c.position.z } }

shift : List Float -> Grid -> Grid
shift rP g =
case rP of
currentLevel :: rest ->
List.filter (\cell -> cell.position.y < currentLevel) g
|> List.map shiftCellByOne
|> mergeGrids (List.filter (\cell -> cell.position.y > currentLevel) g)
|> shift rest

[] ->
g
in
shift (removedPlanes |> List.sort) grid


clearPlanes : Grid -> Int -> Float -> ( Grid, Int )
clearPlanes grid cellCount height =
( filterOutPlanes grid <| getPlanesToRemove grid 0 height cellCount, List.length <| getPlanesToRemove grid 0 height cellCount )
let
planesToRemove =
getPlanesToRemove grid 0 height cellCount
in
( shiftRemainingGrid (filterOutPlanes grid <| planesToRemove) planesToRemove, List.length <| planesToRemove )
36 changes: 35 additions & 1 deletion tests/GridTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,55 @@ suite =
|> Expect.equal True
]
, describe "Grid.getPlanesToRemove"
[ test "Get the bottom lane" <|
[ test "Get the bottom lpane" <|
\_ ->
getPlanesToRemove setupPlanesOneLevel 0 worldDimensions.height (round <| worldDimensions.width * worldDimensions.depth)
|> Expect.equal [ 13 ]
, test "Get the second plane" <|
\_ ->
getPlanesToRemove secondLevelPlaneWithBlocksBelowAndAbove 0 worldDimensions.height (round <| worldDimensions.width * worldDimensions.depth)
|> Expect.equal [ 12 ]
]
, describe "Grid.filterOutPlanes"
[ test "Filter out bottom plane" <|
\_ ->
filterOutPlanes setupPlanesOneLevel [ 13 ]
|> Expect.equal []
, test "Filter out second plane" <|
\_ ->
filterOutPlanes secondLevelPlaneWithBlocksBelowAndAbove [ 12 ]
|> Expect.equal
[ { color = Color 255 255 255, position = Position 0 13 0 }
, { color = Color 0 0 0, position = Position 0 11 0 }
]
]
, describe "Grid.clearPlanes"
[ test "Clear one plane from grid" <|
\_ ->
clearPlanes setupPlanesOneLevel (round <| worldDimensions.width * worldDimensions.depth) worldDimensions.height
|> Expect.equal ( [], 1 )
, test "Clear four level plane with one additional cell on top which remains after clear." <|
\_ ->
clearPlanes planesFourLevelWithOneAdditionalCell (round <| worldDimensions.width * worldDimensions.depth) worldDimensions.height
|> Expect.equal ( [ { color = Color 255 255 255, position = Position 0 13 0 } ], 4 )
, test "Clear four level plane with thre additional cell on top which are on seperate levels." <|
\_ ->
clearPlanes planesFourLevelWithAdditionalCells (round <| worldDimensions.width * worldDimensions.depth) worldDimensions.height
|> Expect.equal
( [ { color = Color 255 255 255, position = Position 0 13 0 }
, { color = Color 255 255 255, position = Position 0 12 0 }
, { color = Color 255 255 255, position = Position 0 11 0 }
]
, 4
)
, test "Clear second level plane with blocks on first plane and additional cell on top." <|
\_ ->
clearPlanes secondLevelPlaneWithBlocksBelowAndAbove (round <| worldDimensions.width * worldDimensions.depth) worldDimensions.height
|> Expect.equal
( [ { color = Color 255 255 255, position = Position 0 13 0 }
, { color = Color 0 0 0, position = Position 0 12 0 }
]
, 1
)
]
]
28 changes: 27 additions & 1 deletion tests/TestSetup.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module TestSetup exposing (blueTetroidInTopCenter, cell, orangeTetroidInTopCenter, position, setupPlanesOneLevel, setupTetroid, worldDimensions)
module TestSetup exposing (blueTetroidInTopCenter, cell, orangeTetroidInTopCenter, planesFourLevelWithAdditionalCells, planesFourLevelWithOneAdditionalCell, position, secondLevelPlaneWithBlocksBelowAndAbove, setupPlanesFourLevel, setupPlanesOneLevel, setupTetroid, worldDimensions)

import Dimensions exposing (..)
import Expect exposing (Expectation)
Expand Down Expand Up @@ -42,6 +42,32 @@ setupPlanesOneLevel =
createFilledGrid (worldDimensions.height - 1) worldDimensions


setupPlanesFourLevel : Grid
setupPlanesFourLevel =
mergeGrids (mergeGrids (createFilledGrid (worldDimensions.height - 1) worldDimensions) (createFilledGrid (worldDimensions.height - 2) worldDimensions))
(mergeGrids (createFilledGrid (worldDimensions.height - 3) worldDimensions) (createFilledGrid (worldDimensions.height - 4) worldDimensions))


planesFourLevelWithOneAdditionalCell : Grid
planesFourLevelWithOneAdditionalCell =
{ color = Color 255 255 255, position = Position 0 (worldDimensions.height - 5) 0 } :: setupPlanesFourLevel


planesFourLevelWithAdditionalCells : Grid
planesFourLevelWithAdditionalCells =
{ color = Color 255 255 255, position = Position 0 (worldDimensions.height - 5) 0 }
:: { color = Color 255 255 255, position = Position 0 (worldDimensions.height - 6) 0 }
:: { color = Color 255 255 255, position = Position 0 (worldDimensions.height - 7) 0 }
:: setupPlanesFourLevel


secondLevelPlaneWithBlocksBelowAndAbove : Grid
secondLevelPlaneWithBlocksBelowAndAbove =
{ color = Color 255 255 255, position = Position 0 (worldDimensions.height - 1) 0 }
:: { color = Color 0 0 0, position = Position 0 (worldDimensions.height - 3) 0 }
:: createFilledGrid (worldDimensions.height - 2) worldDimensions


blueTetroidInTopCenter : Tetroid
blueTetroidInTopCenter =
createTetroid blue
Expand Down

0 comments on commit 259e311

Please sign in to comment.