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;
};
Code: Select all
#include <iostream>
#include "Character.h"
using namespace std;
int main()
{
Character Hero;
Hero.setXCoord(0);
Hero.setYCoord(0);
return 0;
}
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
}
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;
}
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;
}
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;
}