From 14612a40e447ceb90e6c0590157e8afab21d9f8a Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sat, 8 Feb 2020 15:42:46 +0100 Subject: [PATCH 1/2] Add links to chapters 3 and 4 --- docs/tutorial/02-draw-a-snake.md | 2 ++ docs/tutorial/03-let-the-snake-move.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/tutorial/02-draw-a-snake.md b/docs/tutorial/02-draw-a-snake.md index cddc843..87eb7de 100644 --- a/docs/tutorial/02-draw-a-snake.md +++ b/docs/tutorial/02-draw-a-snake.md @@ -252,3 +252,5 @@ snake = %{body: [{9, 9}, {10, 9}, {11, 9}], size: 3} ``` ![snake of 3 tiles on game screen](./../images/03-three-tile-snake.png) + +[Let's make the snake move](./03-let-the-snake-move.md) diff --git a/docs/tutorial/03-let-the-snake-move.md b/docs/tutorial/03-let-the-snake-move.md index c364450..03bf77d 100644 --- a/docs/tutorial/03-let-the-snake-move.md +++ b/docs/tutorial/03-let-the-snake-move.md @@ -176,3 +176,5 @@ config :snake, :viewport, %{ ``` ![snake moving](./../images/04-moving-snake.gif) + +[Let's control the snake](./04-control-snake-movements.md) From 0d543f635a25e78160c821e0c37fa7c5068612de Mon Sep 17 00:00:00 2001 From: Guilherme Date: Sat, 8 Feb 2020 15:44:04 +0100 Subject: [PATCH 2/2] Code suggestion for snake moving --- docs/tutorial/03-let-the-snake-move.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/tutorial/03-let-the-snake-move.md b/docs/tutorial/03-let-the-snake-move.md index 03bf77d..1c1c9e2 100644 --- a/docs/tutorial/03-let-the-snake-move.md +++ b/docs/tutorial/03-let-the-snake-move.md @@ -109,13 +109,13 @@ Now we're ready to implement the movement logic. Let's recall what happens durin defp move_snake(%{snake: snake} = state) do %{body: body, direction: direction} = snake - # new head + # new head's position [head | _] = body new_head = move(state, head, direction) - # truncate body - size = length(body) - new_body = Enum.take([new_head | body], size) + # place a new head on the tile that we want to move to + # and remove the last tile from the snake tail + new_body = List.delete_at([new_head | body], -1) state |> put_in([:snake, :body], new_body)