Skip to content

Commit c9e1d25

Browse files
committed
Episode 9: Restricting movement
1 parent 4699a91 commit c9e1d25

File tree

2 files changed

+107
-37
lines changed

2 files changed

+107
-37
lines changed

src/Main.elm

Lines changed: 92 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Browser.Events exposing (onKeyDown)
55
import Html exposing (Html, button, div, text)
66
import Html.Events exposing (onClick)
77
import Json.Decode as Decode
8-
import List exposing (drop, head, length, map, range)
8+
import List exposing (all, drop, head, length, map, range)
99
import Random
1010
import String exposing (fromInt)
1111
import Svg
@@ -51,6 +51,8 @@ type alias Board =
5151
type FieldColor
5252
= Blue
5353
| Red
54+
| Purple
55+
| Green
5456

5557

5658
type Field
@@ -72,6 +74,18 @@ type alias PieceDefinition =
7274
}
7375

7476

77+
zPiece : PieceDefinition
78+
zPiece =
79+
{ tiles =
80+
[ ( 0, 0 )
81+
, ( -1, 1 )
82+
, ( 0, 1 )
83+
, ( 1, 0 )
84+
]
85+
, color = Green
86+
}
87+
88+
7589
tPiece : PieceDefinition
7690
tPiece =
7791
{ tiles =
@@ -96,9 +110,23 @@ jPiece =
96110
}
97111

98112

113+
lPiece : PieceDefinition
114+
lPiece =
115+
{ tiles =
116+
[ ( 0, 0 )
117+
, ( -1, 0 )
118+
, ( 1, 1 )
119+
, ( 1, 0 )
120+
]
121+
, color = Purple
122+
}
123+
124+
99125
pieceDefinitions =
100126
[ tPiece
101127
, jPiece
128+
, lPiece
129+
, zPiece
102130
]
103131

104132

@@ -219,6 +247,11 @@ spawnPiece pieceIndex model =
219247
}
220248

221249

250+
flip : (a -> b -> c) -> b -> a -> c
251+
flip f a b =
252+
f b a
253+
254+
222255
movePiece : Key -> Model -> Model
223256
movePiece key model =
224257
case model.currentPiece of
@@ -230,42 +263,71 @@ movePiece key model =
230263
( x, y ) =
231264
currentPiece.position
232265

233-
tiles =
234-
currentPiece.tiles
235-
236-
newPosition =
266+
( newPosition, newTiles ) =
237267
case key of
238268
LeftArrow ->
239-
( x - 1, y )
269+
( ( x - 1, y )
270+
, currentPiece.tiles
271+
)
240272

241273
RightArrow ->
242-
( x + 1, y )
274+
( ( x + 1, y )
275+
, currentPiece.tiles
276+
)
243277

244278
DownArrow ->
245-
( x, y )
246-
247-
newTiles =
248-
case key of
249-
LeftArrow ->
250-
tiles
251-
252-
RightArrow ->
253-
tiles
254-
255-
DownArrow ->
256-
let
257-
rotate ( tx, ty ) =
258-
( -ty, tx )
259-
in
260-
map rotate tiles
279+
( currentPiece.position
280+
, map
281+
(\( tx, ty ) -> ( -ty, tx ))
282+
currentPiece.tiles
283+
)
261284

262285
movedPiece =
263286
{ currentPiece
264287
| position = newPosition
265288
, tiles = newTiles
266289
}
290+
291+
canMove =
292+
all ((==) (Just Empty)) <|
293+
map (flip lookUp model.board) <|
294+
occupiedPositions movedPiece
267295
in
268-
{ model | currentPiece = Just movedPiece }
296+
if canMove then
297+
{ model | currentPiece = Just movedPiece }
298+
299+
else
300+
model
301+
302+
303+
occupiedPositions : CurrentPiece -> List Position
304+
occupiedPositions { position, tiles } =
305+
let
306+
( x, y ) =
307+
position
308+
309+
translate ( tx, ty ) =
310+
( tx + x, ty + y )
311+
in
312+
map translate tiles
313+
314+
315+
lookUp : Position -> Board -> Maybe Field
316+
lookUp ( tx, ty ) { rows } =
317+
let
318+
mbRow =
319+
List.head <| List.drop ty rows
320+
in
321+
if tx < 0 || ty < 0 then
322+
Nothing
323+
324+
else
325+
case mbRow of
326+
Nothing ->
327+
Nothing
328+
329+
Just (Row fields) ->
330+
List.head <| List.drop tx fields
269331

270332

271333
canDrop : CurrentPiece -> Board -> Bool
@@ -280,18 +342,6 @@ canDrop { position, tiles } board =
280342
translatedTiles =
281343
map translateToCurrentPos tiles
282344

283-
lookUp ( tx, ty ) { rows } =
284-
let
285-
mbRow =
286-
List.head <| List.drop ty rows
287-
in
288-
case mbRow of
289-
Nothing ->
290-
Nothing
291-
292-
Just (Row fields) ->
293-
List.head <| List.drop tx fields
294-
295345
isEmpty pos =
296346
lookUp pos board == Just Empty
297347

@@ -485,6 +535,12 @@ ffToColor field =
485535
Field Red ->
486536
"red"
487537

538+
Field Purple ->
539+
"purple"
540+
541+
Field Green ->
542+
"green"
543+
488544

489545
fieldView : Field -> Int -> Int -> Html Msg
490546
fieldView field row column =

tests/Example.elm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ import Test exposing (..)
1010
suite : Test
1111
suite =
1212
describe "canDrop"
13-
[ test "can drop piece on empty board if not at bottom" <|
13+
[ test "lookUp returns Nothing for lookup left outside of board" <|
14+
\_ ->
15+
let
16+
emptyBoard =
17+
mkEmptyBoard 3 3
18+
in
19+
Expect.equal (lookUp ( -1, 1 ) emptyBoard) Nothing
20+
, test "lookUp returns Nothing for lookup below of board" <|
21+
\_ ->
22+
let
23+
emptyBoard =
24+
mkEmptyBoard 3 3
25+
in
26+
Expect.equal (lookUp ( -1, 1 ) emptyBoard) Nothing
27+
, test "can drop piece on empty board if not at bottom" <|
1428
\_ ->
1529
let
1630
currentPiece =

0 commit comments

Comments
 (0)