Jump to content
  • Advertisement
Sign in to follow this  

Help With A Basic C++ Game

This topic is 2473 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just getting into C++ programming. I decided to program a little game to get used to using if statements, variables, and writing in couts. It's a two choice text based game, where each choice leads you down a different path. This is the code for the first few choices:

#include <iostream>
using namespace std;
int main()
int startgame;

cout << "\n";
cin >> startgame;
if (startgame == 1)
int a;
cout << "You find yourself in a darkened room, lying on a bed.\n";
cout << "The sheets feel soft, and you can hear voices coming\n";
cout << "from down the hallway.";
cout << "\n";
cout << "Do you want to:\n";
cout << "1) Go back to sleep\n";
cout << "2) Get out of bed\n";
cin >> a;
if (a == 1)
{// 1) Go back to sleep
int b;
cout << "You awake in a small bed. The sheets feel rough, like sack cloth.\n";
cout << "You can make out the shape of a bare lightbulb hanging from the ceiling.\n";
cout << "After turning on the light, you notice a door in the corner of the dingy room.\n";
cout << "You also notice a bedside table with two drawers under a particularly large\n";
cout << "patch of the peeling wallpaper.\n";
cout << "Will you:\n";
cout << "1) Open the door\n";
cout << "2) Examine the table\n";
cin >> b;

if (b == 1)
{// 1) Open the door

cout << "Upon opening the door, you realize that the small room\n";
cout << "is suspended about 500 feet above a vast ocean, stretching\n";
cout << "as far as the eye can see. A scaffolding runs along the west side\n";
cout << "of the building. You cannot see where it leads.\n";
cout << "Will you:\n";
cout << "1) Jump into the ocean\n";
cout << "2) Walk along the scaffolding\n";
if (b == 2)
{// 2) Examine the table
cout << "You walk over to the table. It's top is empty.\n";
cout << "Pulling out the top drawer, you see a beautiful\n";
cout << "field of flowers on rolling green hills.\n";
cout << "You close the top drawer. Upon opening the second,\n";
cout << "You see a small village in the wintertime.\n";
cout << "Snow lines the streets, and smoke pours out of\n";
cout << "the chimneys of the small houses.\n";
cout << "The choice is yours:\n";
cout << "1) Top drawer\n";
cout << "2) Bottom drawer\n";
if (a == 2)
{// 2) Get out of bed

cout << "After you climb out of the bed, you hear the voices die down.\n";
cout << "You hear the steady sound of footsteps coming down the hallway.\n";
cout << "Looking around frantically, you notice a small closet in the corner\n";
cout << "across from the door. You also spot a metal cane leaning against the wall\n";
cout << "beside the bed.\n";
cout << "Do you:\n";
cout << "1) Hide in the closet\n";
cout << "2) Grab the cane and face the intruder\n";

This is how the game works. It's a bit convoluted, and will only become more so. I was wondering what simpler way there is of doing it, other than just using a-z for variables. Also, any tips that you have on organizing code to be more readable would be appreciated as well.

Share this post

Link to post
Share on other sites
One thing I would do is indent some of your if statements more to make things a little more readable. Having everything, including the nested ifs, indented the same amount makes things a little confusing. Here's an example of what I mean.


As for structure, there's a couple of things that might make it a little easier. Functions and/or switch statements might be things to look into.

Share this post

Link to post
Share on other sites
Consider this: Each "Room" in your game has the following:

1) a description
2) a set of choices taking you to other rooms

Thus we could do something like:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

struct Room {
std::string description;
std::vector<int> choices

void loadRoomsFromFile(std::vector<Room> &rooms, std::string filename) {
ifstream fs;
char buffer[1024];
buffer[0] = 0;
while (fs.eof() == false) {
Room newRoom;
while(buffer[0]!='#') {
buffer = fs.getline(buffer,1024);
std::string bufferString(buffer);
while(buffer[0]!='#') {
buffer = fs.getline(buffer,1024);

int main() {
int currentRoom = 0;
std::vector<Room> rooms;

loadRoomsFromFile(rooms, "rooms.txt");

while (currentRoom>=0) { //we can exit the game by selecting an option that sets current room to a negative value
std::cout << rooms[currentRoom].description;
int choice = 0;
std::cin >> choice;
if (rooms[currentRoom].choices.size()>=choice && choice>0) { //make sure the choice we entered is valid
currentRoom = rooms[currentRoom].choices[choice-1];

Then you can simply create a textfile with all rooms looking like this:


You find yourself in a darkened room, lying on a bed.
The sheets feel soft, and you can hear voices coming
from down the hallway.

Do you want to:
1) Go back to sleep
2) Get out of bed
//etc etc , first room is 0, second 1, and so on, the numbers after each description is the room option 1, 2, etc takes you to.

You can now add more than 2 paths from each room and go back and forth freely between them if you want, adding more rooms doesn't require any changes to the code. (You can also name the file something else such as rooms.dat if you prefer that)

//Note: I didn't test this code, it might contain errors.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!