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

Commit

Permalink
Do minor code optimisation and letting
Browse files Browse the repository at this point in the history
  • Loading branch information
GiBg1aN committed Oct 16, 2017
1 parent d0b52cf commit 6e4f280
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 52 deletions.
8 changes: 3 additions & 5 deletions src/model/Board.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Board exposing (..)

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


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

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


addSnakeAndFood : Snake -> Location -> Board -> Board
Expand All @@ -27,4 +25,4 @@ addSnakeAndFood snake food board =
else
Absent
in
Matrix.mapWithLocation aux board
Matrix.mapWithLocation aux board
90 changes: 43 additions & 47 deletions src/update/UpdateUtils.elm
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ handleKeyBoardMsg move model =

direction =
arrowsDirection pressedKeys

( newModel, newCmd ) =
moveSnake direction model
in
( newModel, newCmd )
moveSnake direction model



Expand Down Expand Up @@ -58,19 +55,19 @@ moveSnake direction model =
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 == 970 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 @@ -89,28 +86,27 @@ updateSnake direction model =
if direction /= NoDirection then
List.head model.snake
|> Maybe.andThen
(\x ->
(\head ->
let
newHead =
parseHead direction x model
parseHead direction head model

increaseSnake cellLocation model cellState =
case cellState of
Absent ->
ListE.init <| cellLocation :: model.snake

PresentFood ->
Just <| cellLocation :: model.snake

PresentSnake ->
-- Inhibits the snake to go backwards.
if List.member cellLocation <| List.take 2 model.snake then
Just model.snake
else
Nothing
in
Matrix.get newHead model.board
|> Maybe.andThen
(\y ->
case y of
Absent ->
ListE.init <| newHead :: model.snake

PresentFood ->
Just <| newHead :: model.snake

PresentSnake ->
-- Inhibits the snake to go backwards.
if List.member newHead <| List.take 2 model.snake then
Just model.snake
else
Nothing
)
Matrix.get newHead model.board |> Maybe.andThen (increaseSnake newHead model)
)
else
Just model.snake
Expand All @@ -122,18 +118,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 )

0 comments on commit 6e4f280

Please sign in to comment.