From 34532d1ffc4ae89d84d82d8d3736aac9fc11cb00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constantin=20F=C3=BCrst?= Date: Wed, 1 May 2024 16:20:40 +0200 Subject: [PATCH] implement lesson06 --- LESSONS.md | 26 +++++++++++++++++++++++++- components/Snake.cpp | 25 +++++++++++++++++++++++++ components/Snake.h | 4 ++++ main.cpp | 22 +++++++++++++++++++++- 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/LESSONS.md b/LESSONS.md index 3995cfb..5f03faf 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -112,4 +112,28 @@ This one is quite difficult. Don't worry if it takes a lot of time and if you're 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_. \ No newline at end of file +Once you're ready, you can _git merge lesson06_. + +--- + +## Lesson 06 + +#### Prequisites +None + +#### Files +- _components/Snake.cpp_ +- _components/Snake.h_ +- _main.cpp_ + +#### Task + +Our snake seems invincible for now and can't eat. Run into a wall - nothing will happen. Bite yourself - no injury. Try to eat the snack - you will just slither over it. Sad Snake :( + +1) See the changes to _components/Snake.*_ and try to implement the test functions which we will use later on. +2) After implementing them, move to _main.cpp_ where __mainL06__ will allow you to test the newly written functions. +3) Figure out how you can test the bit-itself and bit-snack by yourself. + +Once you're ready, you can _git merge lesson07_. + +--- \ No newline at end of file diff --git a/components/Snake.cpp b/components/Snake.cpp index 92890c4..b474265 100644 --- a/components/Snake.cpp +++ b/components/Snake.cpp @@ -81,3 +81,28 @@ void Snake::moveRight(){ direction_ = RIGHT; move(); } + +bool Snake::isBitten() const { + const Point& head = snake_.front(); + + // TODO: return true if any point of the snake has the same position as the head + // remember to skip the head as the snake is not flexible enough to bite off its own head + + return false; +} + +bool Snake::hasBitSnack(uint32_t snackY, uint32_t snackX) const { + const Point& head = snake_.front(); + + // TODO: return true if the heads position is on the same coordinates as snack + // and return false otherwise +} + +bool Snake::hasCrashedWall() const { + const Point& head = snake_.front(); + + return (head.getY() < GAME_TOP_WALL_Y) || + (head.getY() > GAME_BOTTOM_WALL_Y) || + (head.getX() < GAME_LEFT_WALL_X) || + (head.getX() > GAME_RIGHT_WALL_X); +} \ No newline at end of file diff --git a/components/Snake.h b/components/Snake.h index 5aa646c..fc8bf5a 100644 --- a/components/Snake.h +++ b/components/Snake.h @@ -24,4 +24,8 @@ public: void moveLeft(); void moveRight(); void move(); + + bool isBitten() const; + bool hasBitSnack(uint32_t snackY, uint32_t snackX) const; + bool hasCrashedWall() const; }; diff --git a/main.cpp b/main.cpp index 9358868..17abfd2 100755 --- a/main.cpp +++ b/main.cpp @@ -85,7 +85,27 @@ void mainL04() { std::cout << "Helper QUIT" << std::endl; } +void mainL06() { + Graphics::get().init("Learners Helper 06"); + + Snake snake; + + // TODO: Now the snake will eventually crash into the lower wall which we detect and then abort + // TODO: Figure out how to test the bit-itself and bit-snack functions yourself + + while(true) { + snake.moveDown(); + Graphics::get().refreshScreen(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + if (snake.hasCrashedWall()) break; + } + + Graphics::get().finalize(); + + std::cout << "Helper QUIT" << std::endl; +} + int main() { - mainL04(); + mainL06(); return 0; } \ No newline at end of file