Hello everyone, this is my first time posting on these forums so if I am in the wrong place, please let me know.
Basically, I am trying to learn c++ and since I love games, i figured i would start learning by making a small text game.
I looked through a few tutorials and went to work. I am new at this so please be constructive and let me know where I can improve on any of my code. My first question is, Would it be better to have the game map in an array or keep it with switch statements like I have now? If so, would someone be willing to show me an example?
I thank you guys for your time and help.
:)
apparently my code is too long to put in here, any idea how to let you guys see it to get some advice?
You should really look into using OOP for this. Everything with "player" in front of it should basically be a member of a class called Player or something.
// p02 - North Road 1.
void pirithaNorthRoad1Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " North Road 1:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t North Raod 2 (City of Piritha)\n";
cout << " (S)outh --> \t Center of Town (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaNorthRoad2Func();
break;
case'S':
pirithaCOTFunc();
break;
case'E':
pirithaInnFunc();
break;
case'W':
pirithaWeaponShopFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaNorthRoad1Func();
break;
}
return;
}
// p03 - North Road 2.
void pirithaNorthRoad2Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " North Road 2:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t North Road 3 (City of Piritha)\n";
cout << " (S)outh --> \t North Road 2 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaNorthRoad3Func();
break;
case'S':
pirithaNorthRoad1Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaNorthRoad2Func();
break;
}
return;
}
// p04 - North Road 3.
void pirithaNorthRoad3Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " North Road 3:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t North Road 4 (City of Piritha)\n";
cout << " (S)outh --> \t North Road 2 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaNorthRoad4Func();
break;
case'S':
pirithaNorthRoad2Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaNorthRoad3Func();
break;
}
return;
}
// p05 - North Road 4.
void pirithaNorthRoad4Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " North Road 4:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t North Gate (City of Piritha)\n";
cout << " (S)outh --> \t North Road 3 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaNorthGateFunc();
break;
case'S':
pirithaNorthRoad3Func();
break;
case'E':
pirithaThiefGuildFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaNorthRoad4Func();
break;
}
return;
}
// p06 - North Gate.
void pirithaNorthGateFunc()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " North Gate:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Standing between you and the gates is a heavily armored City Guard.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t North Forest 1 (North Woods)\n";
cout << " (S)outh --> \t North Road 3 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
cout << "\n\n";
cout << "The City Guard prevents you from leaving and says - The gates are closed for the evening. Come back in the morning.\n\n";
pirithaNorthGateFunc();
break;
case'S':
pirithaNorthRoad4Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaNorthGateFunc();
break;
}
return;
}
// p07 - South Road 1.
void pirithaSouthRoad1Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " South Road 1:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t Center of Town (City of Piritha)\n";
cout << " (S)outh --> \t South Road 2 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaCOTFunc();
break;
case'S':
pirithaSouthRoad2Func();
break;
case'E':
pirithaItemShopFunc();
break;
case'W':
pirithaArmorShopFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaSouthRoad1Func();
break;
}
return;
}
// p08 - South Road 2.
void pirithaSouthRoad2Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " South Road 2:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t South Road 1 (City of Piritha)\n";
cout << " (S)outh --> \t South Road 3 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaSouthRoad1Func();
break;
case'S':
pirithaSouthRoad3Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaSouthRoad2Func();
break;
}
return;
}
// p09 - South Road 3.
void pirithaSouthRoad3Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " South Road 3:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t South Road 2 (City of Piritha)\n";
cout << " (S)outh --> \t South Road 4 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaSouthRoad2Func();
break;
case'S':
pirithaSouthRoad4Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaSouthRoad3Func();
break;
}
return;
}
// p10 - South Road 4.
void pirithaSouthRoad4Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " South Road 4:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t South Road 3 (City of Piritha)\n";
cout << " (S)outh --> \t South Gate (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaSouthRoad3Func();
break;
case'S':
pirithaSouthGateFunc();
break;
case'W':
pirithaFighterGuildFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaSouthRoad4Func();
break;
}
return;
}
// p11 - South Gate.
void pirithaSouthGateFunc()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " South Gate:\n";
cout << " ---------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " east and west, and the road to the south and north.\n\n";
cout << " Obvious exits are:\n";
cout << " (N)orth --> \t South Road 4 (City of Piritha)\n";
cout << " (S)outh --> \t South Woods (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'N':
pirithaSouthRoad4Func();
break;
case'S':
cout << "\n\n";
cout << "The City Guard prevents you from leaving and says - The gates are closed for the evening. Come back in the morning.\n\n";
pirithaSouthGateFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaSouthGateFunc();
break;
}
return;
}
// p12 - East Road 1.
void pirithaEastRoad1Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " East Road 1:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t East Road 2 (City of Piritha)\n";
cout << " (W)est --> \t Center of Town (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
pirithaEastRoad2Func();
break;
case'W':
pirithaCOTFunc();
break;
case'N':
pirithaInnFunc();
break;
case'S':
pirithaItemShopFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaEastRoad1Func();
break;
}
return;
}
// p13 - East Road 2.
void pirithaEastRoad2Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " East Road 2:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t East Road 3 (City of Piritha)\n";
cout << " (W)est --> \t East Road 1 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'W':
pirithaEastRoad3Func();
break;
case'E':
pirithaEastRoad1Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaEastRoad2Func();
break;
}
return;
}
// p14 - East Road 3.
void pirithaEastRoad3Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " East Road 3:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t East Road 3 (City of Piritha)\n";
cout << " (W)est --> \t East Road 1 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
pirithaEastRoad4Func();
break;
case'W':
pirithaEastRoad2Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaEastRoad3Func();
break;
}
return;
}
// p15 - East Road 4.
void pirithaEastRoad4Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " East Road 4:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t East Road 3 (City of Piritha)\n";
cout << " (W)est --> \t East Road 1 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E' | 'e':
pirithaEastGateFunc();
break;
case'W':
pirithaEastRoad3Func();
break;
case'N':
pirithaClericGuildFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaEastRoad4Func();
break;
}
return;
}
// p16 - East Gate.
void pirithaEastGateFunc()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " East Gate:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t East Woods (City of Piritha)\n";
cout << " (W)est --> \t East Road 1 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
cout << "\n\n";
cout << "The City Guard prevents you from leaving and says - The gates are closed for the evening. Come back in the morning.\n\n";
pirithaEastGateFunc();
break;
case'W':
pirithaEastRoad4Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaEastGateFunc();
break;
}
return;
}
// p17 - West Road 1.
void pirithaWestRoad1Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " West Road 1:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t Center of Town (City of Piritha)\n";
cout << " (W)est --> \t West Road 2 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
pirithaCOTFunc();
break;
case'W':
pirithaWestRoad2Func();
break;
case'N':
pirithaWeaponShopFunc();
break;
case'S':
pirithaArmorShopFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaWestRoad1Func();
break;
}
return;
}
// p18 - West Road 2.
void pirithaWestRoad2Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " West Road 2:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t West Road 1 (City of Piritha)\n";
cout << " (W)est --> \t West Road 3 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
pirithaWestRoad1Func();
break;
case'W':
pirithaWestRoad3Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaEastRoad2Func();
break;
}
return;
}
// p19 - West Road 3.
void pirithaWestRoad3Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " West Road 3:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t West Road 2 (City of Piritha)\n";
cout << " (W)est --> \t West Road 4 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
pirithaWestRoad2Func();
break;
case'W':
pirithaWestRoad4Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaWestRoad3Func();
break;
}
return;
}
// p20 - West Road 4.
void pirithaWestRoad4Func()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " West Road 4:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t East Road 3 (City of Piritha)\n";
cout << " (W)est --> \t East Road 1 (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'E':
pirithaWestRoad3Func();
break;
case'W':
pirithaWestGateFunc();
break;
case'N':
pirithaMageGuildFunc();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaWestRoad4Func();
break;
}
return;
}
// p21 - West Gate.
void pirithaWestGateFunc()
{
system ("cls");
cout << "\n";
cout << " ---------------\n";
cout << " West Gate:\n";
cout << " ----------------\n\n";
cout << " This is a smoothly paved and well traveled road within the city \n";
cout << " walls of Pirithia. The street is lined with lamps, that light up\n";
cout << " the streets on even the darkest of nights. There are houses to the\n";
cout << " north and south, and the road to the east and west.\n\n";
cout << " Obvious exits are:\n";
cout << " (E)ast --> \t West Road 4 (City of Piritha)\n";
cout << " (W)est --> \t West Woods (City of Piritha)\n\n";
cout << " Direction of travel: ";
cin >> Selection;
switch (Selection)
{
case'W':
cout << "\n\n";
cout << "The City Guard prevents you from leaving and says - The gates are closed for the evening. Come back in the morning.\n\n";
pirithaWestGateFunc();
break;
case'E':
pirithaWestRoad4Func();
break;
default:
cout << "\n\n";
cout << "You can not go that way!\n";
pirithaWestGateFunc();
break;
}
return;
}
Hey, sorry I just saw your post about using OOP. Like i said before, i saw a few tutorials and just figured I would give it a go at learning the language this way so im really new at this. How would you do what you suggested, sir?
and thank you guys very much for humoring me with this and helping me learn a bit more.
What you seem to be struggling with (which is expected of a beginner) is the concept of hardcoded vs a Data driven solution. This is where OOP comes in.
Long answer: Hardcoded - One way of describing hardcodedness would be to say that code is hardcoded when it is written in such a way that it clearly is not taking advantage of classes and algorithms to do most of the work for you (when it's appropriate). This results in code which is not modular or expandable. Sometimes, it's perfectly reasonable to have a hardcoded look-up table or some other such thing, it just comes down to whether or not you know what you're doing.
For instance, you've written all these different functions, each of them dedicated to one location in your game world. Additionally, you haven't wrapped any of your player, mob, armor / weapon etc variables in any classes, which, like I said above, makes it unnecessarly difficult to modify your code at a later time. What if you decided to make this game turn based, with possibly two or more human players?
Short answer :
Read up on classes and other OOP concepts.
Hello and thank you for your suggestion. I am reading up on classes now, but I am still failing to see what I gain from then specifically. So, if I made a 'player' class, and added into this class all the player variables, character creations, stats, and future battle system to it, that would make things easier for me in the future?
Also, back to the original issue, is the way I have the map set up, a good way to do it, or should I do the map a different way?
Lets look at things. Say you have a game server. It can hold an infinite number of players. Without using classes/structs, you'd have to hard code each individual value into your program (impossible)
Or, you could have classes that can make objects.
1 2 3 4 5
std::vector<Player> players; // A vector is basically a resizable array
Player tempPlayer("parameter");
players.push_back(tempPlayer); // Add it
Something like this would greatly reduce the size of your program. Even if it was a set number of players, you wouldn't want to hardcode each variable in (Especially in graphical apps)
(It's something you get using into larger projects)