Problem with console game loop not ending
Oct 14, 2016 at 6:40pm UTC
Hi, in my main function, my while loop doesn't end and return to 0 for some reason. I use a function in a class that returns a pointer for HP and I use it for the entire program and it works fine until I plug it into the while loop in my main function.
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int killcount = 0;
//quick little clearing screen function
void clearScreen(){
system("pause" );
system("cls" );
}
class playerAndEnemyClass{
public :
int playerHealth = 100;
int *playerHealthPointer = &playerHealth;
int playerAttack = 25;
int *playerAttackPointer = &playerAttack;
int playerDefence = 10;
int *playerDefencePointer = &playerDefence;
int playerGP = 0;
int *playerGPPointer = &playerGP;
int playerMAXHealth = 100;
int *playerMAXHealthPointer = &playerMAXHealth;
int fighting(){
srand(time(0));
int zombieHealth = 5 + rand()%21; //5-25
int zombieAttack = 10 + rand()%16; //10-25
int zombieDefence = 5 + rand()%11; //5-15
int zombieGP = 50 + rand()%151; //50-200
setStatsForBattle(zombieAttack, zombieDefence);
int rr = 1+rand()%2;
bool ItsMyTurn;
if (rr==1){
cout << "You go first!\n\n" ;
ItsMyTurn = true ;
}else if (rr==2){
cout << "Zombie goes first!\n\n" ;
ItsMyTurn = false ;
}
while (actuallyGetHP()>0 && zombieHealth>0){
if (ItsMyTurn==false ){
int rr1 = 1+rand()%6;
switch (rr1){
case 1:
cout << "Critical Hit! Enemy did " << zombieAttack + zombieAttack << " damage!" << endl;
*playerHealthPointer -= zombieAttack + zombieAttack;
ItsMyTurn = true ;
break ;
case 2:
case 3:
cout << "Normal hit. Enemy did " << zombieAttack << " damage!" << endl;
*playerHealthPointer -= zombieAttack;
ItsMyTurn = true ;
break ;
default :
cout << "Miss!" << endl;
ItsMyTurn = true ;
}//ends switch
cout << endl;
}//ends zombie turn
else if (ItsMyTurn==true ){
int rr1 = 1+rand()%6;
switch (rr1){
case 1:
cout << "Critical Hit! You did " << playerAttack + playerAttack << " damage!" << endl;
zombieHealth -= playerAttack + playerAttack;
ItsMyTurn = false ;
break ;
case 2:
case 3:
cout << "Normal hit. You did " << playerAttack << " damage!" << endl;
zombieHealth -= playerAttack;
ItsMyTurn = false ;
break ;
default :
cout << "Miss!" << endl;
ItsMyTurn = false ;
}//ends switch
cout << endl;
}//ends my turn
cout << endl;
}//ends while loop
if (zombieHealth<=0){
killcount++;
*playerGPPointer += zombieGP;
cout << "You killed the zombie. You earned " << zombieGP << "GP! You have " << *playerHealthPointer << " life left." << endl;
cout << "Current killcount: " << killcount << endl;
cout << "Current GP: " << *playerGPPointer << "GP" << endl << endl;
}else if (actuallyGetHP()<=0){
cout << "You died nigguh" << endl;
return 0;
}
}//ends fighting
void setStatsForBattle(int thezombieattack, int thezombiedefence){
if (thezombieattack<=playerDefence){
thezombieattack = 0;
}else {
thezombieattack -= playerDefence;
}
if (playerAttack<=thezombiedefence){
playerAttack = 0;
}else {
playerAttack -= thezombiedefence;
}
}
int getHP(int *x){
return *x;
}
int actuallyGetHP(){
getHP(playerHealthPointer);
}
void healPlayerHealth(){
*playerHealthPointer = *playerMAXHealthPointer;
}
void changePlayerMaxHealth(int x){
*playerMAXHealthPointer += x;
}
};
class mapAndMovementClass{
public :
mapAndMovementClass(){ // constructor
srand(time(0));
for (int row=0; row<50; row++){
for (int col=0; col<25; col++){
if (row==0 || row==49 || col==0 || col==24){
theMap[row][col] = '#' ;
}
else {
theMap[row][col] = zombieGeneration();
}
}//ends column
}//ends row
}//ends constructor
playerAndEnemyClass po1;
char theMap[50][25];
char thePlayer[50][25];
int pX = 48;
int *pXpointer = &pX;
int pY = 1;
int *pYpointer = &pY;
void printMap(){
for (int row=0; row<50; row++){
for (int col=0; col<25; col++){
cout << theMap[row][col] << " " ;
}//ends col
cout << endl;
}//ends row
cout << endl;
}
char zombieGeneration(){
int zr = 1+rand()%10;
if (zr>5){
return 'z' ;
}
return ' ' ;
}
void playerMovementforthisclassonly(int *x, int *y, char dosmap[50][25], char dosplayer[50][25]){
dosplayer[*x][*y] = '%' ;
dosmap[*x][*y] = dosplayer[*x][*y];
printMap();
cout << "Use WASD to move!" << endl << endl;
cout << "Current Health: " << po1.actuallyGetHP() << endl << endl;
char input;
cin >> input;
if (input == 'w' || input == 'W' ){
if (dosmap[*x-1][*y]=='z' ){
po1.fighting();
}
if (dosmap[*x-1][*y]!='#' ){
dosplayer[*x][*y] = ' ' ;
dosmap[*x][*y] = dosplayer[*x][*y];
*x -= 1;
}
}//ends W
if (input == 'a' || input == 'A' ){
if (dosmap[*x][*y-1]=='z' ){
po1.fighting();
}
if (dosmap[*x][*y-1]!='#' ){
dosplayer[*x][*y] = ' ' ;
dosmap[*x][*y] = dosplayer[*x][*y];
*y -= 1;
}
}//ends A
if (input == 's' || input == 'S' ){
if (dosmap[*x+1][*y]=='z' ){
po1.fighting();
}
if (dosmap[*x+1][*y]!='#' ){
dosplayer[*x][*y] = ' ' ;
dosmap[*x][*y] = dosplayer[*x][*y];
*x += 1;
}
}//ends S
if (input == 'd' || input == 'D' ){
if (dosmap[*x][*y+1]=='z' ){
po1.fighting();
}
if (dosmap[*x][*y+1]!='#' ){
dosplayer[*x][*y] = ' ' ;
dosmap[*x][*y] = dosplayer[*x][*y];
*y += 1;
}
}//ends D
}//ends movement
void actuallyMoveThePlayer(){
playerMovementforthisclassonly(pXpointer, pYpointer, theMap, thePlayer);
}
};
int main(){
playerAndEnemyClass PlayerObject1;
mapAndMovementClass MapObject1;
while (PlayerObject1.actuallyGetHP()>0){
MapObject1.actuallyMoveThePlayer();
clearScreen();
}
return 0;
}
Oct 15, 2016 at 12:04pm UTC
@WheatFieldOnFire yes, 1 chance for a critical hit, 2 chances for a normal hit, and 3 chances for a miss is what I was going for
Oct 15, 2016 at 6:21pm UTC
I figured it out
Last edited on Oct 15, 2016 at 8:13pm UTC
Oct 15, 2016 at 8:15pm UTC
@WheatFieldOnFire I used to do that too but then I saw someone's code who did it how I did it in this program and I was like wow that actually makes sense haha I try not to rewrite stuff as often as possible
Topic archived. No new replies allowed.