Skip to content

Commit ff3e5cc

Browse files
Merge pull request coders-school#106 from lukasz-kukulka/exitConfirm
Exit function and confirm
2 parents 1e81de0 + edc3dd1 commit ff3e5cc

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

shm/inc/Game.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class Game {
2222
Exit
2323
};
2424

25+
enum class ConfirmOption {
26+
Yes,
27+
No,
28+
Error
29+
};
30+
2531
void startGame();
2632

2733
private:
@@ -39,6 +45,7 @@ class Game {
3945
void checkCargo(); //NOT IMPLEMENTED
4046
void buy(); //NOT IMPLEMENTED
4147
void sell(); //NOT IMPLEMENTED
48+
bool exitGame();
4249

4350
std::unique_ptr<Player> player_;
4451
std::unique_ptr<Time> time_;
@@ -48,6 +55,7 @@ class Game {
4855
bool isGameWon() const;
4956
bool isGameLost() const;
5057
bool validatingMenuChoose(size_t option);
58+
ConfirmOption confirmOption(std::string announcement);
5159

5260
MenuOption menuOption_ { MenuOption::NoChoose };
5361
};

shm/src/Game.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ Game::MenuOption Game::selectOption() {
104104
sell();
105105
break;
106106
case MenuOption::Exit :
107+
if (exitGame() == false) {
108+
menuOption_ = MenuOption::NoChoose;
109+
}
107110
break;
108111
default:
109112
std::cout << "Option doesn't exists\n";
@@ -123,6 +126,32 @@ bool Game::validatingMenuChoose(size_t option) {
123126
return true;
124127
}
125128

129+
Game::ConfirmOption Game::confirmOption(std::string announcemen) {
130+
std::cout << announcemen << '\n';
131+
char answer;
132+
std::cin >> answer;
133+
if (answer == 'Y' || answer == 'y') {
134+
return ConfirmOption::Yes;
135+
}
136+
if (answer == 'N' || answer == 'n') {
137+
return ConfirmOption::No;
138+
}
139+
std::cout << "Wrong answer, you must choose Y or N\n";
140+
return ConfirmOption::Error;
141+
}
142+
143+
bool Game::exitGame() {
144+
while (true) {
145+
ConfirmOption exitAnswer = confirmOption("Are you sure you wanna exit game? Y/N");
146+
if (exitAnswer == ConfirmOption::Yes) {
147+
return true;
148+
}
149+
if (exitAnswer == ConfirmOption::No) {
150+
return false;
151+
}
152+
}
153+
}
154+
126155
void Game::travel() {
127156
/* 1. PRINT MAP AND SHOW POSITION
128157
2. PROMPT TO CHOOSE AN ISLAND IN LOOP

0 commit comments

Comments
 (0)