+1 678 648 4277 
Table Of Contents
  • Creating The New Game

Creating The New Game

• void setupGame(mastermind*& currentGame, mastermind*& savedGame) - this function creates a new game by performing the following tasks
1. Prompt the user for peg colors terminated with ”0” (they must have at least one peg color)
2. If no currentGame in progress, go to step 5
3. If there is a current game in progress, ask the user if they wish to save the game, if they wish to save the game call the saveGame function if the saveGame function did not actually save the game then exit the setup function, otherwise go to step 5
4. If there is a current game in progress, and the user does not want to save the game, deallocate the currentGame and go to step 5
5. Allocate a new game to currentGame pointer by passing the vector from step 1 to the constructor
Saving A Current Game
• void saveGame(mastermind*& currentGame, mastermind*& savedGame) - this function saves a current game by performing the following tasks
1. If there is no saved game, set saveGame to currentGame and assign currentGame to NULL and exit function
2. If there is a saved game, ask the user if they still want to save over the savedGame, if they want to save over, deallocate savedGame and set it to currentGame, then set currentGame to NULL
3. If the user does not want to save the game then do nothing and exit from the function
 • void loadGame(mastermind*& currentGame, mastermind*& savedGame) - this function saves a current game by performing the following tasks
1. If there is no saved game, exit out of the function
2. If there is a current game, ask the user if they wish to destroy this game, if they do, destroy the game and go to step 4
3. If they do not wish to destroy the current game, then exit the function
4. Set current game with the game in saved game and set saved game to NULL
• bool yesOrNo(string msg) - This function prompts the user by outputting msg and re-prompts if the user does not enter Y/y or N/n, once the user enters Y/y or N/n it returns true if the user entered Y/y and returns false if they entered N/n.
• void playGame(mastermind*& currentGame, mastermind*& savedGame) - this function saves a current game by performing the following tasks
1. If there is no current game, exit out of the function
2. Output the turn number and output the amount of peg colors the user must enter
3. If they enter ”P” or ”p” ask the user if they wish to save their progress, if they do then call save game and exit the function, if they do not wish to save the game then just exit the function without modifying currentGame or savedGame
4. If they entered their set of colors for the pegs, call currentGame’s move function
5. Output the silver and gold pegs
3
6. If the game is over (win/lose) output the appropriate function, the destroy the currentGame, set the pointer to NULL as well and escape the game
7. If the game is not over, go back to step 2
Solution
#include
#include
#include
#include "mastermind.h"
using namespace std;
void setupGame(mastermind*&, mastermind*&);
void loadGame(mastermind*&, mastermind*&);
void saveGame(mastermind*&, mastermind*&);
bool yesOrNo(string);
void playGame(mastermind*&, mastermind*&);
string formatString(string);
int main()
{
 mastermind * currentGame;
 mastermind * savedGame;
 char choice;
 bool done = false;
 currentGame = savedGame = NULL;
 while (!done)
 {
  cout<
  cout<< "Do wish to (P)lay a new game, (C)ontinue current game"
   << ", (L)oad saved game, or (Q)uit)? ";
  cin>> choice;
  switch (tolower(choice))
  {
   case 'p':
    setupGame(currentGame, savedGame);
    playGame(currentGame, savedGame);
    break;
   case 'l':
    loadGame(currentGame, savedGame);
    playGame(currentGame, savedGame);
    break;
   case 'c':
    playGame(currentGame, savedGame);
    break;
   case 'q':
    done = true;
    break;
   default:
    cout<< "Nope. 0/10. Do not approve of this input" <
    break;
  }
 }
 //deallocate the objects if any are still left after
 //user decides to quit
 if (currentGame != NULL)
  delete currentGame;
 if (savedGame != NULL)
  delete savedGame;
 cout<
 cout<< "Ok well, I hope this was enjoyable" <
 return 0;
}
void setupGame(mastermind*& currentGame, mastermind*& savedGame)
{
}
void saveGame(mastermind*& currentGame, mastermind*& savedGame)
{
}
void loadGame(mastermind*& currentGame, mastermind*& savedGame)
{
}
bool yesOrNo(string msg)
{
 char choice;
 cout<
 do
 {
  cout<< msg;
  cin>> choice;
  choice = tolower(choice);
 }
 while (choice != 'y' && choice != 'n');
 return choice == 'y';
}
void playGame(mastermind*& currentGame, mastermind*& savedGame)
{
}