Assingment variable problmes
Jun 9, 2010 at 3:18pm UTC
Ok so I have this game I am working on and I am trying to stream line it a bit by cuting out a lot of the if else statments by replacing them with this;
1 2 3 4 5 6
case 's' :
ninja.hp = ninja.hp - (shiningStrike * (die/100));
cout<<die<<" Damage " <<endl;
cout<<ninja.hp<<endl;
break ;
But when I debug the ninja hp is not updateing when I press s, but it ninja hp is decresed when I press v or x. Am I writing something wrong here?
Thank you
here the whole thing if needed;
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
//good guy ie player
class Player
{
public :
int hp;
int sp;
int mp;
};
class Bot
{
public :
int hp; //hit points for emeny
int atk; //damage to player health
};
void atkTimer(){
cout << "Player attacked please wait" << endl;
//time guage for attack bar player cannot attck for 5 seconds
Sleep(5000); //this is in milliseconds
}
int main()
{
//attack related items and skills
//attack bar
bool atb = true ; //if false then player cannot attack
//amount of damage is abitray
int slash = 10; //players pushs x does 10 in damage
int flameSlash = 20; //player pushs s does 20 in damage
int shiningStrike = 30; //player pushs v does 30 in damage
//skill wheel does x in damage
int powerDive = 15;
int powerCharge = 25;
int wildStrike = 35;
int renegade = 40;
int shiningStars = 50;
//magic
int light = 15;
int flame = 25;
int snow = 150;
//itmes
int poten = 30;
int snikers = 100;
int powerbar = 200;
//build player
Player Maverick;
Maverick.hp = 1220;
Maverick.mp = 220;
Maverick.sp = 100;
Player Cap;
Cap.hp = 50;
Cap.mp = 100;
Cap.sp = 155;
Player Jen;
Jen.hp = 1555;
Jen.mp = 555;
Jen.sp = 111;
//build bot
Bot ninja;
ninja.hp = 500;
ninja.atk = 20;
//array to hold characters
//This is for characters
Player players[3];
players[0] = Maverick;
players[1] = Cap;
players[2] = Jen;
players[0].hp = 1500;
//print off player data
cout << "Maverick" << endl;
cout << "Health " << Maverick.hp <<endl;
cout << "Magic " << Maverick.mp <<endl;
cout << "Skill " << Maverick.sp <<endl;
cout <<endl;
//print off ninja
cout << "Enemy Ninja Health " << ninja.hp <<endl;
//player battle starts
while (ninja.hp > 0 || Maverick.hp < 0) //fight until ninja or player dies
{
srand(time(0)); //seeds
int randNum = rand();
int die = (randNum % 100) +1; //gets number between 1 - 100
//attack input code
cout << "Attack!" << endl;
char button; //input for button pressed
cin >> button;
//input is either x v or s on key board.
switch (button){
case 'x' :
if (die <= 20)
{
ninja.hp -= (slash * .20);//does 20 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 30)
{
ninja.hp -= (slash * .30);//does 30 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 40)
{
ninja.hp -= (slash * .40);//does 40 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 50)
{
ninja.hp -= (slash * .50);//does 50 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 60)
{
ninja.hp -= (slash * .60);//does 60 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 70)
{
ninja.hp -= (slash * .70);//does 70 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 80)
{
ninja.hp -= (slash * .80);//does 80 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 90)
{
ninja.hp -= (slash * .90);//does 90 % of damage
cout<<die<<"%" <<endl;
}else if (die >= 90)
{
ninja.hp -= (slash + 20);//does 30 % of damage
cout<<die<<"% Critical Hit!" <<endl;
}
break ;
case 'v' :
if (die <= 20)
{
ninja.hp -= (flameSlash * .20);//does 20 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 30)
{
ninja.hp -= (flameSlash * .30);//does 30 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 40)
{
ninja.hp -= (flameSlash * .40);//does 40 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 50)
{
ninja.hp -= (flameSlash * .50);//does 50 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 60)
{
ninja.hp -= (flameSlash * .60);//does 60 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 70)
{
ninja.hp -= (flameSlash * .70);//does 70 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 80)
{
ninja.hp -= (flameSlash * .80);//does 80 % of damage
cout<<die<<"%" <<endl;
}else if (die <= 90)
{
ninja.hp -= (flameSlash * .90);//does 90 % of damage
cout<<die<<"%" <<endl;
}else if (die >= 90)
{
ninja.hp -= (flameSlash + 20);//does 30 % of damage
cout<<die<<"% Critical Hit!" <<endl;
}
break ;
case 's' :
ninja.hp = ninja.hp - (shiningStrike * (die/100));
cout<<die<<" Damage " <<endl;
cout<<ninja.hp<<endl;
break ;
case 'l' :
cout << "Skill Wheel" <<endl;
cout << "1 for Power Dive" <<endl;
cout << "2 for Power Charge" <<endl;
cout << "3 for Wild Strike" <<endl;
cout << "4 for Renegade" <<endl;
cout << "5 for Shining Stars" <<endl;
int button1;
cin >> button1;
//inner if else
if (button1 == 1)
{
ninja.hp -= powerDive;
}else if (button1 == 2)
{
ninja.hp -= powerCharge;
}else if (button1 == 3)
{
ninja.hp -= wildStrike;
}else if (button1 == 4)
{
ninja.hp -= renegade;
}else if (button1 == 5)
{
ninja.hp -= shiningStars;
}
break ;
case 'r' :
cout << "Press 1 for Light" <<endl;
cout << "Press 2 for Flame" <<endl;
cout << "Press 3 for Snow" <<endl;
int button2;
cin >> button2;
//inner if else
if (button2 == 1)
{
Maverick.hp += light;
cout<<"Health Regined" <<endl;
}else if (button2 == 2)
{
ninja.hp -= flame;
}else if (button2 == 3)
{
ninja.hp -= snow;
}
break ;
case 'i' :
cout << "Press 1 for Poten" <<endl;
cout << "Press 2 for Snikers" <<endl;
cout << "Press 3 for PowerBar" <<endl;
int button3;
cin >> button3;
//inner if else
if (button3 == 1)
{
Maverick.hp += poten;
}else if (button3 == 2)
{
Maverick.hp += snikers;
}else if (button3 == 3)
{
Maverick.hp += powerbar;
}
break ;
case 'd' :
int tempAtt;
tempAtt = ninja.atk/2;
Maverick.hp -= tempAtt;
break ;
case 'c' :
//charater switch command
break ;
}//end of switch for input
cout << "Enemy Ninja Health " << ninja.hp <<endl;
atb = false ;
//ninja stricks like the flu
Maverick.hp -= ninja.atk;
atkTimer();
atb = true ; //player can attack now
}//end of while
return 0;
}
Jun 9, 2010 at 4:54pm UTC
when you do:
char button; //input for button pressed
cin >> button;
Some times its works bad with cin char
I think you ca try to do
scanf("%c",&button);
And maybe you must use fflush to flush ios
Jun 9, 2010 at 5:06pm UTC
Thanks for the input;
I tried the
scanf("%c",&button);
but it still gave the same problme but thne it went though the simulation twice instead of once.
Jun 9, 2010 at 5:30pm UTC
die
is an integer from 1 to 100. Therefore die/100
is one if die==100
and zero in every other case. Fix it by doing it die/100.0
. This forces your compiler to make a double division (instead of int). It should be fine now.
EDIT: OODAMA RASENGAN!!!
Last edited on Jun 9, 2010 at 5:35pm UTC
Jun 9, 2010 at 7:15pm UTC
Thanks Master, that fixed it. ORO and I just got out of my first calculus class last semester.
Topic archived. No new replies allowed.