Skip to content

Commit 1e81de0

Browse files
Merge pull request coders-school#105 from lukasz-kukulka/gameLoop
Game loop and vaid data
2 parents 20e20d1 + 3f4dde3 commit 1e81de0

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

shm/inc/Game.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Game {
3434
const size_t finalGoal_;
3535
size_t currentDay_{};
3636

37-
void selectOption();
37+
MenuOption selectOption();
3838
void travel(); //NOT IMPLEMENTED
3939
void checkCargo(); //NOT IMPLEMENTED
4040
void buy(); //NOT IMPLEMENTED
@@ -47,6 +47,7 @@ class Game {
4747

4848
bool isGameWon() const;
4949
bool isGameLost() const;
50+
bool validatingMenuChoose(size_t option);
5051

5152
MenuOption menuOption_ { MenuOption::NoChoose };
5253
};

shm/src/Game.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,8 @@ Game::Game(size_t money, size_t gameDays, size_t finalGoal)
2222

2323
void Game::startGame() {
2424
printWelcomeScreen();
25-
printIntenface();
26-
printMap();
27-
// while (game_days_ > current_day_) {
28-
// printMenu();
29-
// if (isGameWon()) {
30-
// // YOU WON!
31-
// return;
32-
// }
33-
// std::cin.get();
34-
// // CONTUNUE GAME
35-
// }
36-
// YOU LOST!
25+
while (selectOption() != MenuOption::Exit && isGameWon() == false && isGameLost() == false) {
26+
}
3727
}
3828

3929
bool Game::isGameWon() const {
@@ -88,11 +78,14 @@ void Game::printMap() {
8878
}
8979
}
9080

91-
void Game::selectOption() {
92-
printWelcomeScreen();
81+
Game::MenuOption Game::selectOption() {
82+
printIntenface();
9383
printMenu();
9484
size_t option {};
95-
std::cin >> option;
85+
do {
86+
std::cout << "Please insert you choice: ";
87+
std::cin >> option;
88+
} while (validatingMenuChoose(option) == false);
9689
menuOption_ = static_cast<MenuOption>(option);
9790
switch(menuOption_) {
9891
case MenuOption::printMap :
@@ -111,10 +104,23 @@ void Game::selectOption() {
111104
sell();
112105
break;
113106
case MenuOption::Exit :
114-
exit(0);
107+
break;
115108
default:
116109
std::cout << "Option doesn't exists\n";
117110
}
111+
return menuOption_;
112+
}
113+
114+
bool Game::validatingMenuChoose(size_t option) {
115+
size_t firstOptionElement { 1 };
116+
size_t lastOptionElement { 6 };
117+
if (std::cin.fail() || option < firstOptionElement || option > lastOptionElement) {
118+
std::cin.clear();
119+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
120+
std::cout << "Wrong value please insert number between " << firstOptionElement << " and " << lastOptionElement << ".\n";
121+
return false;
122+
}
123+
return true;
118124
}
119125

120126
void Game::travel() {

0 commit comments

Comments
 (0)