Skip to content

Commit

Permalink
Merge pull request coders-school#106 from lukasz-kukulka/exitConfirm
Browse files Browse the repository at this point in the history
Exit function and confirm
  • Loading branch information
lukasz-kukulka authored Jul 8, 2021
2 parents 1e81de0 + edc3dd1 commit ff3e5cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions shm/inc/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class Game {
Exit
};

enum class ConfirmOption {
Yes,
No,
Error
};

void startGame();

private:
Expand All @@ -39,6 +45,7 @@ class Game {
void checkCargo(); //NOT IMPLEMENTED
void buy(); //NOT IMPLEMENTED
void sell(); //NOT IMPLEMENTED
bool exitGame();

std::unique_ptr<Player> player_;
std::unique_ptr<Time> time_;
Expand All @@ -48,6 +55,7 @@ class Game {
bool isGameWon() const;
bool isGameLost() const;
bool validatingMenuChoose(size_t option);
ConfirmOption confirmOption(std::string announcement);

MenuOption menuOption_ { MenuOption::NoChoose };
};
29 changes: 29 additions & 0 deletions shm/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ Game::MenuOption Game::selectOption() {
sell();
break;
case MenuOption::Exit :
if (exitGame() == false) {
menuOption_ = MenuOption::NoChoose;
}
break;
default:
std::cout << "Option doesn't exists\n";
Expand All @@ -123,6 +126,32 @@ bool Game::validatingMenuChoose(size_t option) {
return true;
}

Game::ConfirmOption Game::confirmOption(std::string announcemen) {
std::cout << announcemen << '\n';
char answer;
std::cin >> answer;
if (answer == 'Y' || answer == 'y') {
return ConfirmOption::Yes;
}
if (answer == 'N' || answer == 'n') {
return ConfirmOption::No;
}
std::cout << "Wrong answer, you must choose Y or N\n";
return ConfirmOption::Error;
}

bool Game::exitGame() {
while (true) {
ConfirmOption exitAnswer = confirmOption("Are you sure you wanna exit game? Y/N");
if (exitAnswer == ConfirmOption::Yes) {
return true;
}
if (exitAnswer == ConfirmOption::No) {
return false;
}
}
}

void Game::travel() {
/* 1. PRINT MAP AND SHOW POSITION
2. PROMPT TO CHOOSE AN ISLAND IN LOOP
Expand Down

0 comments on commit ff3e5cc

Please sign in to comment.