Browse Source

implement lesson05

lesson05
Constantin Fürst 6 months ago
parent
commit
097da7607e
  1. 26
      LESSONS.md
  2. 5
      components/Snake.cpp

26
LESSONS.md

@ -88,4 +88,28 @@ Now things are getting serious! The snake has smelled our delicous food and is a
3) Even though our snake is far from complete, we want to see it! As we decided the snake to be made up of a list of points, use a modern for-each loop to call _print()_ on all points of the snakes body. 3) Even though our snake is far from complete, we want to see it! As we decided the snake to be made up of a list of points, use a modern for-each loop to call _print()_ on all points of the snakes body.
4) You have already interacted with std::vector by now. Look at the documentation for it and figure out its purpose and what else you can do with it. Play around! 4) You have already interacted with std::vector by now. Look at the documentation for it and figure out its purpose and what else you can do with it. Play around!
Once you're ready, you can _git merge lesson05_.
Once you're ready, you can _git merge lesson05_.
---
## Lesson 05
#### Prequisites
- Iterators/Iterating over Elements of Container
#### Files
- _components/Snake.cpp_
- _components/Snake.h_
- _main.cpp_
#### Task
The snake showed up but something is wrong. Its growing constantly without even having food. Lets fix that as we want to be super biologically accurate: no growth without energy. And as our snake can't eat yet, it should not grow at all.
This one is quite difficult. Don't worry if it takes a lot of time and if you're really stuck, try the master branch with its solution.
1) The culprit is our snakes _Snake::move()_ function which we must fix in order for the snake to stop growing endlessly.
2) There are some hints written as todos but these leave a lot of room for the implementation. If you need some help, search for iterators (over std::vector) or use a for-loop over all indices from the back. These are just suggestions - try to come up with your own solution!
3) Try this out in main where you can still use the __mainL04__ from the previous task.
Once you're ready, you can _git merge lesson06_.

5
components/Snake.cpp

@ -42,6 +42,11 @@ void Snake::move() {
auto head = snake_.begin(); auto head = snake_.begin();
auto second = std::next(snake_.begin()); auto second = std::next(snake_.begin());
// TODO: the current tail of our snake shoudl be hidden
// TODO: each element, from the tail to the second-to-head
// should move to the position of the element before it
// update the previous to head node // update the previous to head node
// by copying from head and setting // by copying from head and setting
// the image to be body instead of head // the image to be body instead of head

Loading…
Cancel
Save