Game loop assistance

Hi, I'm currently making a text based rpg game. I need some help on how to make game loop after I defeat my enemy or the enemy defeats me. I need it to say "You win!" or "You lose!" and then loop back to the start, allowing me to play again. Here's my code:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
 


#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>

using namespace std;

int main()
{

	int eHealth = 100;  //initial Enemy Health(based on enemy)
	int pHealth = 100;  //initial Player Health 
	int eAttack = 0;    //Enemy Attack Strength(based on enemy)
	int pAttack = 0;    //Player Attack Strength(based on level and items)
	int selection = 0;  //Selection Variable for Battle Menu
	int hPower = 0;     //Healing power Variable(random number)
	int eSelection = 0; //Enemies battle menu selection variable
	int counter = 0;    //Counter to establish whos turn it is 
	int itemSelect = 0; //Item inventory selection
	int eMagic = 50;    //Enemies magic meter
	int pMagic = 50;    //Players magic meter

	cout << "" << endl;
	cout << "Player:            Enemy: " << endl;
	cout << "" << endl;
	cout << "Health: " << pHealth << "        " << "Health: " << eHealth << endl;  //player and enemy health levels
	cout << "Magic: " << pMagic << "          " << "Magic: " << eMagic << endl;    //player and enemy magic levels
	cout << endl;
	cout << endl;
	cout << "What would you like to do?" << endl;
	cout << "1.Attack" << endl;                     //lines 31-33 are the battle menu appears frequently throught game
	cout << "2.Heal" << endl;
	cout << "3.Item" << endl;
	do                  //start of post test loop that runs the game until somebody is dead
	{

		if (counter == 0)// if the counter variable is 0 it is the players turn
		{
			cin >> selection;
			srand(static_cast<int>(time(0)));//randomize all the random variables

			switch (selection)
			{
			case 1:// player chooses to ATTACK
				pAttack = 1 + rand() % (35 - 1 + 1);//attack power can be between 1-35
				cout << "You chose to Attack and caused " << pAttack << " damage." << endl;
				system("pause");
				system("cls");
				eHealth = eHealth - pAttack;
				cout << "" << endl;
				cout << "Player:            Enemy: " << endl;
				cout << "" << endl;
				cout << "Health: " << pHealth << "        " << "Health: " << eHealth << endl;  //player and enemy health levels
				cout << "Magic: " << pMagic << "          " << "Magic: " << eMagic << endl;    //player and enemy magic levels
				cout << endl;
				cout << endl;
				cout << "1.Attack" << endl;
				cout << "2.Heal" << endl;
				cout << "3.Item" << endl;
				break;
			case 2://Player chooses to heal, it costs 10 magic to heal so if you dont have enough magic you loose a turn
				if (pMagic>9)
				{
					hPower = 1 + rand() % (35 - 1 + 1);//healing power can be any number between 1-35
					cout << "You chose to Heal and restored " << hPower << " health." << endl;
					system("pause");
					system("cls");
					pHealth = pHealth + hPower;
					pMagic = pMagic - 10;
					cout << "" << endl;
					cout << "Player:            Enemy: " << endl;
					cout << "" << endl;
					cout << "Health: " << pHealth << "        " << "Health: " << eHealth << endl;  //player and enemy health levels
					cout << "Magic: " << pMagic << "          " << "Magic: " << eMagic << endl;    //player and enemy magic levels
					cout << endl;
					cout << endl;
					cout << "1.Attack" << endl;
					cout << "2.Heal" << endl;
					cout << "3.Item" << endl;
				}//endIF
				break;

			case 3:// if player chooses to use item inventory system
				cout << "ITEM INVENTORY" << endl;
				cout << "1.Potion            Restores HP" << endl;
				cout << "2.Elixar             Restores MP" << endl;
				cout << "3.Bomb              Causes Damage to all players" << endl;
				cout << "4.Freeze Ray       Freezes opponent for 3 Turns" << endl;

				cin >> itemSelect;
				switch (itemSelect)
				{
				case 1:
					pHealth = pHealth + 75;
					system("cls");
					cout << "" << endl;
					cout << "Player:            Enemy: " << endl;
					cout << "" << endl;
					cout << "Health: " << pHealth << "        " << "Health: " << eHealth << endl;  //player and enemy health levels
					cout << "Magic: " << pMagic << "          " << "Magic: " << eMagic << endl;    //player and enemy magic levels
					cout << endl;
					cout << endl;
					cout << "1.Attack" << endl;
					cout << "2.Heal" << endl;
					cout << "3.Item" << endl;
					break;
				case 2:


					break;
				}//endItemSwitch        
			}//endSelectionSwitch
			counter = 1;// advances the counter to 1 to allow the enemies turn
		}//endif
		eSelection = rand() % 2 + 1;
		switch (eSelection)
		{
		case 1:
			eAttack = 1 + rand() % (35 - 1 + 1);
			cout << "Enemy chose to Attack and caused " << hPower << " damage." << endl;
			system("pause");
			system("cls");
			pHealth = pHealth - eAttack;
			cout << "" << endl;
			cout << "Player:            Enemy: " << endl;
			cout << "" << endl;
			cout << "Health: " << pHealth << "        " << "Health: " << eHealth << endl;  //player and enemy health levels
			cout << "Magic: " << pMagic << "          " << "Magic: " << eMagic << endl;    //player and enemy magic levels
			cout << endl;
			cout << endl;
			cout << "1.Attack" << endl;
			cout << "2.Heal" << endl;
			cout << "3.Item" << endl;
			break;

		case 2:

			if (eMagic>10)
			{
				hPower = 1 + rand() % (35 - 1 + 1);
				cout << "Enemy chose to Heal and restored " << hPower << " health." << endl;
				system("pause");
				system("cls");
				eMagic = eMagic - 10;
				eHealth = eHealth + hPower;
				cout << "" << endl;
				cout << "Player:            Enemy: " << endl;
				cout << "" << endl;
				cout << "Health: " << pHealth << "        " << "Health: " << eHealth << endl;  //player and enemy health levels
				cout << "Magic: " << pMagic << "          " << "Magic: " << eMagic << endl;    //player and enemy magic levels
				cout << endl;
				cout << endl;
				cout << "1.Attack" << endl;
				cout << "2.Heal" << endl;
				cout << "3.Item" << endl;
			}
			break;
		}//endeSelectionSwitch
		counter = 0;
	} while (eHealth > 1 && pHealth > 1);//loops while both players life is over 1 ends postest loop when players life or enemies falls below 1
	

	
	return 0;
}

Last edited on
OK... Well as a beginner but with a few ideas in mind, I'm going to help you.
First things first. I think you should rather use 'void' to simplify the game's code. I mean with 'void' you can jump from one thing to another and keep jumping and create a virtual loop.
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
#include <iostream>

using namespace std;

int input;

void play()
{
    int main(); //Defining it.

    cout<<"Play the game!\n"; //Just for example.
    cin>>input; //Just for example so you can write a number and continue.
    main(); //'Jumping to Main'.
}

int main()
{
  cout<<"1. Play\n";
  cin>>input;
  switch (input){
  case 1:
      play(); //Jump to play
  }
}

//If successfull, loop created! On a side note, it makes your game much more nicer
//than just 'end' uses. 


But from the looks of it, you are a bit higher level than me, but hey, I can help right?

So, I suggest you start using void functions, and I learnt something from you too (the use of ends).
The main function shouldn't be called - it's a special function as it's the entry point into the program. It might be good to break some of the game action into separate functions though.

You could loop on them wanting to play the game - after giving the result of the game, win or lose, ask if they want to play again. If yes, then loop again, else break out of the loop.
Topic archived. No new replies allowed.