I made something like this and posted for help a while ago, but that code was sloppy so i re-wrote it all and now i have a problem. I put in a random number generator that generates a number between 0 and 4, when it does that its supposed to make the opponent choose a move using switch statements but its not working it just outputs the random number generated. Its kind of like a pokemon battle system. I get no compiler errors it just doesnt work the way i want it to, run it and you'll see what i mean if you need to.
You're not storing the randomized value in anything... you're just calling rand(), applying a modulo, and printing it. You don't have a variable that's holding on to its value. :|
You just need some variable, like an int to store what rand() % 5 gives you back.
srand() (which should only be called once, by the way) sets up the random number generator, while rand() gives you back a random number every time you call it. The % puts a limit on the range of random numbers.
Um... no, the way you were calling srand() the first time was fine, it's just that you only needed to do it once in your whole program.
What you need to change is the way you're using the rand() % 5. Right now, you're printing the value it returns. Wouldn't you rather use = to store in in a variable so you can get it back later?*
*You actually don't really need a variable, but it makes things simpler.
ok i got it working but i have another problem. I use a move, then the opponent uses a move, then the game ends? its suppoosed to keep going back and forth? why isnt it doing that?
Ok i have one more problem. When the health of the player or the opponent reach 0 i want it to display a message saying something but that never happens. It just keeps subtracting health? why is that?
Inside player() you call TRex(), and TRex() calls player(). You just have loops within loops.
TRex() shouldn't have a loop in it, which means it doesn't need a "turn". Also make sure to not call player.
Then you get back to the original player(), but the loop doesn't ask for a choice again, so you need to move the start of the while up a little. (to line 50).
The loop in player can be while(true), only ending via break when health is too low.
But, when the game is over, you call Entrance, which is going to make another game inside the game you are playing. Rather, simply have those conditions break the loop so that the function ends.
Then you get back to entrance, and it is done with so the program ends. You could make a loop in entrance that only continues while choice is "Go". Make sure to cin >> choice from within the loop, otherwise it can't update.
Something that is hiding these errors is that Player and TRex health is a global variable. Consider having the health's within player() and then pass the player's health as a reference variable to TRex.
time_t T;
time(&T);
srand(T);
int A;
for(int R = 0; R < 3; ++R)
{
A = rand() % 2;
}
switch(A)
{
case 0:
TRex();
break;
case 1:
VRaptor();
break;
}
if(A == 1)
{
opponentH == TRexHealth;
}
elseif(A == 2)
{
opponentH == VRaptorHealth;
}
this is supposed to choose an opponent and based on the opponent it chooses it will assign opponentH to the opponents health. but when i run it it keeps looping and screwing up, what am i doing wrong? Here is all my code.
its called object oriented programming, and making an object called dinosaur would allow you to create multiple dinosaurs without making a whole new set of variables for each one. just a new object.
It can make your job easier, you can create a "Basic" Dinosaur, and create all the other Dinosaurs based on the "Basic" one, without needing to rewrite most of the code.