Skip to content

Commit 3f4dde3

Browse files
Add game loop and validation insert option menu
1 parent 1c33e3e commit 3f4dde3

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
@@ -20,18 +20,8 @@ Game::Game(size_t money, size_t gameDays, size_t finalGoal)
2020

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

3727
bool Game::isGameWon() const {
@@ -86,11 +76,14 @@ void Game::printMap() {
8676
}
8777
}
8878

89-
void Game::selectOption() {
90-
printWelcomeScreen();
79+
Game::MenuOption Game::selectOption() {
80+
printIntenface();
9181
printMenu();
9282
size_t option {};
93-
std::cin >> option;
83+
do {
84+
std::cout << "Please insert you choice: ";
85+
std::cin >> option;
86+
} while (validatingMenuChoose(option) == false);
9487
menuOption_ = static_cast<MenuOption>(option);
9588
switch(menuOption_) {
9689
case MenuOption::printMap :
@@ -109,10 +102,23 @@ void Game::selectOption() {
109102
sell();
110103
break;
111104
case MenuOption::Exit :
112-
exit(0);
105+
break;
113106
default:
114107
std::cout << "Option doesn't exists\n";
115108
}
109+
return menuOption_;
110+
}
111+
112+
bool Game::validatingMenuChoose(size_t option) {
113+
size_t firstOptionElement { 1 };
114+
size_t lastOptionElement { 6 };
115+
if (std::cin.fail() || option < firstOptionElement || option > lastOptionElement) {
116+
std::cin.clear();
117+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
118+
std::cout << "Wrong value please insert number between " << firstOptionElement << " and " << lastOptionElement << ".\n";
119+
return false;
120+
}
121+
return true;
116122
}
117123

118124
void Game::travel() {

0 commit comments

Comments
 (0)