Text game health not working
Aug 24, 2012 at 9:06pm UTC
The player and animals health is not working. Any help is appreciated! Here is 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 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
#include <iostream>
#include <ctime>
#include <cstdlib>
int gold = 50;
using namespace std;
class human
{
public :
int health, gold;
int attack();
};
class animals
{
public :
int health;
int attack ()
{
int iSecret;
cout << "I'm attacking!" ;
srand ( time(NULL) );
iSecret = rand() % 10 + 1;
const int g = iSecret;
cout << " You have applied " << g << " damage." ;
health = health - g;
if (health > 0)
{
cout << "The animals health is " << health << "It is now attacking you!" ;
animattack();
}
else
{
cout << "The animal has died" ;
}
return 0;
}
int animattack ()
{
int iSecret;
srand ( time(NULL) );
iSecret = rand() % 100 + 1;
human player;
player.health = player.health - iSecret;
cout << "Your health is now " << player.health;
return player.health;
}
};
animals animal ()
{
int iSecret;
srand ( time(NULL) );
iSecret = rand() % 10 + 1;
if (iSecret == 1)
{
int health;
cout << "Jaguar" ;
animals jaguar;
jaguar.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return jaguar;
}
else if (iSecret == 2)
{
int health;
cout << "Snake" ;
animals snake;
snake.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return snake;
}
else if (iSecret == 3)
{
int health;
cout << "Tiger" ;
animals tiger;
tiger.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return tiger;
}
else if (iSecret == 4)
{
int health;
cout << "Lion" ;
animals lion;
lion.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return lion;
}
else if (iSecret == 5)
{
int health;
cout << "Zebra" ;
animals zebra;
zebra.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return zebra;
}
else if (iSecret == 6)
{
int health;
cout << "Leopard" ;
animals leopard;
leopard.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return leopard;
}
else if (iSecret == 7)
{
int health;
cout << "Elephant" ;
animals elephant;
elephant.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return elephant;
}
else if (iSecret == 8)
{
int health;
cout << "Cougar" ;
animals cougar;
cougar.health = health;
srand ( time(NULL) );
health = rand() % 10 +1;
return cougar;
}
else if (iSecret == 9)
{
int health;
cout << "Sun Bear" ;
animals sunbear;
sunbear.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return sunbear;
}
else
{
int health;
cout << "Okapi" ;
animals okapi;
okapi.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return okapi;
}
cout << "!" ;
}
int run ()
{
cout << "I have run away!" ;
gold = gold - 5;
cout << "I lost 5 gold! I now have " << gold << " gold" ;
return 0;
}
int main()
{
int iSecret;
srand ( time(NULL) );
iSecret = rand() % 100 + 1;
human player;
player.health = 100;
char choice;
cout << "\t\t\tWelcome to gerfy's game!\n\n " ;
cout << "In this game you want are trying to get across" ;
cout << " the jungle but there are many\n different animals that will attack you." ;
cout << " To get past the animals you must kill\n them. As you kill the animals you will" ;
cout << " you will get gold. You can choose to run away from the animal or attack it." ;
cout << " If you run away from the animal you will get more health. " ;
cout << "\n\n\t\t\t Now lets start the game!" ;
cout << "\n\n You run into a " ;
animal();
cout << " Do you want to [f]ight or [r]un away?" ;
cin >> choice;
if (choice == 'f' )
{
animals jaguar;
jaguar.attack();
}
else if (choice == 'r' )
{
run ();
}
else
{
cout << "Error" ;
}
return 0;
}
Aug 24, 2012 at 9:15pm UTC
In what way does it not work?
Aug 24, 2012 at 10:17pm UTC
Health comes out to a really long number
Aug 24, 2012 at 10:33pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int health; // Here you create health without initializing it.
cout << "Jaguar" ;
animals jaguar;
jaguar.health = health; // Here you are using the uninitialized health
// variable to give a value to another variable.
// What value do you think jaguar.health will
// contain now?
srand ( time(NULL) ); // Normally you don't want to call srand more than
// once in a program. At the beginning of main is
// a good place.
health = rand() % 10 + 1; // Now you give health a value but you never
// use it.
Last edited on Aug 24, 2012 at 10:34pm UTC
Aug 24, 2012 at 10:35pm UTC
Make a constructor to initialize health as Peter87 said.
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
#include <iostream>
#include <ctime>
#include <cstdlib>
int gold = 50;
using namespace std;
class human
{
public :
human(){health=100;}
int health, gold;
int attack();
};
class animals
{
public :
int health;
animals(){health=100;}
int attack ()
{
int iSecret;
cout << "I'm attacking!" ;
srand ( time(NULL) );
iSecret = rand() % 10 + 1;
const int g = iSecret;
cout << " You have applied " << g << " damage." ;
health = health - g;
if (health > 0)
{
cout << "The animals health is " << health << "It is now attacking you!" ;
animattack();
}
else
{
cout << "The animal has died" ;
}
return 0;
}
int animattack ()
{
int iSecret;
srand ( time(NULL) );
iSecret = rand() % 100 + 1;
human player;
player.health = player.health - iSecret;
cout << "Your health is now " << player.health;
return player.health;
}
};
animals animal ()
{
int iSecret;
srand ( time(NULL) );
iSecret = rand() % 10 + 1;
if (iSecret == 1)
{
int health;
cout << "Jaguar" ;
animals jaguar;
jaguar.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return jaguar;
}
else if (iSecret == 2)
{
int health;
cout << "Snake" ;
animals snake;
snake.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return snake;
}
else if (iSecret == 3)
{
int health;
cout << "Tiger" ;
animals tiger;
tiger.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return tiger;
}
else if (iSecret == 4)
{
int health;
cout << "Lion" ;
animals lion;
lion.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return lion;
}
else if (iSecret == 5)
{
int health;
cout << "Zebra" ;
animals zebra;
zebra.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return zebra;
}
else if (iSecret == 6)
{
int health;
cout << "Leopard" ;
animals leopard;
leopard.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return leopard;
}
else if (iSecret == 7)
{
int health;
cout << "Elephant" ;
animals elephant;
elephant.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return elephant;
}
else if (iSecret == 8)
{
int health;
cout << "Cougar" ;
animals cougar;
cougar.health = health;
srand ( time(NULL) );
health = rand() % 10 +1;
return cougar;
}
else if (iSecret == 9)
{
int health;
cout << "Sun Bear" ;
animals sunbear;
sunbear.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return sunbear;
}
else
{
int health;
cout << "Okapi" ;
animals okapi;
okapi.health = health;
srand ( time(NULL) );
health = rand() % 10 + 1;
return okapi;
}
cout << "!" ;
}
int run ()
{
cout << "I have run away!" ;
gold = gold - 5;
cout << "I lost 5 gold! I now have " << gold << " gold" ;
return 0;
}
int main()
{
int iSecret;
srand ( time(NULL) );
iSecret = rand() % 100 + 1;
human player;
player.health = 100;
char choice;
cout << "\t\t\tWelcome to gerfy's game!\n\n " ;
cout << "In this game you want are trying to get across" ;
cout << " the jungle but there are many\n different animals that will attack you." ;
cout << " To get past the animals you must kill\n them. As you kill the animals you will" ;
cout << " you will get gold. You can choose to run away from the animal or attack it." ;
cout << " If you run away from the animal you will get more health. " ;
cout << "\n\n\t\t\t Now lets start the game!" ;
cout << "\n\n You run into a " ;
animal();
cout << " Do you want to [f]ight or [r]un away?" ;
cin >> choice;
if (choice == 'f' )
{
animals jaguar;
jaguar.attack();
cin.get();
cin.get();
}
else if (choice == 'r' )
{
run ();
cin.get();
cin.get();
}
else
{
cout << "Error" ;
cin.get();
cin.get();
}
return 0;
}
Last edited on Aug 24, 2012 at 10:36pm UTC
Aug 24, 2012 at 10:53pm UTC
how do i get it so the attacking repeats. What loop would I use for, while, or do while?
Aug 24, 2012 at 11:15pm UTC
There isn't really much difference in this case.
Topic archived. No new replies allowed.