collapse collapse

* Who's Online

  • Dot Guests: 262
  • Dot Hidden: 0
  • Dot Users: 0

There aren't any users online.

* Board Stats

  • stats Total Members: 88
  • stats Total Posts: 11164
  • stats Total Topics: 1700
  • stats Total Categories: 4
  • stats Total Boards: 76
  • stats Most Online: 1470

Author Topic: C++ Game Help  (Read 4326 times)

0 Members and 1 Guest are viewing this topic.

Offline WizCoder

  • Newbie
  • *
  • Posts: 17
  • Reputation 24
    • View Profile
C++ Game Help
« on: September 17, 2012, 05:13:30 pm »
Code: [Select]
#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstring>

/* Defining our values */





using namespace std;

class Player{
    public:
    /* All integers for our player meaning YOU */
 
    char name[20];
   
    int attack;
    int defense;
    int health;
   
   
   
   
   
    void setHealth(int cHealth);
    void setDefense(int dDefense);
   
   
   
    };
   
void Player::setHealth(int cHealth){
   
    Player::health = cHealth; //Setting the integer health to equal to our new integer cHealth
   
   
    }
   
void Player::setDefense(int dDefense){
   
    Player::defense = dDefense; //Setting the integer defense to equal to our new integer cDefense
   
    }
   

class Monster{
   
    /* All integers and data for Monster Meaning BOT */
     public:
    int botattack;
    int botdefense;
    int bothealth;
   
   
   
    void setBHealth(int BHealth);
    void setBDefense(int BDefense);
   
   
   
    };

//Setting bots health  = our varible BHealth   
void Monster::setBHealth(int BHealth){
   
    Monster::bothealth = BHealth;
   
    }
   
//Setting bots defense  = our varible BDefense     
void Monster::setBDefense(int BDefense){
   
    Monster::botdefense = BDefense;
   
    }
int main(){
   
   
    Player Human;
    Monster Bot;
   
    /* Configuring our HUMAN.. UNPROTECTED */
   
   
    Human.setHealth(100); //We've given the Human(Player) a maximum of 100 Hitpoints
    Human.setDefense(50); //We've given the Human(Player) a maximum of 50 Defense Points
   
    int health;
    health = 100;
    cout <<"Choose a name for your Character."<<endl;
    cout <<endl;
    cout <<"Name: ";
    cin.getline(Human.name,20,'\n');
   
    do {
    /* Configuring our BOT. UNPROTECTED */
    Bot.setBHealth(20); //Vice Versa
    Bot.setBDefense(50); //Vice Versa to Human
   
   
 
   
   
    /* Player Stats */
    int input;
   
    cout << "Health: " << Human.health << endl;
    cout << "Defense: " << Human.defense << endl;
    cout << "------------------------"<<endl;
    cout <<endl;
   
   
   
    cout <<"1.Battle"<<endl;
    cout <<"2.Exit"<<endl;
    cout <<endl;
    cout <<"Selection: ";
    cin >> input;
   


    /* Battle Settings, RANDOM BOT */
   
    if ( input == 1 ) {
       
               
               
   
    while ( Bot.bothealth > 0 ) {
       
        Human.attack = rand() % 10;
        Bot.bothealth = Bot.bothealth - Human.attack;
       
        cout <<endl;
       
        cout <<"You attacked Enemy For " << Human.attack << endl;
        cout <<"Enemys Health is: " << Bot.bothealth << endl;
       
        cin.get();
       
        cout << endl;
        Bot.botattack = rand() % 5;
        Human.health = Human.health - Bot.botattack;
        cout <<"Enemy Attacked You For " << Bot.botattack << endl;
        cout <<"Your Health: " << Human.health <<endl;
       
        cin.get();
        }
       
       
   
    }   
}

while ( Bot.bothealth > 0 );
    int input;
if ( input == 1){
   
        Human.attack = rand() % 10;
        Bot.bothealth = Bot.bothealth - Human.attack;
       
        cout <<endl;
       
        cout <<"You attacked Enemy For " << Human.attack << endl;
        cout <<"Enemys Health is: " << Bot.bothealth << endl;
       
        cin.get();
       
        cout << endl;
        Bot.botattack = rand() % 5; //Random value Divided by 5. This can be adapted to 1+(rand()%10)   
        Human.health = Human.health - Bot.botattack;
        cout <<"Enemy Attacked You For " << Bot.botattack << endl;
        cout <<"Your Health: " << Human.health <<endl;
       
        cin.get();
   
    }
   
       
    cin.get();
    system("pause");
   
   
}

So basically once you run this youll be prompted with asking for your characters name then after you enter it you will bring you to the menu which will say type 1 to start the battle... Everytime battle has finished and you won I want it to bring you back to the main menu and allow you to type 1 again then it goes into battle.


Problem:
I've tried do while ,for, and while loops but everytime battle has finished it only brings to menu then when 1 is typed it doesn't redo the battle....

Can someone give me new code for this fixed or lead me to the right direction cause I've tried everything..

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: C++ Game Help
« Reply #1 on: September 17, 2012, 07:26:25 pm »
You need to set the bot health after they select something - otherwise everyone's health doesn't get reset and when you have the check:
Code: [Select]
while ( Bot.bothealth > 0 )It will always pass over because the bot's health will be 0 from the last "battle".
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

Offline WizCoder

  • Newbie
  • *
  • Posts: 17
  • Reputation 24
    • View Profile
Re: C++ Game Help
« Reply #2 on: September 17, 2012, 08:21:15 pm »
Yeah I was already aware of that , I did everything right the very first time I attempted ..
my problem was I got confused with < and > signs so I wrote the wrong one and that pretty much screwed everything up....


I got it fixed now and I can continue deving

Thanks for replying!

Offline Nathan

  • Administrator
  • Hero Member
  • *
  • Posts: 1437
  • Reputation 1768
  • Gender: Male
  • woof woof
    • View Profile
Re: C++ Game Help
« Reply #3 on: September 17, 2012, 09:37:08 pm »
Yeah I was already aware of that , I did everything right the very first time I attempted ..
my problem was I got confused with < and > signs so I wrote the wrong one and that pretty much screwed everything up....


I got it fixed now and I can continue deving

Thanks for replying!
What was your solution?
Projects:
[ Axios Engine ] [ sourcehub ]
Compilers: Microsoft Visual Studio 2008, GNU C++, FASM, MASM, VB 6/.Net.
Languages: C++, PHP, ASM, JS, VB6/.Net, BASIC, HTML, MySQL
Please buy me some books: Amazon Wishlist

 

Donate


* Search


* Recent Posts

I miss the chatbox by Shishka
[August 27, 2019, 11:52:39 pm]


Image Comparison by Shishka
[May 15, 2017, 01:18:02 pm]


Re: srchub - free source code hosting by Nathan
[December 14, 2015, 11:37:02 pm]


Re: srchub - free source code hosting by Celestialkey
[November 27, 2015, 08:51:42 am]


Updates by Nathan
[October 30, 2015, 08:27:36 pm]