Cleaning up my code and Help displaying game over
Posted: Mon Nov 10, 2008 4:01 pm
Yes Im a Noob and its my first dedicated program...
You probably have to see the whole Program to see the problem...
main.c
main.h
The problem that it does not display "GAME OVER" when the hp reached 0 or lower, I think the problem is that the code that says to run that never actually runs while its during a battle since its in a different function. I was thinking to put it in a battle function, it runs twice since its in the game function... so just help put it into separate source codes and use classes, I tried before, it failed epicly...
You probably have to see the whole Program to see the problem...
main.c
Code: Select all
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "main.h"
using namespace std;
void Shop();
void fight();
void SkillPoint();
void levelup();
void Items();
void Save();
void Load();
void Game();
void NewGame();
int main(int argc, char *argv[])
{
cout <<"1=Load \t 2=New Game" << endl;
cin >> choice;
if (choice == 1)
{
Load();
}
if (choice == 2)
{
NewGame();
}
}
void fight()
{
cout << "Bat appeared!" << endl;
for (;BatHP >0;)
{
cout << BatHP <<"/" << OGBatHP <<"\nYour HP: "<< hp <<"/"<< fullhp <<"\nYour MP: "<<mp <<"/"<< fullmp << endl;
cout << "1=Attack 2=Power Hit 3=Stab 4=Item" << endl;
cin >> choice1;
if (choice1 == 1)
{
BatHP = BatHP - (BatDEF / 2 + atk);
cout << "Bat took " << +(BatDEF / 2 + atk)
<< " Damage" << endl;
if(BatHP <= 0)
{
cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
exp += exprate;
money += 30;
OGBatHP += 50;
BatDEF += 10;
BatAtk += 10;
exprate += 10;
}
if(BatHP >= 0)
{
hp = hp - (def / 2 + BatAtk);
cout << "You took " << -(def / 2 + BatAtk)
<< " Damage" << endl;
}
}
else if ((choice1 == 2) && (Super > 0) && (mp >= 150))
{
mp -= 150;
cout <<"MP-150\nPower Hit!"<< endl;
BatHP = BatHP - (BatDEF / 2 + (atk * Super));
cout << "Bat took " << -(BatDEF / 2 - (atk * Super))
<< " Damage" << endl;
if(BatHP <= 0)
{
cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
exp += exprate;
money += 30;
OGBatHP += 50;
BatDEF += 10;
BatAtk += 10;
exprate += 10;
}
if(BatHP >= 0)
{
hp = hp - (def / 2 + BatAtk);
cout << "You took " << -(def / 2 + BatAtk)
<< " Damage" << endl;
}
}
else if ((choice1 == 3) && (Stab > 0) && (mp >= 200))
{
mp -= 200;
cout <<"MP-200\nStab!!!"<< endl;
BatHP = BatHP - ((atk * Stab)/2);
cout << "Bat took " << (atk * Stab)/2
<< " Damage" << endl;
if(BatHP <= 0)
{
cout << "***Bat Died!*** \n you gained " << (exp += exprate) <<"exp.\n you got 30 money" << endl;
exp += exprate;
money += 30;
OGBatHP += 50;
BatDEF += 10;
BatAtk += 10;
exprate += 10;
}
if(BatHP >= 0)
{
hp = hp - (def / 2 + BatAtk);
cout << "You took " << -(def / 2 + BatAtk)
<< " Damage" << endl;
}
}
else if (choice1 == 4)
{
Items();
}
else
{
cout <<"No Can Do"<< endl;
}
}
}
void SkillPoint()
{
cout <<"HP Mastery=" << hpmastery << endl;
cout <<"MP Mastery=" << mpmastery << endl;
cout <<"Attack Mastery=" << atkmastery << endl;
cout <<"Defence Mastery=" << defmastery << endl;
cout <<"Skill Points=" << SP << endl;
cout <<"What would you like to upgrade?" << endl;
cout <<"1=HP Mastery \t 2=MP Mastery \t 3=Attack Mastery \t 4=Defence Mastery \n5=Power Hit \t 6=Stab" <<endl;
cin >> choice;
if ((choice == 1) && (SP > 0))
{
SP -= 1;
hpmastery += 5;
cout <<"HP Mastery Up!" << endl;
}
else if ((choice == 2) && (SP > 0))
{
SP -= 1;
mpmastery += 5;
cout <<"MP Mastery Up!" << endl;
}
else if ((choice == 3) && (SP > 0))
{
SP -= 1;
atkmastery += 10;
cout <<"Attack Mastery Up!" << endl;
}
else if ((choice == 4) && (SP > 0))
{
SP -= 1;
defmastery += 10;
cout <<"Defence Mastery Up!" << endl;
}
else if ((choice == 5) && (SP > 0) && (Super < 10))
{
SP -= 1;
Super += 1;
cout <<"Power Hit Up!"<< endl;
}
else if ((choice == 6) && (SP > 0) && (Stab < 10))
{
SP -= 1;
Stab += 1;
cout <<"Stab Up!"<< endl;
}
else
{
cout <<"No can do" << endl;
}
}
void levelup()
{
level ++;
fullhp += hpmastery;
fullmp += mpmastery;
hp = fullhp;
mp = fullmp;
atk += atkmastery;
def += defmastery;
SP += 3;
explevel *=2;
cout <<"Level Up!" << endl;
cout << hp <<"+" << hpmastery <<"=" <<hp + hpmastery <<" HP"<< endl;
cout << mp <<"+" << mpmastery <<"=" <<mp + mpmastery <<" MP"<< endl;
cout << atk <<"+" << atkmastery <<"=" <<atk + atkmastery <<" Attack"<< endl;
cout << def <<"+" << defmastery <<"=" <<def + defmastery <<" Defence"<< endl;
}
void Items()
{
cout <<"1= HP Potion " << HPpot <<" 2= MP Potion " << MPpot <<" 3= Back" << endl;
cin >> choice;
if ((choice == 1) && (HPpot > 0) && (hp <= fullhp))
{
HPpot --;
hp += 100;
cout <<"Healed 100 HP" <<endl;
}
else if ((choice == 2) && (MPpot > 0) && (mp <= fullmp))
{
MPpot --;
mp += 100;
cout <<"Healed 100 MP" <<endl;
}
else if (choice == 3)
{
cout <<"OK" << endl;
}
else
{
cout <<"No can do" << endl;
}
}
void Save()
{
ofstream save("DaveSata.dav");
save << fixed;
save << left;
save << name << endl;
save << level << endl;
save << money << endl;
save << exp << endl;
save << explevel << endl;
save << exprate << endl;
save << hp << endl;
save << mp << endl;
save << hpmastery << endl;
save << mpmastery << endl;
save << fullhp << endl;
save << fullmp << endl;
save << atk << endl;
save << def << endl;
save << atkmastery << endl;
save << defmastery << endl;
save << Super << endl;
save << Stab << endl;
save << HPpot << endl;
save << MPpot << endl;
save << BatHP << endl;
save << BatDEF << endl;
save << BatAtk << endl;
save << OGBatHP << endl;
save.close();
cout <<"Game Saved!" << endl;
}
void Load()
{
ifstream load("DaveSata.dav");
load >> name;
load >> level;
load >> money;
load >> exp;
load >> explevel;
load >> exprate;
load >> hp;
load >> mp;
load >> hpmastery;
load >> mpmastery;
load >> fullhp;
load >> fullmp;
load >> atk;
load >> def;
load >> atkmastery;
load >> defmastery;
load >> Super;
load >> Stab;
load >> HPpot;
load >> MPpot;
load >> BatHP;
load >> BatDEF;
load >> BatAtk;
load >> OGBatHP;
cout <<"Game Loaded!" << endl;
Game();
}
void Game()
{
for(; hp > 0 ;)
{
cout <<"\n\n\n\n\n\n\n\n\n\n\n";
cout << "Hi " << name << " how are you doing?" << endl;
cout <<"Level=" << level << endl;
cout <<"HP=" << hp <<"/"<< fullhp << "\t Attack=" << atk << endl;
cout <<"MP=" << mp <<"/"<< fullmp << "\t Defence=" << def << endl;
cout <<"Money=" << money << "\t EXP=" << exp <<"/"<<explevel<< endl;
cout <<"HP Mastery=" << hpmastery << endl;
cout <<"MP Mastery=" << mpmastery << endl;
cout <<"Attack Mastery=" << atkmastery << endl;
cout <<"Defence Mastery=" << defmastery << endl;
cout <<"1=Inn \t 2=Shop \t 3=Suicide" << endl <<
"4=Fight \t 5=Skills \t 6=Items" << endl <<
"7=Save" << endl;
cin >> choice;
if ((choice == 1) && (money >= 50))
{
hp = fullhp;
mp = fullmp;
money -= 50;
cout <<"Your HP and MP is Fully Restored" << endl;
}
else if (choice == 2)
{
Shop();
}
else if (choice == 3)
{
cout <<"You stab yourself!" << endl;
while (hp>=0) {
cout << "HP:" << hp << "\n";
--hp;
}
}
else if (choice == 4)
{
BatHP = OGBatHP;
fight();
}
else if (choice == 5)
{
SkillPoint();
}
else if (choice == 6)
{
Items();
}
else if (choice == 7)
{
Save();
}
else
{
cout << "Make another selection." << endl;
}
if (exp >= explevel)
{
levelup();
}
if (hp <=0)
{
cout << "GAME OVER" << endl;
system("color 40");
system("PAUSE");
}
}
system("PAUSE");
}
void Shop()
{
int choice2;
cout <<"Hello welcome to the Shop \nWhat would you like?\n 1=Attack Up 100 money \t 2=Defence Up 100 money \n 3=HP Potion 50 money\t 4=MP Potion 50 money" << endl;
cin >> choice2;
if ((choice2 == 1) && (money >= 100))
{
atk += 10;
money -= 100;
cout <<"Attack + 10" << endl;
}
else if ((choice2 == 2) && (money >= 100))
{
def += 10;
money -= 100;
cout <<"Defence + 10" << endl;
}
else if ((choice2 == 3) && (money >= 50))
{
HPpot ++;
money -= 50;
cout <<"HP Potion +1" << endl;
}
else if ((choice2 == 4) && (money >= 50))
{
MPpot ++;
money -= 50;
cout <<"MP Potion +1" << endl;
}
else
{
cout <<"No can do" << endl;
}
}
void NewGame()
{
cout << "What is your name?" << endl;
cin >> name;
hp = 200,mp = 200,fullhp = 200, fullmp = 200, money = 100, choice , level = 1 ,hpmastery = 25,mpmastery = 25,atk = 50,def = 50,atkmastery = 10, defmastery = 10,SP = 0;
BatHP, BatDEF = 10, BatAtk = 10, choice1,OGBatHP = 100;
explevel = 100, exp = 0,exprate = 10;
Super = 0, Stab = 0;
HPpot = 0, MPpot = 0;
Game();
}
Code: Select all
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
string name;
int hp ,mp,fullhp, fullmp, money, choice , level ,hpmastery,mpmastery,atk,def,atkmastery, defmastery,SP;
int BatHP, BatDEF, BatAtk, choice1,OGBatHP;
int explevel, exp,exprate;
int Super, Stab;
int HPpot, MPpot;
The problem that it does not display "GAME OVER" when the hp reached 0 or lower, I think the problem is that the code that says to run that never actually runs while its during a battle since its in a different function. I was thinking to put it in a battle function, it runs twice since its in the game function... so just help put it into separate source codes and use classes, I tried before, it failed epicly...