Finishing up on a small project i've created to improve my c++ skills. Im nearing the end and encountering some problems, after the game finishes i want to user to enter Y or N to play again. Upon entering Y the objects that have been used in the program are deleted and a goto statement returns the user to the first line of main that re-declares all variables again for another go. How would i go about doing so? would this work? If you have another way of doing so i would greatly appreciate the help! Thanks.
Here's my main.cpp(the project in whole is over 400 lines, i dont want to bother.)
//VERSION 1.06B
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "Map.h"
#include "Player.h"
/// Declaring main varibles to be used inside of main loop
bool Game = true;
bool pOneTurn = true;
bool pTwoTurn = false;
bool Winner = true;
/// Declaring function to be used inside main
externvoid PrintOpening(Map &Screen);
externvoid DisplayWinner(bool Winner); //takes up space in main so another file is used to hold the large text functions
externvoid Delay();
bool Restart();
/// End function/variable declaration
int main()
{
Map mObj(2);
PrintOpening(mObj);
mObj.createmap();
mObj.SSP();
RESTART:
mObj.RefreshScreen();
while(mObj.GetBool()){ /// MAIN LOOP(WHILE GAME RUNNING)
while(pOneTurn && mObj.GetBool()){ /// PLAYER ONE'S TURN
int moves = 0;
while(moves < 3){
std::cout << "PLAYER ONE w(UP) s(DOWN) a(LEFT) d(RIGHT) g(SHOOT)"; //display controls and stats
std::cout << "\n --- MOVES LEFT --- " << 3 - moves << std::endl;
mObj.InputChoice(1); //run input function to see where to move
mObj.RefreshScreen(); //rewrite the array
Game = mObj.Game();
moves++;
}
mObj.RefreshScreen();
Winner = true; //means player one wins
pTwoTurn = true; //set player twos turn to true
pOneTurn = false; //set player ones turn to false
}
while(pTwoTurn && mObj.GetBool()){ /// PLAYER TWO'S TURN
int moves=0;
while(moves < 3){
std::cout << "PLAYER TWO w(UP) s(DOWN) a(LEFT) d(RIGHT) g(SHOOT)";
std::cout << "\n --- MOVES LEFT --- " << 3-moves << std::endl;
mObj.InputChoice(2);
mObj.RefreshScreen();
Game = mObj.Game();
moves++;
}
mObj.RefreshScreen();
Winner = false; //player two wins
pOneTurn = true;
pTwoTurn = false;
}
}
mObj.cls();
DisplayWinner(Winner); //display winner of game
//-------------------------------------------------------------------------------------------------------------
//how do i reset objects?!??! is goto also the only option?
if(Restart()){mObj.~Map(); goto RESTART;} //i know, goto's are very bad, but i could think of a better statement!
std::cout << "\n\t\tThanks for playing!";
}
bool Restart() //tests whether play wants to play again
{
char PlayAgain; //Y for yes N for no
std::cout << "\t\tPlay again? (Y)es (N)o";
std::cin >> PlayAgain;
PlayAgain = toupper(PlayAgain);
if(PlayAgain == 'Y'){returntrue;}
returnfalse;
}