Skip to content
This repository has been archived by the owner on Jun 4, 2021. It is now read-only.

Commit

Permalink
Add Constants module and general refactor
Browse files Browse the repository at this point in the history
Now every common hardcoded parameter have been encoded in a variable in a
proper module.
  • Loading branch information
GiBg1aN committed Oct 19, 2017
1 parent 8863d9b commit f16bc2d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/model/Board.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Board exposing (..)

import Cell exposing (..)
import Constants
import Matrix exposing (Location, Matrix)
import Snake exposing (..)

Expand All @@ -11,7 +12,7 @@ type alias Board =

initMatrix : Board
initMatrix =
addSnakeAndFood initSnake ( 1, 1 ) <| Matrix.square 10 (\_ -> Absent)
addSnakeAndFood initSnake Constants.foodStartingPoint <| Matrix.square Constants.boardSize (\_ -> Absent)


addSnakeAndFood : Snake -> Location -> Board -> Board
Expand All @@ -25,4 +26,4 @@ addSnakeAndFood snake food board =
else
Absent
in
Matrix.mapWithLocation aux board
Matrix.mapWithLocation aux board
16 changes: 16 additions & 0 deletions src/model/Constants.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Constants exposing (..)


boardSize : number
boardSize =
10


foodStartingPoint : ( number, number )
foodStartingPoint =
( 1, 1 )


maxScore : number
maxScore =
970
3 changes: 2 additions & 1 deletion src/model/Model.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Model exposing (..)

import Board exposing (..)
import Constants
import Keyboard.Extra as KE exposing (Direction(..), Key, arrowsDirection)
import Matrix exposing (Location)
import Snake exposing (..)
Expand Down Expand Up @@ -33,7 +34,7 @@ init =
, snake = initSnake
, pressedKeys = []
, lastMove = West
, foodLocation = ( 1, 1 )
, foodLocation = (Constants.foodStartingPoint)
, speed = Time.second
}
, Cmd.none
Expand Down
10 changes: 3 additions & 7 deletions src/model/Snake.elm
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ testingSnake n =
testingSnakeAux <| n - 1


normalize : Int -> Int
normalize length =
let
startingLength =
List.length initSnake
in
(length - startingLength) * 10
calcScore : Int -> Int
calcScore snakeLength =
(snakeLength - List.length initSnake) * 10
61 changes: 31 additions & 30 deletions src/update/UpdateUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module UpdateUtils exposing (handleFoodMsg, handleKeyBoardMsg, moveSnake)

import Board exposing (addSnakeAndFood)
import Cell exposing (..)
import Constants
import Keyboard.Extra as KE exposing (Direction(..), Key, arrowsDirection)
import List.Extra as ListE exposing (init)
import Matrix exposing (Location)
import Model exposing (..)
import Random exposing (Generator, int, pair)
import Snake exposing (Snake, normalize)
import Snake exposing (Snake, calcScore)
import Status exposing (..)
import Time

Expand All @@ -21,7 +22,7 @@ handleKeyBoardMsg move model =
direction =
arrowsDirection pressedKeys
in
moveSnake direction model
moveSnake direction model



Expand All @@ -31,7 +32,7 @@ handleKeyBoardMsg move model =
handleFoodMsg : Location -> Model -> ( Model, Cmd Msg )
handleFoodMsg food model =
if List.member food model.snake then
( model, Random.generate Food <| pair (int 0 9) (int 0 9) )
( model, Random.generate Food <| pair (int 0 (Constants.boardSize - 1)) (int 0 (Constants.boardSize - 1)) )
else
( { model
| foodLocation = food
Expand All @@ -50,24 +51,24 @@ moveSnake direction model =
isEaten model.snake xs

score =
List.length >> normalize <| xs
List.length >> calcScore <| xs

newSpeed =
Time.second * 1 / logBase 2 (toFloat <| 5 * (score // 100) + 2)
in
if xs == model.snake then
( model, newMessage )
else if score == 970 then
( { model | status = Win }, Cmd.none )
else
( { model
| board = addSnakeAndFood xs model.foodLocation model.board
, snake = xs
, lastMove = direction
, speed = newSpeed
}
, newMessage
)
if xs == model.snake then
( model, newMessage )
else if score == Constants.maxScore then
( { model | status = Win }, Cmd.none )
else
( { model
| board = addSnakeAndFood xs model.foodLocation model.board
, snake = xs
, lastMove = direction
, speed = newSpeed
}
, newMessage
)

Nothing ->
( { model | status = Lost }, Cmd.none )
Expand All @@ -78,7 +79,7 @@ isEaten oldSnake newSnake =
if List.length oldSnake == List.length newSnake then
Cmd.none
else
Random.generate Food <| pair (int 0 9) (int 0 9)
Random.generate Food <| pair (int 0 (Constants.boardSize - 1)) (int 0 (Constants.boardSize - 1))


updateSnake : Direction -> Model -> Maybe Snake
Expand Down Expand Up @@ -106,7 +107,7 @@ updateSnake direction model =
else
Nothing
in
Matrix.get newHead model.board |> Maybe.andThen (increaseSnake newHead model)
Matrix.get newHead model.board |> Maybe.andThen (increaseSnake newHead model)
)
else
Just model.snake
Expand All @@ -118,18 +119,18 @@ parseHead direction location model =
( i, j ) =
location
in
case direction of
North ->
( (i - 1) % 10, j )
case direction of
North ->
( (i - 1) % 10, j )

South ->
( (i + 1) % 10, j )
South ->
( (i + 1) % 10, j )

West ->
( i, (j - 1) % 10 )
West ->
( i, (j - 1) % 10 )

East ->
( i, (j + 1) % 10 )
East ->
( i, (j + 1) % 10 )

_ ->
( i, j )
_ ->
( i, j )
4 changes: 2 additions & 2 deletions src/view/Score.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Score exposing (view)

import Html exposing (text)
import Model exposing (..)
import Snake exposing (normalize)
import Snake exposing (calcScore)


view : Model -> Html.Html msg
view model =
List.length model.snake |> (normalize >> toString >> text)
List.length model.snake |> (calcScore >> toString >> text)

0 comments on commit f16bc2d

Please sign in to comment.