Thread: problem with my 2. game constructor problem I think

  1. #1
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    problem with my 2. game constructor problem I think

    my first game works well(guess game)

    hi I'm doing my self 2. game and I have a problem at save point

    if I'd know how to save a file it would be much better

    but... ... I have problem as game don't want to save as I thought it would
    it uses constructor where it shouldn't
    or at least I think like that (PC never makes mistake) --> don't know why it uses constructor

    main menu.cpp
    Code:
    #include "header1.h"
    
    
    
    int main()
    {
        login log;
        string nameq;
        nameq = log.return_name ();
        
    
    
        
        
        cout << "                               main menu\n\n\n";
        cout << "                            choose your game\n\n\n";
        for (int menu = 0;menu == 0;menu--)
        {
            int mainmenu;
            cout <<"1. Play guessgame\n";
            cout <<"2. Play game 2\n";
            cout <<"10. Exit\n";
            cin >> mainmenu;
            switch (mainmenu)
                   {
                   
                   case 1:
                        guessgame (nameq);
                        menu = 1;
                        break;
                   case 2:
                        menugame2 (nameq);
                        menu = 1;
                        break;
                   case 10:
                        menu = 0;
                        cout << "goodbye\n";
                        break;
                   default:
                   menu = 1;
                   cout << "wrong command try again\n";
                   break;
                   }
        }
        return 0;
    }

    header1.h
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    #include "intro.h"
    #include "guessgame1.h"
    #include "game2menu.h"

    intro.h
    Code:
    class login
    {
          private:
                  string name;
          public:
                 string return_name ()
                 {
                        cout << "before you enter to our games please tell us your name\n\n";
                        getline (cin, name);
                        cout << "thank you for sharring information " << name << endl;
                        return name;
                 }
    };

    guessgame1.h
    Code:
    void guessgame (string name)
    {//1
         cout << "                             hello: "<< name <<"\n\n\n";
         cout << "                               this is the game\n\n\n";
         cout << "                             Guess my number 1 - 10\n\n\n\n\n\n";
         int x = 0;
         int y = 0;
         for (int game = 0; (!(x == 10 || y == 10)) ;game++)
         {//2
             cout << "\n\n\n\ntry to guess what is my number\n";
             int mynumber;
             mynumber = (rand ()%10);
             int x1;
             cin >> x1;
             
             if ( x1 == mynumber)
                {//3.1
                     x++;
                     cout << "\n\n\nyou win\n";
                     
                     if (x == 10)
                     {//4 - 3.1
                           cout << "\nyou won "<< x <<" / computer won " << y << ".\n";
                           cout << "\n\n\nyou won the game\n";
                           system ("pause");
                           return;
                     }//4 - 3.1
                     
                }//3.1
                
             else if ( x1 != mynumber)
                {//3.2
                     y++;
                     cout << "\n\n\ncomputer won\n";
                     
                     if (y == 10)
                     {//4 - 3.1
                           cout << "\nyou won "<< x <<" / computer won " << y << ".\n";
                           cout << "\n\n\nthe computer has won the game\n";
                           system ("pause");
                           return;
                     }//4 - 3.1
                }//3.2
             cout << "\nyou won "<< x <<" / computer won " << y << ".\n";
         }//2
    }//1

    game2menu.h
    Code:
    #include "save.h"
    #include "load.h"
    #include "game2.h"
    
    
    void menugame2 (string name)
    {
              for (int menu_choice = 0;menu_choice == 0;menu_choice--)
              {
                  game2class game;
                  cout << "1 New Game\n" ;
                  cout << "2 Save Game\n" ;
                  cout << "3 Load Game\n" ;
                  cout << "10 Exit Game\n" ;
                  
                  
                  
                  
                  
                  int health;
                  int attack;
                  int strength;
                  int defense;
                  int stamina;
                  int exp_point;
                  
    
                  
                  
                  
                  int menu;
                  cin >> menu;
                  switch (menu)
                  {
                         case 1:
                              game.game2 (name);
                              menu_choice = 1;
                              break;
                         case 2:
                              health = game.savegame_health ();
                              attack = game.savegame_attack ();
                              strength = game.savegame_strength ();
                              defense = game.savegame_defense ();
                              stamina = game.savegame_stamina ();
                              exp_point = game.savegame_exp_point ();
                              
                              cout << "save contence " << health << endl;
                              cout << "save contence " << attack << endl;
                              cout << "save contence " << strength << endl;
                              cout << "save contence " << defense << endl;
                              cout << "save contence " << stamina << endl;
                              cout << "save contence " << exp_point << endl;
                              
                                                        
                              menu_choice = 1;
                              break;
                         case 3:
                              load ();
                              menu_choice = 1;
                              break;
                         case 10:
                              menu_choice = 0;
                              break;
                         default:
                                 menu_choice = 1;
                                 cout << "wrong command try again\n";
                                 break;
                  }
              }
    };

    save.h
    Code:
    class save
    {
          public:
                 void save_health    (int save_health)
                 {
                      health = save_health;
                 }
                 void save_attack    (int save_attack)
                 {
                      attack = save_attack;
                 }
                 void save_strength  (int save_strength)
                 {
                      strength = save_strength;
                 }
                 void save_defense   (int save_defense)
                 {
                      defense = save_defense;
                 }
                 void save_stamina   (int save_stamina)
                 {
                      stamina = save_stamina;
                 }
                 void save_exp_point (int save_exp_point)
                 {
                      exp_point = save_exp_point;
                 }
                 
                 void save1 ()
                 {
                      cout << health << endl;
                      cout << attack << endl;
                      cout << strength << endl;
                      cout << defense << endl;
                      cout << stamina << endl;
                      cout << exp_point << endl;
                 }
                 
                 int health;
                 int attack;
                 int strength;
                 int defense;
                 int stamina;
                 int exp_point;
    };

    load.h
    Code:
    void load ()
    {
         cout << "load" ;
    }

    game2.h
    Code:
    #include "player.h"
    
    
    bool dungeon ()
    {
         cout << "you were attacked by dragon" << endl;
         int combat;
         cout << combat;
    }
    
    
    
    
    
    
    
    class game2class
    {
          public:
                 void game2 (string name)
                 {//void
                      cout << "                             hello: "<< name <<"\n\n\n";
                      cout << "                               this is the game\n\n\n";
                      cout << "                             Dungeon and dragons\n\n\n\n\n\n";
                      player.townchoice ();
                      for (int town_choice = 0;town_choice == 0;town_choice--)
                      {//loop
                          
                          
                          int town;
                          cin >> town;
                          switch (town)
                          {//switch
                                 case 1:
                                      player.townchoice ();
                                      town_choice = 1;
                                      break;
                                 case 2:
                                      player.stats ();
                                      town_choice = 1;
                                      break;
                                 case 3:
                                      player.increaseattack ();
                                      town_choice = 1;
                                      break;
                                 case 4:
                                      player.increasestrength ();
                                      town_choice = 1;
                                      break;
                                 case 5:
                                      player.increasedefense ();
                                      town_choice = 1;
                                      break;
                                 case 6:
                                      player.increasestamina ();
                                      town_choice = 1;
                                      break;
                                 case 7:
                                      dungeon ();
                                      town_choice = 1;
                                      break;
                                 case 10:
                                      town_choice = 0;
                                      break;
                                 default:
                                         town_choice = 1;
                                         cout << "wrong command try again\n";
                                         break;
                          }//switch
                      }//loop
    
    
                      cout << "\n\n\nsorry this game is still in development\n" << endl;
                 }//void
                 
                 
                 
                 
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    //this is save contence
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int savegame_health ()
                 {
                     save_health = player.save_health ();
                     return save_health ;
                 }
                              
                 int savegame_attack ()
                 {
                     save_attack = player.save_attack ();
                     return save_attack ;
                 }
                 
                 int savegame_strength ()
                 {
                     save_strength = player.save_strength ();
                     return save_strength ;
                 }
                 
                 int savegame_defense ()
                 {
                     save_defense = player.save_defense ();
                     return save_defense ;
                 }
                 
                 int savegame_stamina ()
                 {
                     save_stamina = player.save_stamina ();
                     return save_stamina ;
                 }
                 
                 int savegame_exp_point ()
                 {
                     save_exp_point = player.save_exp_point ();
                     return save_exp_point ;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    
                 playerplayer player;
    
    
                 int save_health;
                 int save_attack;
                 int save_strength;
                 int save_defense;
                 int save_stamina;
                 int save_exp_point;
    
    
    };//game2class

    player.h
    Code:
    class playerplayer
    {
          public:
                 playerplayer ()
                 {
                       health    = 10;
                       attack    = 10;
                       strength  = 10;
                       defense   = 10;
                       stamina   = 10;
                       exp_point = 10;
                 }
    ///////////////////////////////////////
    // save contence                     //
    /////////////                        //
                 int save_health ()      //
                 {                       //
                     return health;      //
                 }                       //
                 int save_attack ()      //
                 {                       //
                     return attack;      //
                 }                       //
                 int save_strength ()    //
                 {                       //
                     return strength;    //
                 }                       //
                 int save_defense ()     //
                 {                       //
                     return defense;     //
                 }                       //
                 int save_stamina ()     //
                 {                       //
                     return stamina;     //
                 }                       //
                 int save_exp_point ()   //
                 {                       //
                     return exp_point;   //
                 }                       //
    /////////////                        //
    // save contence                     //
    ///////////////////////////////////////
                 
                 
                 void townchoice ()
                 {
                      cout << "1 town menu\n";
                      cout << "2 your statistics\n";
                      cout << "3 enter weapon........h\n";
                      cout << "4 enter workshop\n";
                      cout << "5 enter armor shop\n";
                      cout << "6 enter atletics arena\n";
                      cout << "7 enter dungeon\n";
                      cout << "10 quit game\n";
                 }
                 
                 void stats ()
                 {
                      cout << "your health is   " << health << endl;
                      cout << "your attack is   " << attack << endl;
                      cout << "your strength is " << strength << endl;
                      cout << "your defense is  " << defense << endl;
                      cout << "your stamina is  " << stamina << endl;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int increasehealth ()
                 {
                     if (exp_point == 0)
                     {
                               cout << "you have run out of exp points" << endl;
                               return health;
                     }
                     
                     else if (exp_point > 0)
                     health++;
                     exp_point--;
                     cout << "your health is now " << health << endl;
                     return health;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int increaseattack ()
                 {
                     if (exp_point == 0)
                     {
                               cout << "you have run out of exp points" << endl;
                               return attack;
                     }
                     
                     else if (exp_point > 0)
                     attack++;
                     exp_point--;
                     cout << "your attack is now " << attack << endl;
                     return attack;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int increasestrength ()
                 {
                     if (exp_point == 0)
                     {
                               cout << "you have run out of exp points" << endl;
                               return strength;
                     }
                     
                     else if (exp_point > 0)
                     strength++;
                     exp_point--;
                     cout << "your strength is now " << strength << endl;
                     return strength;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int increasedefense ()
                 {
                     if (exp_point == 0)
                     {
                               cout << "you have run out of exp points" << endl;
                               return defense;
                     }
                     
                     else if (exp_point > 0)
                     defense++;
                     exp_point--;
                     cout << "your defense is now " << defense << endl;
                     return defense;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int increasestamina ()
                 {
                     if (exp_point == 0)
                     {
                               cout << "you have run out of exp points" << endl;
                               return stamina;
                     }
                     
                     else if (exp_point > 0)
                     stamina++;
                     exp_point--;
                     cout << "your stamina is now " << stamina << endl;
                     return stamina;
                 }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////////////////////////
                 int health;
                 int attack;
                 int strength;
                 int defense;
                 int stamina;
                 int exp_point;
    };





    I hope I copied everything correctly


    problem is because it uses always all player variables as 10 as constructor says

    don't know why

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to tell us what you do (i.e. what functions you call) that lead to Something Bad happening.

  3. #3
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    Unhappy

    in

    void menugame2

    when I go to case 2 to "save" even if in game I change the variables the variables still stay same as constructor says

    I cannot change those variables even if I bring them back

    Code:
                         case 2:
                              health = game.savegame_health ();
                              attack = game.savegame_attack ();
                              strength = game.savegame_strength ();
                              defense = game.savegame_defense ();
                              stamina = game.savegame_stamina ();
                              exp_point = game.savegame_exp_point ();
                              
                              cout << "save contence " << health << endl;
                              cout << "save contence " << attack << endl;
                              cout << "save contence " << strength << endl;
                              cout << "save contence " << defense << endl;
                              cout << "save contence " << stamina << endl;
                              cout << "save contence " << exp_point << endl;
    here should be changed variables from "playing game"

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your save functions don't actually do anything. They just return the current value of each variable.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    yes but those values don't want to change

    well I'm more a noob for programing now

    I thought for save it would be to to return valuables that were changed by game



    how else could I do save option?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you saving to? You can't just name something 'save' and expect it to save something. You'll need to figure out what format you want, and write it out to a file (or whatever you plan on doing to actually 'save').


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    well I don't know C++ that much yet

    all I wanted to do is when program is running and when I ext the game and enter again variables wouldn't change from time when I exit the game

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 10:49 AM
  2. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM
  3. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 09:42 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Problem with a game im making
    By TheGr8one in forum Game Programming
    Replies: 2
    Last Post: 10-19-2001, 07:38 PM