adds scoring

This commit is contained in:
Victor Martinez 2022-08-15 04:46:35 +02:00
parent 6f20144526
commit 8440a7af06
3 changed files with 82 additions and 35 deletions

View file

@ -8,6 +8,6 @@ You can use the arrow keys to move the numbers on the board.
## TODO ## TODO
+ [ ] Add scoring system + [x] Add scoring system
+ [ ] Add a message when winning or losing + [ ] Add a message when winning or losing
+ [ ] Improve styling + [ ] Improve styling

View file

@ -116,98 +116,145 @@ addCell model cell position =
{ model | board = newBoard } { model | board = newBoard }
setBoard : Model -> Board -> Model setBoard : Board -> Model -> Model
setBoard model board = setBoard board model =
{ model | board = board } { model | board = board }
addScore : Int -> Model -> Model
addScore score model =
{ model | score = model.score + score }
foldUp_ : Model -> Model foldUp_ : Model -> Model
foldUp_ model = foldUp_ model =
let let
newBoard = result =
model.board model.board
|> Array.toList |> Array.toList
>> List.map Array.toList >> List.map Array.toList
>> List.Extra.transpose >> List.Extra.transpose
>> List.map foldRowLeft_ >> List.map foldRowLeft_
>> List.Extra.transpose >> (\rows -> ( rows |> List.map Tuple.first, rows |> List.map Tuple.second ))
>> List.map Array.fromList >> Tuple.mapBoth
>> Array.fromList (List.foldl (+) 0)
(List.Extra.transpose
>> List.map Array.fromList
>> Array.fromList
)
score =
Tuple.first result
newBoard =
Tuple.second result
in in
setBoard model newBoard model
|> setBoard newBoard
|> addScore score
foldDown_ : Model -> Model foldDown_ : Model -> Model
foldDown_ model = foldDown_ model =
let let
newBoard = result =
model.board model.board
|> Array.toList |> Array.toList
>> List.map Array.toList >> List.map Array.toList
>> List.Extra.transpose >> List.Extra.transpose
>> List.map foldRowRight_ >> List.map foldRowRight_
>> List.Extra.transpose >> (\rows -> ( rows |> List.map Tuple.first, rows |> List.map Tuple.second ))
>> List.map Array.fromList >> Tuple.mapBoth
>> Array.fromList (List.foldl (+) 0)
(List.Extra.transpose
>> List.map Array.fromList
>> Array.fromList
)
score =
Tuple.first result
newBoard =
Tuple.second result
in in
setBoard model newBoard model
|> setBoard newBoard
|> addScore score
foldLeft_ : Model -> Model foldLeft_ : Model -> Model
foldLeft_ model = foldLeft_ model =
let let
result =
model.board |> Array.map (Array.toList >> foldRowLeft_ >> Tuple.mapSecond Array.fromList)
points =
result |> Array.map Tuple.first |> Array.foldl (+) 0
newBoard = newBoard =
model.board |> Array.map (Array.toList >> foldRowLeft_ >> Array.fromList) Array.map Tuple.second result
in in
setBoard model newBoard model
|> setBoard newBoard
|> addScore points
foldRight_ : Model -> Model foldRight_ : Model -> Model
foldRight_ model = foldRight_ model =
let let
result =
model.board |> Array.map (Array.toList >> foldRowRight_ >> Tuple.mapSecond Array.fromList)
points =
result |> Array.map Tuple.first |> Array.foldl (+) 0
newBoard = newBoard =
model.board |> Array.map (Array.toList >> foldRowRight_ >> Array.fromList) Array.map Tuple.second result
in in
setBoard model newBoard model
|> setBoard newBoard
|> addScore points
foldRowLeft_ : List Cell -> List Cell foldRowLeft_ : List Cell -> ( Int, List Cell )
foldRowLeft_ = foldRowLeft_ =
List.reverse >> foldRowRight_ >> List.reverse List.reverse >> foldRowRight_ >> Tuple.mapSecond List.reverse
foldRowRight_ : List Cell -> List Cell foldRowRight_ : List Cell -> ( Int, List Cell )
foldRowRight_ row = foldRowRight_ row =
row row
|> List.filter ((/=) Nothing) |> List.filter ((/=) Nothing)
|> List.map (Maybe.withDefault 0) |> List.map (Maybe.withDefault 0)
|> foldNumsListRight_ |> foldNumsListRight_
|> List.map Just |> Tuple.mapSecond
|> (\folded -> List.repeat (List.length row - List.length folded) Nothing ++ folded) (List.map Just
>> (\folded -> List.repeat (List.length row - List.length folded) Nothing ++ folded)
)
foldNumsListRight_ : List Int -> List Int foldNumsListRight_ : List Int -> ( Int, List Int )
foldNumsListRight_ = foldNumsListRight_ =
doFoldNumsListRight_ [ 0 ] doFoldNumsListRight_ 0 [ 0 ]
doFoldNumsListRight_ : List Int -> List Int -> List Int doFoldNumsListRight_ : Int -> List Int -> List Int -> ( Int, List Int )
doFoldNumsListRight_ result original = doFoldNumsListRight_ score result original =
case original of case original of
[] -> [] ->
List.filter ((/=) 0) result ( score, List.filter ((/=) 0) result )
_ -> _ ->
case ( List.Extra.last original, List.head result ) of case ( List.Extra.last original, List.head result ) of
( Just last, Just first ) -> ( Just last, Just first ) ->
if last == first then if last == first then
doFoldNumsListRight_ ([ 0, last + last ] ++ (List.tail result |> Maybe.withDefault [])) (pop_ original) doFoldNumsListRight_ (score + last + last) ([ 0, last + last ] ++ (List.tail result |> Maybe.withDefault [])) (pop_ original)
else else
doFoldNumsListRight_ (last :: result) (pop_ original) doFoldNumsListRight_ score (last :: result) (pop_ original)
_ -> _ ->
[] ( score, [] )
move : Model -> Direction -> Model move : Model -> Direction -> Model

View file

@ -26,11 +26,11 @@ mainColor =
score_ : Int -> Element Msg score_ : Int -> Element Msg
score_ _ = score_ score =
-- score score
-- |> String.fromInt |> String.fromInt
-- |> (++) "Score: " |> (++) "Score: "
text "Welcome :)" |> Element.text
|> Element.el [ centerY, centerX ] |> Element.el [ centerY, centerX ]
|> List.singleton |> List.singleton
|> row [ width fill, height <| fillPortion 1, paddingXY 5 5, Element.Font.bold, Element.Font.color mainColor ] |> row [ width fill, height <| fillPortion 1, paddingXY 5 5, Element.Font.bold, Element.Font.color mainColor ]