A bunch of questions. Let's take it one at a time?

I'm trying to create a game (well, it's a project for school, really) where the player selects on character. The chosen character gets moved to the top of the list, and a random enemy selection happens from the first index to the 11th.

The first area I need help in: How do I prevent the program from choosing the same character as the player choice? The biggest problem here is that at times, when choosing Aries (please see code) the enemy is the same, but with a different HP value (which is pretty strange.) Any tips on how I can fix this?

I have loads more questions, but I'd like to ask them one at a time in this thread. I apologize for the messy code; I'm a freshman in college taking up game development, and we're pretty new to all of this. Here's the code in question (done on Microsoft VS 2010):

(I'd also appreciate if you can help me point out discrepancies in the code. We're trying to make it work as smoothly as possible despite being required to submit just a basic working copy.)

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <string>

using namespace std;

int char_select,
	char_confirm,
	enemy_select,
	tmp,
	HP[12] = {33, 35, 40, 45, 33, 50, 30, 50, 43, 30, 25, 20},
	ATK[12] = {8, 9, 8, 4, 10, 5, 7, 8, 6, 8, 7, 3},
	DEF[12] = {18, 21, 25, 28, 18, 30, 15, 30, 26, 15, 10, 5};
	
string signs[12] = {"Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"},
	temp;

void main();

void enemyselect();
class BattleSystem{
public:
	int enemyhand;
	void PlayerAttack(){
		int finalATK = ATK[0] - (DEF[enemy_select] * 0.1) + 1;
		HP[enemy_select] -= finalATK;
		cout << finalATK << " damage!\n\n";

		if(HP[enemy_select] > 0)
			Start(HP[0], HP[enemy_select]);
		else{
			cout << "You have defeated " << signs[enemy_select] << "!\n";
			cout << "Press any key to continue to the next round!";
			getch();
			system("CLS");
			enemyselect();
		}

	}
	void EnemyAttack(){
		int finalATK = ATK[enemy_select] - (DEF[0] * 0.1) + 1;
		HP[0] -= finalATK;
		cout << finalATK << " damage.\n\n";

		if(HP[0] > 0)
			Start(HP[0], HP[enemy_select]);
		else{
			cout << "You have been defeated by " << signs[enemy_select] << "..\n";
			cout << "Press any key to return to the main menu.";
			getch();
			system("CLS");
			main();
	}
	}
	void Rock(){
		if (enemyhand == 0){
			cout << "Draw.\n\n";
			Start(HP[0], HP[enemy_select]);
		}
		else if(enemyhand == 1){
			cout << signs[enemy_select] << " chooses Paper. You lost. The enemy attacks for ";
			EnemyAttack();
		}
		else if(enemyhand == 2){
			cout << signs[enemy_select] << " chooses Scissors. You win! You get to attack for ";
			PlayerAttack();

		}
	}
	void Paper(){
		if (enemyhand == 0){
			cout << signs[enemy_select] << " chooses Rock. You win! You get to attack for ";
			PlayerAttack();
		}
		else if(enemyhand == 1){
			cout << "Draw.\n\n";
			Start(HP[0], HP[enemy_select]);
		}
		else if(enemyhand == 2){			
			cout << signs[enemy_select] << " chooses Scissors. You lost. The enemy attacks for ";
			EnemyAttack();
		}
	}
	void Scissors(){
		if (enemyhand == 0){
			cout << signs[enemy_select] << " chooses Rock. You lost. The enemy attacks for ";
			EnemyAttack();
		}
		else if(enemyhand == 1){
			cout << signs[enemy_select] << " chooses Paper. You win! You get to attack for ";
			PlayerAttack();
		}
		else if(enemyhand == 2){
			cout << "Draw.\n\n";
			Start(HP[0], HP[enemy_select]);
		}
	}
	void Start(int PlayerHP, int EnemyHP){
		int hand;
		cout << signs[0] << " vs " << signs[enemy_select] << "\n\n";
		cout << "HP: " << HP[0] << setw(8) << "HP: " << HP[enemy_select];
		cout << "\n\nChoose a hand:\n\n1 = Rock\n2 = Paper\n3 = Scissors\n\n>>";
		cin >> hand;

		srand(time(NULL));
		enemyhand = rand()% 3;

		if(hand - 1 == 0)
			Rock();
		else if(hand - 1 == 1)
			Paper();
		else if(hand - 1 == 2)
			Scissors();
		else{
			system("CLS");
			cout << "You have entered an invalid value. Please try again.\n\n";
			Start(PlayerHP, EnemyHP);
		}
	}
	
};

void statviewer(int selectedchar)
{
		cout << "Name: " << signs[selectedchar] << endl;
		cout << "HP: " << HP[selectedchar] << endl;
		cout << "Attack: " << ATK[selectedchar] << endl;
		cout << "Defense: " << DEF[selectedchar];
}

void howtoplay()
{
	system("CLS");
	cout << "---------- HOW TO PLAY ----------\n\n";
	cout << "In Zodiac Playground, you play as any of the 12 Western Zodiac Signs.\n";
	cout << "Each character has his/her own HP, Attack, and Defense value.\n\n";
	cout << "Your goal is to defeat all the other star signs at Rock, Paper, Scissors.\n";	
	cout << "If you win, you get to attack. If you lose, the enemy attacks you.\n\n";
	cout << "Each attack is calculated based on the attack damage\nminus a percentage of the other's defense.\n\n";
	cout << "Fight until one of you has no HP left. Good luck!\n\n";
	cout << "Press any key to return to the main menu.";
	getch();
	system("CLS");
	main();
}

void credits()
{
	system("CLS");
	cout << "---------- CREDITS ----------\n\n";
	cout << "This game was brought to you by:\n\nKristine Crispino\nStephen Cruz\nPatricia Lopez and\nPauline Wen!\n\n";
	cout << "created 3rd Term, SY 2012-2013\nas a BASPRO2 Final Project for Sir Arjay Orcasitas.\n\n";
	cout << "Press any key to return to the main menu.";
	getch();
	system("CLS");
	main();
}

void enemyselect()
{	
	
	srand(time(NULL));
	enemy_select = rand()% 12 + 1;
	if(signs[char_select - 1] != signs[0]){
	temp = signs[char_select - 1];
	signs[char_select - 1] = signs[0];
	signs[0] = temp;
	tmp = HP[char_select - 1];
	HP[char_select - 1] = HP[0];
	HP[0] = tmp;
	tmp = ATK[char_select - 1];
	ATK[char_select - 1] = ATK[0];
	ATK[0] = tmp;
	tmp = DEF[char_select - 1];
	DEF[char_select - 1] = HP[0];
	DEF[0] = tmp;
	}

	if (enemy_select != char_select - 1){
		BattleSystem Battle;
		Battle.Start(HP[0], HP[enemy_select]);
	}
	else if(enemy_select == char_select)
		enemyselect();
	else
		enemyselect();
	
}

void charselect()
{
	cout << "Welcome to Zodiac Playground. Play as any of the 12 Zodiac Symbols,\nand beat the others at Rock, Paper, Scissors!\n\nPlease select a character\nor enter 13 to return to the main menu:\n\n";

	for(int i = 0; i < 12; i++)
	{
		cout << i + 1 << " = " << signs[i]<< endl;
	}					//PRINT CHARACTER CHOICES
	
	cout << "\n";
	cout <<">> ";
	cin >> char_select;	//USER INPUT: CHARACTER SELECTION

	if(char_select > 13 || char_select < 1){
		system("CLS");
		cout << "You have entered an invalid value. Please try again.\n\n";
		charselect();
	}
	else if(char_select == 13){
		system("CLS");
		main();
	}

	system("CLS");
	cout << "You chose " << signs[char_select - 1] << ". Your stats are:\n\n";
	statviewer(char_select - 1);

	cout << "\n\n\tAre you sure you want to play as " << signs[char_select - 1] << "?\n\n";
	cout << "\t1 = Yes\n";
	cout << "\t2 = No (Return to character select screen)\n";
	cout << ">> ";
	cin >> char_confirm;

	system("CLS");

	if(char_confirm == 1)

		enemyselect();

	else if(char_confirm == 2)
	{
		system("CLS");

		charselect();
	}
}

void main()
{
	cout << "---------- ZODIAC PLAYGROUND ----------\n\n";

	cout << "1: Start Game\n2: Instructions\n3: Credits\n\n>> ";					//MAIN MENU

	int userinput;
	cin >> userinput;

	if(userinput == 1){
		system("CLS");
		charselect();
	}
	else if(userinput == 2)
		howtoplay();
	else if(userinput == 3)
		credits();
	else{
		cout << "\nYou have entered an invalid value. Please try again.\n\n";
		main();
	}

	getch();
}
Last edited on
First of all, in C++, always use 'int main()', never 'void main()'. Some compilers can't handle void, and your system actually uses the return value of your programs. It's common to return the value 0 at the end of your main program.
Also, never call main() in your program, use a while loop instead.

Second of all, I'm not going through this mountain of code, but if your characters are the same, but the stats are different, you should probably look at the code that governs these functions.

I took a look at your charselect() function, which would work, but what you're basically doing is recursively calling other functions. When you call the function enemyselect() or charselect() again at the end of the function, you never exit the charselect() function. Luckily this isn't a memory heavy program, but this is considered bad coding.

Imagine if you'd decide to select a new character ten times. Because you don't close the function, yet call a new one every time, you never get rid of the memory space that is used by the previous functions.

Also, variable declarations outside of your program are also a no-no.
Last edited on
Alright. I switched it to 'int main()'. How do I use a while loop instead of recalling main? Also, the player character and the enemies are taken from a single array.

How do I ask the user to enter the value again if they input an invalid value without recalling charselect()?

I apologize for all the mistakes. I've been finding it very hard to keep up in this class.
Topic archived. No new replies allowed.