Restarting a game with new objects

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.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//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
extern void PrintOpening(Map &Screen);
extern void DisplayWinner(bool Winner); //takes up space in main so another file is used to hold the large text functions
extern void 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'){return true;}
    return false;
}


Last edited on
1
2
3
4
do
{
 // ...
} while(Restart()) ;


Do not call mObj's destructor manually.
Last edited on
should this do while loop be placed outside of all the current loops i have?

EDIT: or should i put a && in the main game loop, change it to do while and run it like that? i appreciate the help!
Last edited on
Put everything that isn't saying thanks for playing inside the loop.
Put everything in main() other than the last two lines into a separate function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void play_game()
{
    Map mObj(2);
    PrintOpening(mObj);
    mObj.createmap();
    mObj.SSP();
    // RESTART: 
    mObj.RefreshScreen();
    while(mObj.GetBool()){ /// MAIN LOOP(WHILE GAME RUNNING)

        // ...

    }
    mObj.cls();
    DisplayWinner(Winner); //display winner of game
}


And then:
1
2
3
4
5
int main()
{
    do play_game() ; while( Restart() ) ;
    std::cout << "\n\t\tThanks for playing!\n" ;
}


Last edited on
Thanks JLBorges! that helped organize my game a lot
Topic archived. No new replies allowed.