Page 1 of 1

A simple way to make a world map in the console

Posted: Fri Jul 30, 2010 11:15 pm
by BlitzCoder
I have decided to put up my way (one of) of how to make a game map/world for a text rpg in the console. If you have other ways to do this, please share as well.

After reading posts on various forums I have seen one common question is "How do I make the game map". One way to do this in the console is to use a double nested for loop to draw a x*x grid and then place objects represented by text onto said grid and then have it do when you make a move it redraws the grid/map with the updated values. Blarg.

This is a much simpler and to the point way.

My example will use a coordinate systems to represent a game world and place events/objects/battles/etc at various X/Y coordinate values.

First with classes:

Lets say you have your Character class. In your main() you have made a new Character called Hero. Your Character class may look like:

Code: Select all

class Character{

public:

    int getXCoord();
    void setXCoord(int xCoord);

    int getYCoord();
    void setYCoord(int yCoord);

private:
    int itsXCoord;
    int itsYCoord;

};

then in your main() you would put

Code: Select all

#include <iostream>
#include "Character.h"
using namespace std;

int main()
{
Character Hero;
Hero.setXCoord(0);
Hero.setYCoord(0);

return 0;
}
Ok, so now we have a Hero, and its X/Y values are 0,0. The point of this is so you can now create a move function or loop that will then change either the x or y Coord value +/-. With this system it would be something like:

You find yourself in a forest. You can move North/South/West/East. The player then picks one of the choices given and the program changes the values of X/Y. Say the player decides to move north. Your program will add 1 to the X value. Now you are at 1.0. How is this useful? It is useful because it allows you to make a virtually infinite sized world and set conditional events at certain X/Y locations. For example: If a player moves to 3/-2 on your world, then perhaps a battle happens:

Code: Select all

if (Hero.getXCoord() == 3 && Hero.getYCoord() == -2){
orcAmbush(); // don;t bother trying to compile this, orcAmbush has not been declared, used as an example
}
Now lets say we want to move around. You have told your player they are in *insert background story* and you cout the current location (0,0). You then want the player to be able to move around your world. Two ways to do this are by using either a playerMove(); function or a loop/nested if series. I will used the latter since using a playerMover function outside of the main scope requires pointers and ...no.

Code: Select all

#include <iostream>
#include "Character.h"
using namespace std;

int main()
{
int move;
Character Hero;
Hero.setXCoord(0);
Hero.setYCoord(0);
cout << "You are at :" << Hero.getXCoord() << ", " << Hero.getYCoord() << endl;
cout << "You find yourself in a clearing. Make a move\n";
cout << "1)North 2)South 3)East 4)West: ";
cin >> move;
if (move == 1){
    Hero.setXCoord(Hero.getXCoord()+1);
}
if (move == 2){
    Hero.setXCoord(Hero.getXCoord()-1);
}
if (move == 3){
    Hero.setYCoord(Hero.getYCoord()-1);
}
if (move == 4){
    Hero.setYCoord(Hero.getYCoord()+1);
}

cout << "Currently you are at: " << Hero.getXCoord() << ", " << Hero.getYCoord();

return 0;
}
Of course this only allows one move, but you can use this type of setup in a loop or a function as well.

Now, to do this all without classes, which is much simpler but much more restrictive on what your player can do (since you lose the class):

Code: Select all

#include <iostream>
using namespace std;

int main()
{
int move;
int xCoord = 0;
int yCoord = 0;

cout << "You are at :" xCoord << yCoord <, endl;
cout << "You find yourself in a clearing. Make a move\n";
cout << "1)North 2)South 3)East 4)West: ";
cin >> move;
if (move == 1){
    xCoord++;
    }
    
/*repeat for rest of choices*/

return 0;
}
This method is much shorter but is restrictive since you can't really do much if you want your x/y to be assigned to a player class. I wanted to show both methods though.

To be honest in my unprofessional opinion the best way to do this type of map/grid/world is to use the class and have a movePlayer() function.

ps: the .cpp is:

Code: Select all

#include "Character.h"

    int Character::getXCoord(){
    return itsXCoord;
    }

    int Character::setXCoord(int xCoord){
    itsXCoord = xCoord;
    }

    int Character::getYCoord(){
        return itsYCoord;
    }

    int Character::setYCoord(int yCoord){
        itsYCoord = yCoord;
    }