@@ -5,7 +5,7 @@ import Browser.Events exposing (onKeyDown)
55import Html exposing (Html , button , div , text )
66import Html.Events exposing (onClick )
77import Json.Decode as Decode
8- import List exposing (drop , head , length , map , range )
8+ import List exposing (all , drop , head , length , map , range )
99import Random
1010import String exposing (fromInt )
1111import Svg
@@ -51,6 +51,8 @@ type alias Board =
5151type FieldColor
5252 = Blue
5353 | Red
54+ | Purple
55+ | Green
5456
5557
5658type 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+
7589tPiece : PieceDefinition
7690tPiece =
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+
99125pieceDefinitions =
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+
222255movePiece : Key -> Model -> Model
223256movePiece 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
271333canDrop : 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
489545fieldView : Field -> Int -> Int -> Html Msg
490546fieldView field row column =
0 commit comments