#include <iostream>
using namespace std;
int main()
{
//Varibles
int enemyHP=100, enemyPower=200, yourHP=100, yourPower=200, yourCommand, enemyCommand;
//Begin ''while'' command
while (yourHP < 0 || enemyHP < 0)
{
//Your turn to attack or defend
cout << "Enter 1 to Attack \n Enter 2 to Defend \n";
cin >> yourCommand;
//Processes command
//Command to attack
if (yourCommand == 1)
{
if (yourPower < 15)
{
cout << "You do not have enough Power Points to Attack \n";
}
enemyHP = enemyHP - 15;
yourPower = yourPower - 5;
yourPower = yourPower + 3;
cout << "You have done 15HP of damage to your opponent and you lost 5 Power Points attacking. But you also gained 3 Power Points during this turn. \n";
}
//Command to defend
if (yourCommand == 2)
{
if (yourPower < 5)
{
cout << "You do not have enough Power Points to Defend \n";
}
yourHP = yourHP + 10;
yourPower = yourPower + 10;
cout << "By defending you have gained 10 Power Points. And you have also gained 10HP. \n";
}
//Enemy''s turn to attack or defend
// Command: If hits are more than 20 then attack
if (enemyHP > 20)
{
enemyCommand = 1;
}
// Command: If hits are less than 20 then defend
if (enemyHP < 20)
{
enemyCommand = 2;
}
//Processes command
if (enemyCommand == 1)
{
yourHP = yourHP - 15;
enemyPower = enemyPower - 5;
enemyPower = enemyPower + 3;
cout << "Your opponent has taken 15HP from you. \n";
}
if (enemyCommand == 2)
{
enemyHP = enemyHP + 10;
enemyPower = enemyPower + 10;
cout << "Since you opponent has decided to defend he gains 10 Power Points and 10HP.";
}
//End ''while'' command
}
if (yourHP < 0)
{
cout << "You lost";
}
if (enemyHP < 0)
{
cout << "You won";
}
int x;
cin >> x;
return 0;
}
Help With a Game I'm Making
I''m making a game where you fight the computer. But it didn''t work and I can''t find out what''s wrong. Here''s the code.
It helps if you can be more specific than "it didn''t work." What exactly does it do, and how does that differ from what it is supposed to do?
That said, I noticed this line:
while (yourHP < 0 || enemyHP < 0)
Since both yourHP and enemyHP start out at 100, the while loop is never entered.
That said, I noticed this line:
while (yourHP < 0 || enemyHP < 0)
Since both yourHP and enemyHP start out at 100, the while loop is never entered.
Well it should be
while ((yourHP > 0 && enemyHP > 0)
the way you have it now. the program won''t run because your energy is at 100, and you''re telling it to enter the loop when it''s below 0.
hope it helped.
if i''m wrong please tell us why.
i''m a beginner.
Knowledge is what you learn, wisdom is how you apply it.
while ((yourHP > 0 && enemyHP > 0)
the way you have it now. the program won''t run because your energy is at 100, and you''re telling it to enter the loop when it''s below 0.
hope it helped.
if i''m wrong please tell us why.
i''m a beginner.
Knowledge is what you learn, wisdom is how you apply it.
Nothing appears on the screen. Anyways. I changed the "while" to
----
while (yourHP > 0 && enemyHP > 0)
----
ANOTHER QUESTION:
How do I make it so the program randomly picks a varible.
For example, let's say there is a varible called "int test". And I want to asign it the value of either "1" or "2". How do I do that?
[edited by - bert2 on July 9, 2002 7:38:34 PM]
----
while (yourHP > 0 && enemyHP > 0)
----
ANOTHER QUESTION:
How do I make it so the program randomly picks a varible.
For example, let's say there is a varible called "int test". And I want to asign it the value of either "1" or "2". How do I do that?
[edited by - bert2 on July 9, 2002 7:38:34 PM]
firstly, initialize the random number generator using the srand() function. The best way to do so is to use the time() function, like so:
srand((unsigned int)time(0));
then, when you want a random number, you use the rand() function. Because it returns a number between 0 and RAND_MAX (a very large number), and you want to ''clip'' it to one of two possible values, you do it like this:
(rand()%2)+1
rand()%2 will always evaluate to either 0 (if rand() is even) or 1 (if rand() is odd). So you add 1 to get your 1 or 2.
Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net
srand((unsigned int)time(0));
then, when you want a random number, you use the rand() function. Because it returns a number between 0 and RAND_MAX (a very large number), and you want to ''clip'' it to one of two possible values, you do it like this:
(rand()%2)+1
rand()%2 will always evaluate to either 0 (if rand() is even) or 1 (if rand() is odd). So you add 1 to get your 1 or 2.
Superpig
- saving pigs from untimely fates
- sleeps in a ham-mock at www.thebinaryrefinery.cjb.net
Ummmm... Can you tell me how to write that? Also, how to I assign the random number to a varible?
quote:Original post by bert2
I''m making a game where you fight the computer.
The compiler recognized you were attacking it and told its big brother WiNdOzE not to go along with it.
I''m not quite sure if you meant that as a joke or if you really meant it and you were trying to explain to me why my program didn''t work before.
I have taken your code and made a few changes. If I were writing this, this is how I would do it.
Like I said, this is just how I would do it.
-Stick
[edited by - scubastick on July 10, 2002 2:11:43 PM]
#include <iostream>#include <ctime>#include <conio.h>using namespace std;int main(){ //Varibles int enemyHP=100, enemyPower=200, yourHP=100, yourPower=200, yourCommand, enemyCommand; //Initialize random seed srand((unsigned int)time(0)); //Begin 'while' command while (yourHP > 0 && enemyHP > 0) { cout << "Your HitPoints: " << yourHP << " / " << "Enemy HitPoints " << enemyHP << endl << endl; //Your turn to attack or defend cout << "Enter 1 to Attack" << endl << "Enter 2 to Defend" << endl << "Enter 3 to Quit" << endl << endl; cin >> yourCommand; //Processes command //Command to attack if (yourCommand == 1) { if (yourPower < 15) { cout << "You do not have enough Power Points to Attack \n"; } else { enemyHP = enemyHP - 15; yourPower = yourPower - 5; yourPower = yourPower + 3; cout << "You have done 15HP of damage to your opponent and you lost 5 Power Points attacking. But you also gained 3 Power Points during this turn. \n"; } } //Command to defend if (yourCommand == 2) { if (yourPower < 5) { cout << "You do not have enough Power Points to Defend \n"; } else { yourHP = yourHP + 10; yourPower = yourPower + 10; cout << "By defending you have gained 10 Power Points. And you have also gained 10HP. \n"; } } if (yourCommand == 3) { cout << "You have quit the battle." << endl; break; } //Enemy's turn to attack or defend // Command: If hits are more than 20 then attack enemyCommand = ((rand()%2)+1); //Processes command if (enemyCommand == 1) { yourHP = yourHP - 15; enemyPower = enemyPower - 5; enemyPower = enemyPower + 3; cout << "Your opponent has taken 15HP from you. \n"; } if (enemyCommand == 2) { enemyHP = enemyHP + 10; enemyPower = enemyPower + 10; cout << "Since you opponent has decided to defend he gains 10 Power Points and 10HP."; } //End 'while' command cout << endl << endl; } cout << "FINAL SCORE" << endl; cout << "Your HitPoints: " << yourHP << " / " << "Enemy HitPoints " << enemyHP << endl << endl; if (yourHP <= 0) { cout << "You lost" << endl; } if (enemyHP <= 0) { cout << "You won" << endl; } //Wait for enter to quit cout << "Press enter to quit." << endl; getch(); return 0;}
Like I said, this is just how I would do it.
-Stick
[edited by - scubastick on July 10, 2002 2:11:43 PM]
If you don''t decide to work with scubastick''s code then be sure you note this important change:
Your code:
His code:
This almost definitely why you do not know who has won at the end of your program (think about it).
Your code:
if (yourHP < 0) { cout << "You lost"; } if (enemyHP < 0) { cout << "You won"; }
His code:
if (yourHP <= 0) { cout << "You lost" << endl; } if (enemyHP <= 0) { cout << "You won" << endl; }
This almost definitely why you do not know who has won at the end of your program (think about it).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement