c++ classes newbie

This is a discussion on c++ classes newbie within the C++ Programming forums, part of the General Programming Boards category; i'm trying to make a simple c++ rpg to learn about classes. first thing im doing getting it to walk ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    c++ classes newbie

    i'm trying to make a simple c++ rpg to learn about classes. first thing im doing getting it to walk around a small town. when i enter n, s, e, or w it wont move me.

    the map i walk around in is a char called seasidetown_map.
    my position on that map is saved with int pos1 and pos2 in the class i made
    its changed with the functions inside the class i made to store the player.

    how bad did i screw up guys

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    void main_menu();
    void new_game();
    void game_run();
    
    char seasidetown_map[100][100]= {"                       x               ",
                                     " i--c-------c----------c----------c    ",
                                     "    |       |          |          |    ",
                                     "    |       |          |          |    ",
                                     "    |       |          |          |    ",
                                     "    |       |          |          |    ",
                                     "    |       |          |          |    ",
                                     "    |       |          w          |    ",
                                     "    |       |                     |    ",
                                     "s---c-------c-----c---------------c    ",
                                     "    |       |     |               |    ",
                                     "    |       |     |               |    ",
                                     "    |       c-----c---------------c    ",
                                     "    |             |                    ",
                                     "    |             |                    ",
                                     "    t             |                    ",
                                     "                  x                    "};
                            
    
    class cplayer
    {
          public:
                 string name;
                 int mega_location;
                 int pos1, pos2;
                 int health, max_health;
                 int mana, max_mana;
                 int gold;
                 int experience;
          void pos1plus()
          {
               pos1++;
          }
          void pos1minus()
          {
               pos1--;
          }
          void pos2plus()
          {
               pos2++;
          }
          void pos2minus()
          {
               pos2--;
          }     
    } hero;
    
    int main()
    {
        main_menu();
        return 0;
    }
    
    void main_menu()
    {
         cout << "menu\n\n"
              << "\t1) new game\n"
              << "\t2) quit\n\n"
              << ">>";
              
         string input;
         getline(cin, input);
         
         if(input == "1")
         {
                  new_game();
         }
         
         if(input == "2")
         {
                  exit(1);
         }
    }
    
    void new_game()
    {
         cout << "\nname your character\n"
              << ">>";
              
         string input;
         getline(cin, input);
         hero.name = input;
         hero.health = 25;
         hero.max_health = 25;
         hero.mana = 10;
         hero.max_mana = 10;
         hero.mega_location = 0;
         hero.gold = 100;
         hero.pos1 = 5;
         hero.pos2 = 23;
         
         game_run();
    }
    
    void game_run()
    {
         jump_back:
                   
         if(hero.mega_location == 0)
         {
                               cout << "Seaside Town\n"
                                    << "------------\n";
         }
         
         int flag_n, flag_e, flag_s, flag_w;
         flag_n = 0;
         flag_e = 0;
         flag_s = 0;
         flag_w = 0;
         
         cout << "possible exits: \n";
         if(seasidetown_map[hero.pos1+1][hero.pos2] == '|' || seasidetown_map[hero.pos1+1][hero.pos2] == 'c')
         {
                      flag_n = 1;
                      cout << "north ";
         }
         if(seasidetown_map[hero.pos1-1][hero.pos2] == '|' || seasidetown_map[hero.pos1+1][hero.pos2] ==  'c')
         {
                      flag_s = 1;
                      cout << "south ";
         }
         if(seasidetown_map[hero.pos1][hero.pos2+1] == '-' || seasidetown_map[hero.pos1+1][hero.pos2] ==  'c')
         {
                      flag_e = 1;
                      cout << "east ";
         }
         if(seasidetown_map[hero.pos1][hero.pos2-1] == '-' || seasidetown_map[hero.pos1+1][hero.pos2] ==  'c')
         {
                      flag_w = 1;
                      cout << "west ";
         }
         
         cout << "\n------------\n"
              << "\n  HP      MP\n"
              << hero.health << "/" << hero.max_health
              << "   " << hero.mana << "/" << hero.max_mana
              << "\n>>";
         
         string input;
         getline(cin, input);
         
         if(input == "n" || flag_n == 1)
         {
                  hero.pos1plus();
         }
         if(input == "e" || flag_e == 1)
         {
                  hero.pos2plus();
         }
         if(input == "s" || flag_s == 1)
         {
                  hero.pos1minus();
         }
         if(input == "w" || flag_w == 1)
         {
                  hero.pos2minus();
         }
         goto jump_back;
    }

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Function:
    Code:
    void new_game()
    {
         cout << "\nname your character\n"
              << ">>";
              
         string input;
         getline(cin, input);
         hero.name = input;
         hero.health = 25;
         hero.max_health = 25;
         hero.mana = 10;
         hero.max_mana = 10;
         hero.mega_location = 0;
         hero.gold = 100;
         hero.pos1 = 5;
         hero.pos2 = 23;
         
         game_run();
    }
    Is going to change the members of the class that should be private. You've made them public and changing them.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Your class, as it is now, looks like a convenient place to store variables.

    Would a player let you change its identity so easily? Imagine your class as an object (tangible or not) that provides an interface.

    Right now, namespace hero would do the trick. Instead, make your CPlayer class such that, upon its creation, it is actually a Player -- even if it's just a zombie player waiting to be told what it is.

    For example:
    Code:
    class cPlayer
    {
          public:
                 cPlayer(const string & name_in, int starting_gold, /*... etc..... */);  //use initializers
                 cPlayer();   //constructs an empty player? No name or anything? Maybe.
                 //Thanks to std::string, you don't need an explicit destructor
                 
                 enum direction { null_direction = 0, north, south, east, west };
                 void move(direction dir, unsigned int spaces = 1);   //consolidate
                 
                 //Setters and getters might work nicely.
                 //This way, cPlayer can check to see whether a change
                 //is appropriate, and deny it if it isn't.
                 const string & name() const { return n; }
                 void change_name(const string & s) { n = s; }
    
                 // . . . . . . . . . . . .
                  
          private:
                 string n;
                 int mega_loc;
                 int p1, p2;
                 int h, max_h;
                 int m, max_m;
                 unsigned int g;   //I assume this Player won't go into debt.
                 int exp;
          
    };
    Oh, and try not to use a global cPlayer hero. Instantiate your cPlayer in an appropriate scope (*cough* new_game) and then pass it around by reference.
    Last edited by CodeMonkey; 08-08-2007 at 10:27 PM. Reason: diction
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with winAPI in classes (newbie question)
    By Spyril in forum Windows Programming
    Replies: 3
    Last Post: 11-09-2007, 02:21 PM
  2. On declaring classes. (Newbie)
    By suzakugaiden in forum C++ Programming
    Replies: 26
    Last Post: 01-31-2005, 05:15 PM
  3. 2 classes merge into 1? (newbie)
    By xion in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2004, 05:20 PM
  4. the holy grail for newbie programmers...
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2003, 07:19 PM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21