473,462 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Handling damage in a game

I have to make a game, where damage is inflicted and subtracted from health after every turn. there is you and the cpu. choices 1 and 2 have limited uses. I cannot figure out how to get the damage to store through out the entire program. here is what i have:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int damage = rand();
  8.     int health = 100;
  9.    const int cannon = 1;
  10.    const int grenade = 2;
  11.    const int rifle = 3;
  12.     int weapon;
  13.     int num_c = 3,    
  14.         num_g = 4; 
  15.  
  16.     cout << "\tTEXT BATTLE GAME\n";
  17.     cout << "Choose A Weapon <1, 2, or 3>\n"
  18.         << "1. cannon: " << num_c << " left\n"
  19.         << "2. grenade: " << num_g << " left\n"
  20.         << "3. rifle\n";
  21.     cin >> weapon;
  22.  
  23.     if (weapon == cannon)
  24.     {    
  25.         int damage = 10 + rand() % 6;
  26.         cout << "You've inflicted " << damage << " damage points on the enemy.\n";
  27.         cout << "Computer: " << health - damage << endl << "You: " << health << endl; 
  28.         num_c--;
  29.         cin.get();
  30.     }
  31.     else if (weapon == grenade)
  32.     {
  33.         int damage = 7 + rand() % 6;
  34.         cout <<"You've inflicted " << damage << " damage points on the enemy.\n";
  35.         cout << "Computer: " << health - damage << endl << "You: " << health << endl;
  36.         num_g--;
  37.         cin.get();
  38.     }
  39.     else if (weapon == rifle)
  40.     {    
  41.         int damage = 3 + rand() % 6;
  42.         cout << " You've inflicted " << damage << " damage points on the enemy.\n";
  43.         cout << "Computer: " << health - damage << endl << "You: " << health << endl;
  44.         cin.get();
  45.     }
  46.     else
  47.     cin.get();
  48.  
  49.     cout << "Press any key to continue....." << cin.get() << endl;
  50.     int computersChoice = 1 + rand() % 3;
  51.  
  52.     if (computersChoice = 1)
  53.     {
  54.         int damage = 10 + rand() % 6;
  55.         cout << "The computer used a cannon in the attack.\n";
  56.         cout << "The computer inflicted " << damage << " damage points to you.\n";
  57.         cout << "Computer: " << health << endl << "You: " << health - damage << endl;
  58.         num_c--;
  59.     }
  60.     else if (computersChoice == 2)
  61.     {
  62.         int damage = 7 + rand() % 6;
  63.         cout << "The computer used a grenade in the attack.\n";
  64.         cout << "The computer inflicted " << damage << " damage points to you.\n";
  65.         cout << "Computer: " << health << endl << "You: " << health - damage << endl;
  66.         num_g--;
  67.     }
  68.     else if (computersChoice == 3)
  69.     {
  70.         int damage = 3 + rand() % 6;
  71.         cout << "The computer used a rifle in the attack.\n";
  72.         cout << "The computer inflicted " << damage << " damage points to you.\n";
  73.         cout << "Computer: " << health << endl << "You: " << health - damage << endl;
  74.     }
  75.     else
  76.         cin.get();
  77. cin.get();
  78. return 0;
  79. }
Feb 13 '12 #1
1 2378
weaknessforcats
9,208 Expert Mod 8TB
It's because you are creating an int damage inside every pair of braces. Then when the program leaves the braces, that variable is destroyed.

Just do this:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    int damage;  //this is your single damage variable
  4.  
  5.    if (something)
  6.    {
  7.       damage =  newvalue;  //uses the already created variable
  8.    }
  9. }
rather than:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    int damage;  //this is your single damage variable
  4.  
  5.    if (something)
  6.    {
  7.       int damage =  newvalue;  //creates another variable
  8.    }
  9. }
Feb 14 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
7
by: Brandon J. Van Every | last post by:
Anyone know of any "good" open source C# game projects out there? Something that actually has a game engine and some content done, so I can just fiddle with it and do interesting / goofy things. ...
1
by: Jerry Fleming | last post by:
Hi, I have wrote a game with python curses. The problem is that I want to confirm before quitting, while my implementation doesn't seem to work. Anyone can help me? #!/usr/bin/python # #...
1
by: fowle040 | last post by:
I underlined and bold print my files. I need to know how to make this code into a working game. The object of the game is to have two players 1- belle and 2-beast. I want them to lose and gain...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
5
by: alesitaam | last post by:
Help!!!! Im new using python, currently writing a program which tests one game, IQ test. When the module is run, the program should ask user to choose the game to start. Also, I'm using Try...Except...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.