Text based adventure game.

This is a discussion on Text based adventure game. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm at the beginning stage of making a text based adventure game. But after making my player.h (which has ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    19

    Text based adventure game.

    Hi, I'm at the beginning stage of making a text based adventure game. But after making my player.h (which has health, name, and things like goNorth which I'm not even sure if it should be there) witch is here:
    Code:
    #pragma once
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class player
    {
    public:
    	char* playerName;
    	int health;
    	void goNorth();
    	void goEast();
    	void goSouth();
    	void goWest();
    	void attackMonster();
    	void getTreasure();
    }
    And then I have my cpp file and I don't know what to put here
    Code:
    void player::goNorth()
    {
    	//what needs to go here?
    }
    So I guess what I'm asking is, am I even on the right track trying to put this in my player class or is it better to have a separate movement class to deal with this? And kind of thing goes in the above scenario. And if anyone has a link to a good tutorial about creating text based games in C++?

    Thanks if you can help me

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    Though it's hard to be absolutely certain without knowing what else in your game needs to move, I think the best course of action is to put the movement in the player class.

    As for what needs to go into your cpp file, you need to know one vital bit of information first: What changes when the player 'goes north', or south, or whatever. Does the coordinates change, for example? Does the amount of total steps the character had taken thus far increase by one? Does the function randomly generate numbers for a chance encounter with a monster, or is there a map that it needs to refer to?

    Once you figure out what changes in the program whenever the player 'goes north', fix your function so that the things that need to be changed, are changed.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    19
    I think it would be best to have a map and then I can assign tasks/monsters to certain areas and then when I get that done I could try and mix it up a little bit. How would you go about making a map of sorts in a game like this though. Would it just be a big loop saying when the user presses go north then go east I'm in the top right part of the maze?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Maybe the player needs to have a present location:

    Code:
    class player
    {
        std::string name;
        int health;
        room *location;
    };
    The room itself has exits in the four basic directions:

    Code:
    class room
    {
        room *toTheEast;
        room *toTheNorth;
        room *toTheWest;
        room *toTheSouth;
    };
    Then your player movement code might look something like this:

    Code:
    void player::goNorth()
    {
        if (location->toTheNorth != NULL)
            location = location->toTheNorth;
        else
            text("You can't go that way.");
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    19
    Thanks for replying guys. brewbuck I'll check out your suggestion tomorrow. It's 2am here and I'm tired XD but with the:
    Code:
    void player::goNorth()
    {
        if (location->toTheNorth != NULL)
            location = location->toTheNorth;
        else
            text("You can't go that way.");
    }
    How would I make it say you're heading north. Instead of just going north, and saying you can't if the move isn't available.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    You could also make an array of rooms, with the player's coordinates being one room in the array.

    Code:
    int a =1,b=1;
    room [15] [15]; //a 15*15 array of rooms
    player.location = room [a] [b];
    void player::goNorth ()
    {
         if (++b<=15)
         {
            ++b
         }
    }
    or something like that?
    Last edited by SqueakyPebble; 01-05-2012 at 07:36 PM.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    Change it to:
    Code:
    void player::goNorth ()
    {
       if (location->toTheNorth != NULL)
       {
           location = location -> toTheNorth;
           text("You are now heading North"); //or whatever you want to say
       }
       else
           text("You can't go that way.");
    }

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You'll want to be able to put your map data into a file, so think about your data format. Consider this map:
    Code:
    ---------------------------
    |  i1       |         m1   |   1,2,3,4  room numbers
    |           |              |   P        player
    |     2            3       |   i1,i2,i3 items
    |           |              |   m1       monster
    -----   -----------   -----
    |           |              |
    |     1            4       |
    |  P        |              |
    |           |   i2      i3 |
    ---------------------------
    Here's a simple data format:
    Code:
    number_of_rooms
    room# north_room east_room south_room west_room item_list(ending with 0) description
    ...
    number_of_items
    item# description
    ...
    number_of_monsters
    monster# item_list(ending with 0) description
    ...
    player_starting_room  item_list(ending with 0)
    Example for map above:
    Code:
    4
    1  2 4 0 0  0      A dingy room.
    2  0 3 1 0  1 0    A dingier room.
    3  0 0 4 2  0      Another room.
    4  3 0 0 1  2 3 0  Yet another room.
    4
    1  A sword.
    2  A lantern.
    3  A particle accelerator.
    4  A huge mace.
    5  A penny.
    1
    1  4 0  A troll.
    1  5 0
    That's just an idea. You'd need to add more stuff.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    9
    Question:
    Quote Originally Posted by oogabooga View Post
    Code:
    4
    1  2 4 0 0  0      A dingy room.
    2  0 3 1 0  1 0    A dingier room.
    3  0 0 4 2  0      Another room.
    4  3 0 0 1  2 3 0  Yet another room.
    4
    1  A sword.
    2  A lantern.
    3  A particle accelerator.
    4  A huge mace.
    5  A penny.
    1
    1  4 0  A troll.
    1  5 0
    What do all those numbers in front of the words do? I haven't seen code like that before...

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by SqueakyPebble View Post
    Question:

    What do all those numbers in front of the words do? I haven't seen code like that before...
    That is not code...but data that describes who resides where ..on the map.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Here is a static example of what oogabooga talks about -> A begginer writing a text based dungeon game.

    But rather than encoding the rooms in the code itself, you read them from a file instead.
    So your data file begins with say "the number of rooms" (so you can call an appropriate malloc), then descriptions of each room (say the directions you can go in, and what's in the room).

    Start with a small static example, then add "load level" and "save level" functions - you'll see what the numbers mean.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    19
    Thanks for the replys lads. I'll try some of the suggestions now. Does anyone have any ideas about how I could give North(south, west etc) a set value and increase/decrease it's value every time it's used? Wouldn't it make it easier then?

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    you could initialise them all as 0 (being a central 'orientation') and decide on a constant that represents the world boundary distance for each direction, probably apply same constant max to each direction for simplicity. To track their absolute position i would give each player n, e, s, w vars in the player struct or class. each time a move is made in each direction increment or decrement by the unit distance value,

    so your const might be 100, giving a north south axis of -100 to 100
    In fact if you only ever picture things in orthogonal movement then you could just have a northsouth and an eastwest var for each player.

    check that world boundary is not exceeded, if so either disallow the move or set their value in that direction to the world edge, like they have hit a wall.



    But saying that you will need to be monitoring room dimensions and position within so a global 'overall' position compass might not be too useful for that, unless you compare player pos to wall positions of current room and monitor player movements accordingly.

    Don't know if this is any use, i have not written any kind of game like this, just some ideas based on the possible coding, rather than gaming knowledge

    This is nt the nicest switch in world, should be tighter i think but you might get some use from the idea in mind, though i am sure others here can provide improved ideas.

    I tried to use simple ideas, but you could be using absolute values for pos checking also.

    Code:
    const int PLUS_BOUNDS = 100;
    const int MINUS_BOUNDS = -100;
    const int STEP = 1;
    
    enum COMPASS
    {
    	NORTH,
    	SOUTH,
    	EAST,
    	WEST,
    };
    
    struct player 
    {
        // player attributes..
    	//...
    	
    	int ns; //init these to 0 on startup
    	int ew;
    	
    	//more attributes ...
    	
    };
    
    
    // i am thinking along these lines:
    
    player myPlayer;
    
    int move;
    
    move = GetMove();
    
    switch(move)
    {
    	case NORTH:
    	{
    		if(myPlayer.ns + STEP <= PLUS_BOUNDS)
    			myPlayer.ns += STEP;
    		else
    			myPlayer.ns = PLUS_BOUNDS
    	}
    	break;
    	case SOUTH:
    	{
    		if(myPlayer.ns - STEP >= MINUS_BOUNDS)
    			myPlayer.ns -= STEP;
    		else
    			myPlayer.ns = MINUS_BOUNDS
    	}
    	break;
    	case EAST:
    	{
    		if(myPlayer.ew + STEP <= PLUS_BOUNDS)
    			myPlayer.ew += STEP;
    		else
    			myPlayer.ew = PLUS_BOUNDS
    	}
    	break;
    	case WEST:
    	{
    		if(myPlayer.ew - STEP >= MINUS_BOUNDS)
    			myPlayer.ew -= STEP;
    		else
    			myPlayer.ew = MINUS_BOUNDS		
    	}
    	break;
    }
    Last edited by rogster001; 01-06-2012 at 06:35 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    19
    Again, thanks for the replys I'll try and implement it once I've figured this out. In my player.h file I have
    Code:
    int health;
    	int strength;
    	int agility;
    	Player();  
    	Player(int strength, int agility);  
    	Player * weapon;
    	Player * armour;
    and then my cpp has
    Code:
    Player::Player(int strength, int agility) 
    {
      strength = strength;
      agility = agility;
      health = 10;
    }
    Then in the main I made 4 HighElf Dwarf human and ork and have them being able to access everything in player. But how would I present a choice, as in when the game loads up you have 4 characters to choose from. All with different attributes?

  15. #15
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Instead of that 2D-grid approach, it's more usual in this case to use the graph approach hinted at by the data format I mentioned earlier. Then your code would be something like this.
    Code:
    enum Dir {
        NORTH, EAST, SOUTH, WEST,
        UP, DOWN,               // easily handles other directions
        NUMDIRECTIONS
    };
    
    
    class Item {
        string        m_descrip;
    public:
        void describe() { cout << descrip << endl; }
    };
    
    
    class Room {
        vector<Room*> m_exits;  // Vector of size Dir::NUMDIRECTIONS.
                                // m_exits[Dir::NORTH] is either NULL
                                // (no exit in that direction) or
                                // a ptr to the room to the north.
                                // Same idea for other directions.
        vector<Item*> m_items;  // items in room
        string        m_descrip;
    public:
        Room* getRoom(Dir dir) { return m_exits[dir]; }
        void describe() { cout << descrip << endl; }
    };
    
    
    class Player {
        Room*         m_location;
        vector<Item*> m_items;
        int           m_health;
    public:
        void move(Dir dir) {
            Room* room = m_location->getRoom(dir);
            if (!room)
                cout << "You can't go that way.\n";
            else
                m_location = room;
        }
    };

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-29-2008, 03:40 AM
  2. Text adventure game GUI
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 11-07-2007, 05:34 PM
  3. Help with my text adventure game.
    By Haggarduser in forum Game Programming
    Replies: 15
    Last Post: 10-26-2007, 01:53 AM
  4. text adventure game
    By linucksrox in forum Game Programming
    Replies: 18
    Last Post: 07-27-2006, 01:36 PM
  5. Please help (Text Adventure Game)
    By C++angel in forum C++ Programming
    Replies: 9
    Last Post: 02-23-2006, 03:33 PM

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