Compare commits
merge into: constantin:master
constantin:lesson02
constantin:lesson03
constantin:lesson04
constantin:lesson05
constantin:lesson06
constantin:lesson07
constantin:lesson08
constantin:lessons
constantin:master
pull from: constantin:lesson07
constantin:lesson02
constantin:lesson03
constantin:lesson04
constantin:lesson05
constantin:lesson06
constantin:lesson07
constantin:lesson08
constantin:lessons
constantin:master
18 Commits
14 changed files with 366 additions and 371 deletions
-
160LESSONS.md
-
27components/Point.cpp
-
1components/Point.h
-
17components/Snack.cpp
-
0components/Snack.h
-
167components/Snake.cpp
-
0components/Snake.h
-
74game/Controller.cpp
-
31game/Controller.h
-
64game/Game.cpp
-
27game/Game.h
-
32input-output/Player.cpp
-
21input-output/Player.h
-
110main.cpp
@ -0,0 +1,160 @@ |
|||
## Lesson 01 |
|||
|
|||
#### Prequisites |
|||
- Classes |
|||
- Functions |
|||
|
|||
#### Files |
|||
- _components/Point.cpp_ |
|||
- _components/Point.h_ |
|||
- _main.cpp_ |
|||
|
|||
#### Task |
|||
1) With the point-class we represent a single dot on screen, giving it a position and character as "image". Begin by inspecting _components/Point.h_ and _components/Point.cpp_ and figure out what a header (.h) and code (.cpp) file contain and how they differ. |
|||
2) When an instance of a class in C++ is created, its constructor is used to initialize values. At some point, the created instance will get destroyed which uses the destructor. Read about these two and implement them in _components/Point.cpp_. |
|||
3) Select to call __mainL01__ in __main__ of _main.cpp_ and think or read about the purpose of this function as the applications entry point. |
|||
4) See what happens when you run the application now and play around with the point or maybe create multiple. You can also modify the destructor to do something special (like print to the command line). |
|||
5) Go to _components/Point.*_ again and look at the member variables x,y and img. Notice that they are defined in a "private" region. Try to modify these directly in the main function __mainL01__. If that does not work, you can implement the getters and setters marked with TODO in _components/Point.cpp_ |
|||
6) Continue exploring around with the point and see what happens. |
|||
|
|||
Once you're ready, you can _git merge lesson02_. |
|||
|
|||
--- |
|||
|
|||
## Lesson 02 |
|||
|
|||
#### Prequisites |
|||
- Random Numbers |
|||
- Functions |
|||
- Pointers/References |
|||
|
|||
#### Files |
|||
- _components/Snack.cpp_ |
|||
- _components/Snack.h_ |
|||
- _main.cpp_ |
|||
|
|||
#### Task |
|||
Every little snake needs to eat. And no snake will appear without food, which is why we begin by providing this essential item. |
|||
|
|||
1) We want to implement the _Snack_ as an instance of the Point-Class from the previous Task. Therefore we will write a function that turns an ordinary Point into a delicious Snack. This will be called _generateSnack_ and we will implement it in _components/Snack.*_ where you can see the skeleton for the function. |
|||
2) A Snack must be placed at a random point when generating, so set its x and y value to a random value that fits on the playing field (see the comment in the function skeleton for a hint). |
|||
3) A Snack must also look enticing, so set its image to the one defined as SNACK_CHAR and then display the delicious snack. |
|||
4) Experiment and use __mainL02__ to test if your code works. |
|||
5) Notice anything interesting with how we pass the Point to the generateSnack-function? The function requests a pointer which is the address of an object instance. To get the address of an existing object, we use operator&. Read up on pointers and references. |
|||
|
|||
Once you're ready, you can _git merge lesson03_. |
|||
|
|||
--- |
|||
|
|||
## Lesson 03 |
|||
|
|||
#### Prequisites |
|||
- Loops |
|||
|
|||
#### Files |
|||
- _components/Point.cpp_ |
|||
- _components/Point.h_ |
|||
- _main.cpp_ |
|||
|
|||
#### Task |
|||
|
|||
This Task will be a little on the short side. We plan to implement our Snake as a collection of points on the screen and for that, each point should be able to move by itself. This is the objective of this lesson. |
|||
|
|||
1) Some new functions have appeared in _components/Point.*_ which you should inspect and try to implement. |
|||
2) Go to _main.cpp_ and see the new __mainL03__ which will use the movement functions now implemented to make one point move across the screen diagonally. Try it out! |
|||
3) As you can see, we use a for-loop for this but C++ has some other types of loop which you should familiarize yourself with. Try and play around with the commented-out loops and get the same result by using while and do-while loops. |
|||
|
|||
Once you're ready, you can _git merge lesson04_. |
|||
|
|||
--- |
|||
|
|||
## Lesson 04 |
|||
|
|||
#### Prequisites |
|||
- Arrays |
|||
- Switch-Statement |
|||
|
|||
#### Files |
|||
- _components/Snake.cpp_ |
|||
- _components/Snake.h_ |
|||
- _main.cpp_ |
|||
|
|||
#### Task |
|||
|
|||
Now things are getting serious! The snake has smelled our delicous food and is appearing - beware its poisonous fangs. |
|||
|
|||
1) You will notice the new component _components/Snake.*_ which you should inspect closely. |
|||
2) C++ has whats called a switch-statement which acts like many if-else-if-else chained. Look at _Snake::updateHead_ and play around with it to figure out how a switch works. |
|||
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! |
|||
|
|||
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_. |
|||
|
|||
--- |
|||
|
|||
## 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_. |
|||
|
|||
--- |
|||
|
|||
## Lesson 07 |
|||
|
|||
#### Prequisites |
|||
None |
|||
|
|||
#### Files |
|||
- _components/Snake.cpp_ |
|||
- _components/Snake.h_ |
|||
- _main.cpp_ |
|||
|
|||
#### Task |
|||
|
|||
The snake just learned to grow by itself! How? |
|||
|
|||
1) See the changes to _components/Snake.*_ and try to understand the new code which allows the snake to grow. |
|||
2) Play around with this in _main.cpp_. |
|||
|
|||
Once you're ready, you can _git merge lesson08_. |
|||
|
|||
--- |
@ -1,17 +1,8 @@ |
|||
#include "Snack.h"
|
|||
|
|||
#include <random>
|
|||
|
|||
void generateSnack(Point* snack){ |
|||
std::random_device dev; |
|||
static std::mt19937 prng(dev()); |
|||
static std::uniform_int_distribution<uint32_t> distX(0, GAME_RIGHT_WALL_X); |
|||
static std::uniform_int_distribution<uint32_t> distY(0, GAME_BOTTOM_WALL_Y); |
|||
|
|||
const uint32_t x = distX(prng); |
|||
const uint32_t y = distY(prng); |
|||
|
|||
snack->setImg(SNACK_CHAR); |
|||
snack->setPoint(y, x); |
|||
snack->print(); |
|||
// TODO: implement me
|
|||
// should set a random position for the point, update its character/image and display the point
|
|||
// x should be between (0, GAME_RIGHT_WALL_X)
|
|||
// y should be between (0, GAME_BOTTOM_WALL_Y)
|
|||
} |
@ -1,74 +0,0 @@ |
|||
#include "Controller.h"
|
|||
|
|||
#include <string>
|
|||
#include <iostream>
|
|||
|
|||
Controller::Controller() |
|||
: input_{0}, score_{0}, snack_{Point(0,0,0)} |
|||
{ |
|||
generateSnack(&snack_); |
|||
} |
|||
|
|||
uint32_t Controller::getCurrScore() const { |
|||
return score_; |
|||
} |
|||
|
|||
void Controller::resetScore() { |
|||
score_ = 0; |
|||
} |
|||
|
|||
void Controller::printScore(uint32_t score) const { |
|||
const std::string str = "Score: " + std::to_string(score); |
|||
// locate message at (-1,-1) because otherwise it'll be printed inside the game box
|
|||
Graphics::get().printMsg(-1, -1, str); |
|||
} |
|||
|
|||
int Controller::act() { |
|||
if (snake_.hasBitSnack(snack_.getY(), snack_.getX())) { |
|||
score_ += 10; |
|||
snake_.incSize(); |
|||
|
|||
generateSnack(&snack_); |
|||
Graphics::get().advanceDifficulty(); |
|||
|
|||
printScore(score_); |
|||
} |
|||
|
|||
switch (input_) { |
|||
case UP: |
|||
snake_.moveUp(); |
|||
Graphics::get().setVertical(true); |
|||
break; |
|||
case DOWN: |
|||
snake_.moveDown(); |
|||
Graphics::get().setVertical(true); |
|||
break; |
|||
case LEFT: |
|||
snake_.moveLeft(); |
|||
Graphics::get().setVertical(false); |
|||
break; |
|||
case RIGHT: |
|||
snake_.moveRight(); |
|||
Graphics::get().setVertical(false); |
|||
break; |
|||
default: |
|||
snake_.move(); |
|||
} |
|||
|
|||
Graphics::get().refreshScreen(); |
|||
|
|||
if (snake_.isBitten() || snake_.hasCrashedWall()) { |
|||
return DEFEAT; |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
int Controller::readInput() { |
|||
input_ = Graphics::get().readInpt(); |
|||
return input_; |
|||
} |
|||
|
|||
bool Controller::wantsToQuit() const { |
|||
return input_ == EXIT_GAME; |
|||
} |
@ -1,31 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include <memory> |
|||
|
|||
#include "../input-output/Graphics.h" |
|||
#include "../components/Snake.h" |
|||
#include "../components/Point.h" |
|||
|
|||
static constexpr int DEFEAT = -1; |
|||
|
|||
class Controller { |
|||
private: |
|||
Snake snake_; |
|||
Point snack_; |
|||
|
|||
int input_; |
|||
uint32_t score_; |
|||
|
|||
void printScore(uint32_t score) const; |
|||
|
|||
public: |
|||
Controller(); |
|||
|
|||
uint32_t getCurrScore() const; |
|||
|
|||
void resetScore(); |
|||
int readInput(); |
|||
int act(); |
|||
|
|||
bool wantsToQuit() const; |
|||
}; |
@ -1,64 +0,0 @@ |
|||
#include "Game.h"
|
|||
|
|||
#include <algorithm>
|
|||
#include <limits.h>
|
|||
|
|||
void SnakeGame::addPlayer(const std::string& name){ |
|||
players_.emplace_back(Player{name}); |
|||
} |
|||
|
|||
uint32_t SnakeGame::getHighScore() const { |
|||
return high_score_; |
|||
} |
|||
|
|||
const std::string& SnakeGame::getBestPlayer() const { |
|||
return best_player_; |
|||
} |
|||
|
|||
void SnakeGame::play(const std::string& name){ |
|||
auto find = std::find_if(players_.begin(), players_.end(), [&name](const Player& p){ return p.getName() == name; }); |
|||
|
|||
if (find == players_.end()) { |
|||
players_.emplace_back(Player(name)); |
|||
find = std::prev(players_.end()); |
|||
} |
|||
|
|||
Graphics::get().init(game_name_); |
|||
|
|||
find->play(); |
|||
|
|||
Graphics::get().finalize(); |
|||
|
|||
|
|||
if (find->getHighScore() > high_score_){ |
|||
high_score_ = find->getHighScore(); |
|||
best_player_ = find->getName(); |
|||
} |
|||
|
|||
std::cout << "Highscore: " << high_score_ << " by " << best_player_ << std::endl; |
|||
} |
|||
|
|||
void SnakeGame::play(){ |
|||
while(1) { |
|||
std::string name; |
|||
std::cout << "Who's playing: "; |
|||
std::cin >> name; |
|||
std::cout << std::endl; |
|||
|
|||
|
|||
play(name); |
|||
std::cin.clear(); |
|||
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); |
|||
std::cout << "Do you or someone else want to play again? (yes or no): "; |
|||
std::string ans; |
|||
|
|||
std::cin >> ans; |
|||
|
|||
if (ans != "yes") { |
|||
std::cout << "Exiting ..." << std::endl; |
|||
break; |
|||
} |
|||
|
|||
std::cout << "Perfect..." << std::endl; |
|||
} |
|||
} |
@ -1,27 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <vector> |
|||
|
|||
#include "Controller.h" |
|||
#include "../components/Snake.h" |
|||
#include "../input-output/Player.h" |
|||
|
|||
class SnakeGame{ |
|||
private: |
|||
const std::string game_name_ = "Snake Game for C++ Course"; |
|||
std::vector<Player> players_; |
|||
|
|||
uint32_t high_score_ = 0; |
|||
std::string best_player_ = "None"; |
|||
|
|||
void play(const std::string& name); |
|||
void addPlayer(const std::string& name); |
|||
|
|||
public: |
|||
uint32_t getHighScore() const; |
|||
const std::string& getBestPlayer() const; |
|||
void printGameStatistics() const; |
|||
|
|||
void play(); |
|||
}; |
@ -1,32 +0,0 @@ |
|||
#include "Player.h"
|
|||
|
|||
Player::Player(std::string name) |
|||
: name_{name} |
|||
{} |
|||
|
|||
const std::string& Player::getName() const { |
|||
return name_; |
|||
} |
|||
|
|||
uint32_t Player::getHighScore() const { |
|||
return high_score_; |
|||
} |
|||
|
|||
void Player::play(){ |
|||
Controller controller; |
|||
|
|||
while (controller.wantsToQuit() == false) { |
|||
controller.readInput(); |
|||
|
|||
if (controller.act() == DEFEAT) { |
|||
break; |
|||
} |
|||
} |
|||
|
|||
const uint32_t score = controller.getCurrScore(); |
|||
controller.resetScore(); |
|||
|
|||
if (score > high_score_) { |
|||
high_score_ = score; |
|||
} |
|||
} |
@ -1,21 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include "../game/Controller.h" |
|||
|
|||
#include <iostream> |
|||
#include <string> |
|||
|
|||
class Player{ |
|||
private: |
|||
std::string name_; |
|||
uint32_t high_score_ = 0; |
|||
uint32_t timesPlayed_ = 0; |
|||
|
|||
public: |
|||
Player(std::string name); |
|||
|
|||
void play(); |
|||
|
|||
uint32_t getHighScore() const; |
|||
const std::string& getName() const; |
|||
}; |
@ -1,11 +1,111 @@ |
|||
|
|||
#include <ncurses.h>
|
|||
#include <iostream>
|
|||
#include <thread>
|
|||
#include <chrono>
|
|||
|
|||
#include "components/Point.h"
|
|||
#include "components/Snack.h"
|
|||
#include "components/Snake.h"
|
|||
#include "input-output/Graphics.h"
|
|||
|
|||
void mainL01() { |
|||
Graphics::get().init("Learners Helper"); |
|||
|
|||
Point p(10,10,'X'); |
|||
p.print(); |
|||
|
|||
Graphics::get().refreshScreen(); |
|||
std::this_thread::sleep_for(std::chrono::seconds(10)); |
|||
|
|||
Graphics::get().finalize(); |
|||
|
|||
std::cout << "Helper QUIT" << std::endl; |
|||
} |
|||
|
|||
void mainL02() { |
|||
Graphics::get().init("Learners Helper 02"); |
|||
|
|||
Point p(10,10); |
|||
generateSnack(&p); |
|||
|
|||
Graphics::get().refreshScreen(); |
|||
std::this_thread::sleep_for(std::chrono::seconds(10)); |
|||
|
|||
Graphics::get().finalize(); |
|||
|
|||
std::cout << "Helper QUIT" << std::endl; |
|||
} |
|||
|
|||
void mainL03() { |
|||
Graphics::get().init("Learners Helper 03"); |
|||
|
|||
Point p(10,10); |
|||
|
|||
for (uint32_t i = 0; i < 100; i++) { |
|||
p.moveDown(); |
|||
Graphics::get().refreshScreen(); |
|||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|||
p.moveRight(); |
|||
Graphics::get().refreshScreen(); |
|||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|||
} |
|||
|
|||
/*
|
|||
while(...) { |
|||
// TODO: play with this
|
|||
} |
|||
*/ |
|||
|
|||
/*
|
|||
do { |
|||
// TODO: play with this
|
|||
} while (...) |
|||
*/ |
|||
|
|||
Graphics::get().finalize(); |
|||
|
|||
std::cout << "Helper QUIT" << std::endl; |
|||
} |
|||
|
|||
void mainL04() { |
|||
Graphics::get().init("Learners Helper 04"); |
|||
|
|||
Snake snake; |
|||
|
|||
for (uint32_t i = 0; i < 100; i++) { |
|||
snake.moveDown(); |
|||
Graphics::get().refreshScreen(); |
|||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|||
snake.moveRight(); |
|||
Graphics::get().refreshScreen(); |
|||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|||
} |
|||
|
|||
Graphics::get().finalize(); |
|||
|
|||
#include "game/Game.h"
|
|||
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() { |
|||
SnakeGame game; |
|||
game.play(); |
|||
mainL06(); |
|||
return 0; |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue