first commit

This commit is contained in:
Victor Martinez 2022-07-17 19:35:46 +02:00
commit 9e211ee939
5 changed files with 264 additions and 0 deletions

10
.gitignore vendored Normal file
View file

@ -0,0 +1,10 @@
# Created by https://www.toptal.com/developers/gitignore/api/elm
# Edit at https://www.toptal.com/developers/gitignore?templates=elm
### Elm ###
# elm-package generated files
elm-stuff
# elm-repl generated files
repl-temp-*
# End of https://www.toptal.com/developers/gitignore/api/elm

25
elm.json Normal file
View file

@ -0,0 +1,25 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"mdgriffith/elm-ui": "1.1.8"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}

114
src/Game.elm Normal file
View file

@ -0,0 +1,114 @@
module Game exposing
( Board
, Cell
, Model
, initialModel
, isCellEmpty
, moveDown
, moveLeft
, moveRight
, moveUp
)
-- TYPE DEFINITIONS
type alias Model =
{ score : Int
, board : Board
}
type alias Board =
List (List Cell)
type alias Cell =
Maybe Int
isCellEmpty : Cell -> Bool
isCellEmpty cell =
case cell of
Nothing ->
True
_ ->
False
-- VALUES
boardSize_ : Int
boardSize_ =
4
initialBoard_ : Board
initialBoard_ =
List.repeat boardSize_
(List.repeat boardSize_ Nothing)
initialModel : Model
initialModel =
Model 0 initialBoard_
-- GAME RULES
generateRandomCell_ : Model -> Model
generateRandomCell_ model =
model
foldDown_ : Model -> Model
foldDown_ model =
model
foldUp_ : Model -> Model
foldUp_ model =
model
foldLeft_ : Model -> Model
foldLeft_ model =
model
foldRight_ : Model -> Model
foldRight_ model =
model
moveDown : Model -> Model
moveDown model =
model
|> foldDown_
|> generateRandomCell_
moveUp : Model -> Model
moveUp model =
model
|> foldUp_
|> generateRandomCell_
moveLeft : Model -> Model
moveLeft model =
model
|> foldLeft_
|> generateRandomCell_
moveRight : Model -> Model
moveRight model =
model
|> foldRight_
|> generateRandomCell_

107
src/Main.elm Normal file
View file

@ -0,0 +1,107 @@
module Main exposing (..)
import Browser
import Element as UI
import Game
import Html exposing (Html)
import Msg exposing (Msg)
type alias Document msg =
{ title : String
, body : List (Html msg)
}
main : Program () Game.Model Msg
main =
Browser.document
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
init : () -> ( Game.Model, Cmd Msg )
init _ =
( Game.initialModel
, Cmd.none
)
update : Msg -> Game.Model -> ( Game.Model, Cmd Msg )
update msg model =
case msg of
Msg.ArrowDown ->
( model |> Game.moveDown
, Cmd.none
)
Msg.ArrowUp ->
( model |> Game.moveUp
, Cmd.none
)
Msg.ArrowLeft ->
( model |> Game.moveLeft
, Cmd.none
)
Msg.ArrowRight ->
( model |> Game.moveRight
, Cmd.none
)
view : Game.Model -> Document Msg
view model =
{ title = "2048"
, body = view_ model
}
view_ : Game.Model -> List (Html Msg)
view_ model =
[ UI.layout [] <|
UI.column []
[ viewScore_ model.score
, viewBoard_ model.board
]
]
viewScore_ : Int -> UI.Element Msg
viewScore_ score =
score
|> String.fromInt
|> UI.text
|> List.singleton
|> UI.row []
viewBoard_ : Game.Board -> UI.Element Msg
viewBoard_ board =
board
|> List.map viewBoardRow_
|> UI.column []
viewBoardRow_ : List Game.Cell -> UI.Element Msg
viewBoardRow_ cells =
cells
|> List.map viewCell_
|> UI.row []
viewCell_ : Game.Cell -> UI.Element Msg
viewCell_ cell =
let
value : String
value =
cell
|> Maybe.map String.fromInt
|> Maybe.withDefault "-"
in
UI.column []
[ UI.text value ]

8
src/Msg.elm Normal file
View file

@ -0,0 +1,8 @@
module Msg exposing (Msg(..))
type Msg
= ArrowUp
| ArrowDown
| ArrowLeft
| ArrowRight