Skip to content

Commit 2a7ff11

Browse files
committed
Wip
1 parent c71c1c7 commit 2a7ff11

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
### 6. Allow snake to eat
2+
3+
We have the food, but our snake can't eat it yet, and we don't want to starve our little friend.
4+
5+
The goal of this chapter is to allow the snake to eat the food and grow, but let's first quickly recap how it works. The snake eats the food when it overlaps it with its head, as soon the snake eats it, it will grow of one unit (a new tile will be appended to its body) and a new food pellet will be placed in the snake world.
6+
7+
In other words, we need to check if the snake's head has the same coordinates of the pellet after every movement. The function `move_snake/1` looks the natural place where to execute this check.
8+
9+
```elixir
10+
defp move_snake(%{snake: snake} = state) do
11+
%{body: body, size: size, direction: direction} = snake
12+
13+
# New head's position
14+
[head | _] = body
15+
new_head = move(state, head, direction)
16+
17+
# Place a new head on the tile that we want to move to
18+
# and remove the last tile from the snake tail
19+
new_body = List.delete_at([new_head | body], -1)
20+
21+
state
22+
|> put_in([:objects, :snake, :body], new_body)
23+
|> maybe_eat_food(new_head)
24+
end
25+
```
26+
27+
👆Let's add it at the end of the state update pipeline, the pipe operator `|>` comes in handy here 💚
28+
29+
Our new function `maybe_eat_food/2` receives:
30+
31+
- The current state as 1st argument
32+
- The snake's head coordinates as 2nd argument
33+
34+
Then, in the case the snake's head overlaps the food pellet, the function will take care of:
35+
36+
- Grow the snake body of one unit
37+
- Place a new food pellet in the snake world
38+
39+
or otherwise, just return the current state without any changes.
40+
41+
Let's draft out the `maybe_eat_pellet/2` function and implement it step by step.
42+
43+
```elixir
44+
def maybe_eat_pellet(state = %{pellet: pellet}, snake_head) do
45+
if (pellet == snake_head)
46+
state
47+
|> grow_snake()
48+
|> place_pellet()
49+
else
50+
state
51+
end
52+
end
53+
```
54+
55+
Growing the snake body can be tricky, let's explore all our alternatives.
56+
57+
Prepending a new tile to the snake head is not a feasible solution because it will potentially lead to unexpected outcome like its dead :skull:.
58+
59+
The most natural approach is to append a new tile at the end of the snake body, but how exactly? We can not simply add a new tile at the end like: `Enum.concat(body, [{x, y}])` since we don't have any information of the tail direction but only of its head. In other words, we can't infer the coordinates (`{x, y}`) of the tile to append. The only way to safely grow our snake it's to preserve its body when the snake ate the pellet. We could set a boolean flag `has_eaten` in the state and in the next game tick, don't delete the last snake's body tile when this flag is true 🤓.
60+
61+
Remember, we set a timer at the beginning in out `init/1` function that periodically sends a message, which is intercepted by our `handle_info/2`, which in turn calls the `move_snake/1` function. That's our game tick.
62+
63+
```elixir
64+
defp move_snake(%{snake: snake} = state) do
65+
%{body: body, size: size, direction: direction} = snake
66+
67+
# New head's position
68+
[head | _] = body
69+
new_head = move(state, head, direction)
70+
71+
# Place a new head on the tile that we want to move to
72+
# and remove the last tile from the snake tail
73+
new_body = List.delete_at([new_head | body], -1)
74+
75+
state
76+
|> put_in([:snake, :body], new_body)
77+
|> maybe_eat_food(new_head)
78+
end
79+
80+
def maybe_eat_pellet(state = %{pellet: pellet}, snake_head) do
81+
if (pellet == snake_head)
82+
state
83+
|> grow_snake()
84+
|> place_pellet()
85+
else
86+
state
87+
end
88+
end
89+
90+
def grow_snake(state = %{%{snake: %{size: size}}) do
91+
put_in(state, [:snake, :has_eaten], true)
92+
end
93+
94+
def place_pellet(state = %{width: width, height: height, snake: %{body: snake_body}}) do
95+
pellet_coords = {
96+
Enum.random(0..(width - 1)),
97+
Enum.random(0..(height - 1))
98+
}
99+
100+
if pellet_coords in snake_body do
101+
place_pellet(state)
102+
else
103+
put_in(state, [:objects, :pellet], pellet_coords)
104+
end
105+
end
106+
```
107+
108+
Let's take a look to these two new functions:
109+
110+
- `grow_snake/1` simply sets the `:has_eatan` flag to true in the state
111+
- `place_pellet/1` computes a new pair of coordinates for the food, if the new value matches any tile in the snake's body, it recursively generate a new position until it does not overlap the snake
112+
113+
We still need to update the `move_snake/1` function to use the `:has_eatan` flag.
114+
115+
```elixir
116+
defp move_snake(%{snake: snake} = state) do
117+
%{body: body, direction: direction} = snake
118+
119+
# New head's position
120+
[head | _] = body
121+
new_head = move(state, head, direction)
122+
123+
# Place a new head on the tile that we want to move to
124+
# and remove the last tile from the snake tail if it has not eaten any pellet
125+
new_body = [new_head | body]
126+
new_body = if snake.has_eaten, do: new_body, else: List.delete_at(new_body, -1)
127+
128+
state
129+
|> put_in([:snake, :body], new_body)
130+
|> maybe_eat_food(new_head)
131+
end
132+
```
133+
134+
Like that, when the flag `:has_eatan` is true, the last tile from the snake's body is not removed anymore. This is the trick that allows us to grow the snake 💪
135+
136+
And now let's run the game and see how if our snake grows when eating.
137+
138+
$ mix scenic.run
139+
140+
Oh snapp! Our snake is growing infinitely!! We forgot to reset the `:has_eaten` flag 🙈
141+
142+
```elixir
143+
defp move_snake(%{snake: snake} = state) do
144+
%{body: body, direction: direction} = snake
145+
146+
# New head's position
147+
[head | _] = body
148+
new_head = move(state, head, direction)
149+
150+
# Place a new head on the tile that we want to move to
151+
# and remove the last tile from the snake tail if it has not eaten any pellet
152+
new_body = [new_head | body]
153+
new_body = if snake.has_eaten, do: new_body, else: List.delete_at(new_body, -1)
154+
155+
state
156+
|> put_in([:snake, :body], new_body)
157+
|> put_in([::snake, :has_eaten], false) # Reset the `:has_eaten` flag before the next check
158+
|> maybe_eat_food(new_head)
159+
end
160+
```
161+
162+
Ok, let's run it again, it should work like a charm now 🤞
163+
164+
TODO:
165+
166+
- add gif

0 commit comments

Comments
 (0)