I haven't done a lot of C++ Command Line I/O in quite a while so I have forgotten a couple of things.
Anyway I started to work on a game just for the command line because I wanted to work on my code design skills more. I felt having to worry about a graphics API would only get in the way. The code design would be close to the same and I'd just have to trade in graphics when I felt like it mostly.
Well the game ended up getting bigger than I thought it would for such a simple game and now I find myself posing this question. I have a game that has a lot of "menus" (with the command line that's almost all their can be) and I find myself repeating a lot of code in places just to wait until I get valid input from the user. Meaning making sure the user inputs an integer (that's no problem) but also making sure the user does a valid selection for that menu. Quick example of some code from a menu in my game.
From the menu from where the user is selecting their game mode:
//GetMode asks the user what mode they want to play on and returns mode selected
GameMode Game::GetGameMode()
{
bool validInput=false;
GameMode gameMode;
while(!validInput)
{
int choice=ToInt(StringInput());
switch(choice)
{
case GameMode::COMPUTER_PLAYER:
gameMode=GameMode::COMPUTER_PLAYER;
validInput=true;
break;
case GameMode::PLAYER_COMPUTER:
gameMode=GameMode::PLAYER_COMPUTER;
validInput=true;
break;
default:
std::cout<<"Please enter a valid choice using the number next to your desired game mode."<<std::endl;
std::cout<<"Press ENTER to try again..."<<std::endl;
std::cin.get();
DisplayGameMode();
}
}
return gameMode;
}
Difficulty Game::GetDifficulty()
{
bool validInput=false;
Difficulty gameDifficulty;
while(!validInput)
{
int choice=ToInt(StringInput());
switch(choice)
{
case Difficulty::EASY:
{
gameDifficulty=Difficulty::EASY;
validInput=true;
break;
}
case Difficulty::MEDIUM:
{
gameDifficulty=Difficulty::MEDIUM;
validInput=true;
break;
}
case Difficulty::HARD:
{
gameDifficulty=Difficulty::HARD;
validInput=true;
break;
}
case Difficulty::EXPERT:
{
gameDifficulty=Difficulty::EXPERT;
validInput=true;
break;
}
case Difficulty::GOD:
{
gameDifficulty=Difficulty::GOD;
validInput=true;
break;
}
default:
{
std::cout<<"Please enter a valid choice using the number next to your desired difficulty."<<std::endl;
std::cout<<"Press ENTER to try again..."<<std::endl;
std::cin.get();
DisplayDifficulty();
}
}
}
return gameDifficulty;
}