Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualDaaark

Posted 10 October 2012 - 03:19 PM

Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now. I added space to define keys, but I do not check for them.

edit: Looks better on PasteBin: http://pastebin.com/cYh15CWk

// ExampleTextAdventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;
//This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
class CRoom
{
public:
string Name;
//a list of exit name strings, and key name strings
//if key string is empty, door is unlocked
string ExitNorth, KeyNorth;
string ExitEast, KeyEast;
string ExitSouth, KeySouth;
string ExitWest, KeyWest;
//Constrcutor, for easy room initalization!
CRoom(string name,
  string exitnorth, string keynorth,
  string exiteast, string keyeast,
  string exitsouth, string keysouth,
  string exitwest, string keywest)
{
  Name = name;
  ExitNorth = exitnorth; KeyNorth = keynorth;
  ExitEast = exiteast; KeyEast = keyeast;
  ExitSouth = exitsouth; KeySouth = keysouth;
  ExitWest = exitwest; KeyWest = keywest;

  return;
}
~CRoom()
{
  //nothing to do!
  //strings clean themselves up

  return;
}
};

//game class, just to keep player and room data organized
class CGame
{
public:
//an array of keys
vector <string> Key;

//an array of rooms
vector <CRoom> Room;
int CurrentRoom;
string Input;

CGame()
{

  //add 9 rooms using the constructor to fill them out quickly
  //123
  //456
  //789
  Room.push_back(CRoom("Room 1", "","",   "Room 2","", "Room 4","", "",""));
  Room.push_back(CRoom("Room 2", "","",   "Room 3","", "Room 5","", "Room 1",""));
  Room.push_back(CRoom("Room 3", "","",   "","",   "Room 6","", "Room 5",""));
  Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));
  Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));
  Room.push_back(CRoom("Room 6", "Room 3","", "","",   "Room 9","", "Room 5",""));
  Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","",   "",""));
  Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","",   "",""));
  Room.push_back(CRoom("Room 9", "Room 6","", "","",   "","",   "Room 8",""));

  //set the current room to room 5, right in the middle
  CurrentRoom = FindRoom("Room 5");
  return;
}

//a prompt function
bool Prompt()
{
  cout << "\n\n\n";
  cout << "You are in " + Room[CurrentRoom]. Name << endl;
  cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
  cout << "-> ";

  //read player input
  string Input;
  cin >> Input;
  int NewRoom = 0;
  switch (Input[0])
  {
   //if the user entered 'q', return false
  case 'Q':
  case 'q':
   return false;
   break;
   //user wants to go north
  case 'N':
  case 'n':
   //check to see if that room exists
   //if FindRoom found a legal room, assign it as the current room
   NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);  
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'E':
  case 'e':
   NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'S':
  case 's':
   NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'W':
  case 'w':
   NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
   //if the user enters an unrecognized command...
  default:
   cout << "Invalid command" << endl;
  }

  return true;
}


//Find a room index based on it's name
//returns an int that is the index of the room in the array
//or -1 if it's not found
int FindRoom(string RoomName)
{
  for (int i = 0; i < Room.size(); ++i)
  {
   //if the room name matches 'RoomName', return the room index
   if (Room[i].Name == RoomName)
   {
	return i;
   }
  }

  //if the code gets down here, the room with 'RoomName' wasn't found
  //so -1 is returned to indicate failure
  return -1;
}
};

int _tmain(int argc, _TCHAR* argv[])
{

//this will hold the input from the player
string Input;
//intialize a new instance of the game class
CGame Game;
while(1)
{
  //run the prompt, and break if the returns false;
  if (!Game.Prompt()) break;
}

cout << "Byeeeeeeeeee" << endl;
return 0;
}


edit: What is up with the code tags? turned my formatting into a disaster!

#5Daaark

Posted 10 October 2012 - 03:17 PM

Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now. I added space to define keys, but I do not check for them.

// ExampleTextAdventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;
//This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
class CRoom
{
public:
string Name;
//a list of exit name strings, and key name strings
//if key string is empty, door is unlocked
string ExitNorth, KeyNorth;
string ExitEast, KeyEast;
string ExitSouth, KeySouth;
string ExitWest, KeyWest;
//Constrcutor, for easy room initalization!
CRoom(string name,
  string exitnorth, string keynorth,
  string exiteast, string keyeast,
  string exitsouth, string keysouth,
  string exitwest, string keywest)
{
  Name = name;
  ExitNorth = exitnorth; KeyNorth = keynorth;
  ExitEast = exiteast; KeyEast = keyeast;
  ExitSouth = exitsouth; KeySouth = keysouth;
  ExitWest = exitwest; KeyWest = keywest;

  return;
}
~CRoom()
{
  //nothing to do!
  //strings clean themselves up

  return;
}
};

//game class, just to keep player and room data organized
class CGame
{
public:
//an array of keys
vector <string> Key;

//an array of rooms
vector <CRoom> Room;
int CurrentRoom;
string Input;

CGame()
{

  //add 9 rooms using the constructor to fill them out quickly
  //123
  //456
  //789
  Room.push_back(CRoom("Room 1", "","",   "Room 2","", "Room 4","", "",""));
  Room.push_back(CRoom("Room 2", "","",   "Room 3","", "Room 5","", "Room 1",""));
  Room.push_back(CRoom("Room 3", "","",   "","",   "Room 6","", "Room 5",""));
  Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));
  Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));
  Room.push_back(CRoom("Room 6", "Room 3","", "","",   "Room 9","", "Room 5",""));
  Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","",   "",""));
  Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","",   "",""));
  Room.push_back(CRoom("Room 9", "Room 6","", "","",   "","",   "Room 8",""));

  //set the current room to room 5, right in the middle
  CurrentRoom = FindRoom("Room 5");
  return;
}

//a prompt function
bool Prompt()
{
  cout << "\n\n\n";
  cout << "You are in " + Room[CurrentRoom]. Name << endl;
  cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
  cout << "-> ";

  //read player input
  string Input;
  cin >> Input;
  int NewRoom = 0;
  switch (Input[0])
  {
   //if the user entered 'q', return false
  case 'Q':
  case 'q':
   return false;
   break;
   //user wants to go north
  case 'N':
  case 'n':
   //check to see if that room exists
   //if FindRoom found a legal room, assign it as the current room
   NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);  
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'E':
  case 'e':
   NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'S':
  case 's':
   NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'W':
  case 'w':
   NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
   //if the user enters an unrecognized command...
  default:
   cout << "Invalid command" << endl;
  }

  return true;
}


//Find a room index based on it's name
//returns an int that is the index of the room in the array
//or -1 if it's not found
int FindRoom(string RoomName)
{
  for (int i = 0; i < Room.size(); ++i)
  {
   //if the room name matches 'RoomName', return the room index
   if (Room[i].Name == RoomName)
   {
	return i;
   }
  }

  //if the code gets down here, the room with 'RoomName' wasn't found
  //so -1 is returned to indicate failure
  return -1;
}
};

int _tmain(int argc, _TCHAR* argv[])
{

//this will hold the input from the player
string Input;
//intialize a new instance of the game class
CGame Game;
while(1)
{
  //run the prompt, and break if the returns false;
  if (!Game.Prompt()) break;
}

cout << "Byeeeeeeeeee" << endl;
return 0;
}


edit: What is up with the code tags? turned my formatting into a disaster!

#4Daaark

Posted 10 October 2012 - 03:17 PM

Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now. I added space to define keys, but I do not check for them.

// ExampleTextAdventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;
//This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
class CRoom
{
public:
string Name;
//a list of exit name strings, and key name strings
//if key string is empty, door is unlocked
string ExitNorth, KeyNorth;
string ExitEast, KeyEast;
string ExitSouth, KeySouth;
string ExitWest, KeyWest;
//Constrcutor, for easy room initalization!
CRoom(string name,
  string exitnorth, string keynorth,
  string exiteast, string keyeast,
  string exitsouth, string keysouth,
  string exitwest, string keywest)
{
  Name = name;
  ExitNorth = exitnorth; KeyNorth = keynorth;
  ExitEast = exiteast; KeyEast = keyeast;
  ExitSouth = exitsouth; KeySouth = keysouth;
  ExitWest = exitwest; KeyWest = keywest;
 
  return;
}
~CRoom()
{
  //nothing to do!
  //strings clean themselves up
 
  return;
}
};

//game class, just to keep player and room data organized
class CGame
{
public:
//an array of keys
vector <string> Key;

//an array of rooms
vector <CRoom> Room;
int CurrentRoom;
string Input;

CGame()
{
 
  //add 9 rooms using the constructor to fill them out quickly
  //123
  //456
  //789
  Room.push_back(CRoom("Room 1", "","",   "Room 2","", "Room 4","", "",""));
  Room.push_back(CRoom("Room 2", "","",   "Room 3","", "Room 5","", "Room 1",""));
  Room.push_back(CRoom("Room 3", "","",   "","",   "Room 6","", "Room 5",""));
  Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));
  Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));
  Room.push_back(CRoom("Room 6", "Room 3","", "","",   "Room 9","", "Room 5",""));
  Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","",   "",""));
  Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","",   "",""));
  Room.push_back(CRoom("Room 9", "Room 6","", "","",   "","",   "Room 8",""));
 
  //set the current room to room 5, right in the middle
  CurrentRoom = FindRoom("Room 5");
  return;
}

//a prompt function
bool Prompt()
{
  cout << "\n\n\n";
  cout << "You are in " + Room[CurrentRoom]. Name << endl;
  cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
  cout << "-> ";
 
  //read player input
  string Input;
  cin >> Input;
  int NewRoom = 0;
  switch (Input[0])
  {
   //if the user entered 'q', return false
  case 'Q':
  case 'q':
   return false;
   break;
   //user wants to go north
  case 'N':
  case 'n':
   //check to see if that room exists
   //if FindRoom found a legal room, assign it as the current room
   NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);  
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'E':
  case 'e':
   NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'S':
  case 's':
   NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'W':
  case 'w':
   NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
   //if the user enters an unrecognized command...
  default:
   cout << "Invalid command" << endl;
  }
 
  return true;
}


//Find a room index based on it's name
//returns an int that is the index of the room in the array
//or -1 if it's not found
int FindRoom(string RoomName)
{
  for (int i = 0; i < Room.size(); ++i)
  {
   //if the room name matches 'RoomName', return the room index
   if (Room[i].Name == RoomName)
   {
    return i;
   }
  }
 
  //if the code gets down here, the room with 'RoomName' wasn't found
  //so -1 is returned to indicate failure
  return -1;
}
};

int _tmain(int argc, _TCHAR* argv[])
{

//this will hold the input from the player
string Input;
//intialize a new instance of the game class
CGame Game;
while(1)
{
  //run the prompt, and break if the returns false;
  if (!Game.Prompt()) break;
}

cout << "Byeeeeeeeeee" << endl;
return 0;
}

#3Daaark

Posted 10 October 2012 - 03:16 PM

Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now.

[source lang=cpp]// ExampleTextAdventure.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <string>#include <vector>#include <iostream>using namespace std;//This class will be the data type that all rooms are based on. It will contain data that is general to all roomsclass CRoom{public:string Name;//a list of exit name strings, and key name strings//if key string is empty, door is unlockedstring ExitNorth, KeyNorth;string ExitEast, KeyEast;string ExitSouth, KeySouth;string ExitWest, KeyWest;//Constrcutor, for easy room initalization!CRoom(string name,  string exitnorth, string keynorth,  string exiteast, string keyeast,  string exitsouth, string keysouth,  string exitwest, string keywest){  Name = name;  ExitNorth = exitnorth; KeyNorth = keynorth;  ExitEast = exiteast; KeyEast = keyeast;  ExitSouth = exitsouth; KeySouth = keysouth;  ExitWest = exitwest; KeyWest = keywest;  return;}~CRoom(){  //nothing to do!  //strings clean themselves up  return;}};//game class, just to keep player and room data organizedclass CGame{public://an array of keysvector <string> Key;//an array of roomsvector <CRoom> Room;int CurrentRoom;string Input;CGame(){  //add 9 rooms using the constructor to fill them out quickly  //123  //456  //789  Room.push_back(CRoom("Room 1", "","",   "Room 2","", "Room 4","", "",""));  Room.push_back(CRoom("Room 2", "","",   "Room 3","", "Room 5","", "Room 1",""));  Room.push_back(CRoom("Room 3", "","",   "","",   "Room 6","", "Room 5",""));  Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));  Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));  Room.push_back(CRoom("Room 6", "Room 3","", "","",   "Room 9","", "Room 5",""));  Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","",   "",""));  Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","",   "",""));  Room.push_back(CRoom("Room 9", "Room 6","", "","",   "","",   "Room 8",""));  //set the current room to room 5, right in the middle  CurrentRoom = FindRoom("Room 5");  return;}//a prompt functionbool Prompt(){  cout << "\n\n\n";  cout << "You are in " + Room[CurrentRoom]. Name << endl;  cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;  cout << "-> ";  //read player input  string Input;  cin >> Input;  int NewRoom = 0;  switch (Input[0])  {   //if the user entered 'q', return false  case 'Q':  case 'q':   return false;   break;   //user wants to go north  case 'N':  case 'n':   //check to see if that room exists   //if FindRoom found a legal room, assign it as the current room   NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);     if (NewRoom > -1) CurrentRoom = NewRoom;   else cout << "You can't travel in that direction from here" << endl;   break;  case 'E':  case 'e':   NewRoom = FindRoom(Room[CurrentRoom].ExitEast);   if (NewRoom > -1) CurrentRoom = NewRoom;   else cout << "You can't travel in that direction from here" << endl;   break;  case 'S':  case 's':   NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);   if (NewRoom > -1) CurrentRoom = NewRoom;   else cout << "You can't travel in that direction from here" << endl;   break;  case 'W':  case 'w':   NewRoom = FindRoom(Room[CurrentRoom].ExitWest);   if (NewRoom > -1) CurrentRoom = NewRoom;   else cout << "You can't travel in that direction from here" << endl;   break;   //if the user enters an unrecognized command...  default:   cout << "Invalid command" << endl;  }  return true;}//Find a room index based on it's name//returns an int that is the index of the room in the array//or -1 if it's not foundint FindRoom(string RoomName){  for (int i = 0; i < Room.size(); ++i)  {   //if the room name matches 'RoomName', return the room index   if (Room[i].Name == RoomName)   { return i;   }  }  //if the code gets down here, the room with 'RoomName' wasn't found  //so -1 is returned to indicate failure  return -1;}};int _tmain(int argc, _TCHAR* argv[]){//this will hold the input from the playerstring Input;//intialize a new instance of the game classCGame Game;while(1){  //run the prompt, and break if the returns false;  if (!Game.Prompt()) break;}cout << "Byeeeeeeeeee" << endl;return 0;}[/source]

#2Daaark

Posted 10 October 2012 - 03:16 PM

Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now.

// ExampleTextAdventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;
//This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
class CRoom
{
public:
string Name;
//a list of exit name strings, and key name strings
//if key string is empty, door is unlocked
string ExitNorth, KeyNorth;
string ExitEast, KeyEast;
string ExitSouth, KeySouth;
string ExitWest, KeyWest;
//Constrcutor, for easy room initalization!
CRoom(string name,
  string exitnorth, string keynorth,
  string exiteast, string keyeast,
  string exitsouth, string keysouth,
  string exitwest, string keywest)
{
  Name = name;
  ExitNorth = exitnorth; KeyNorth = keynorth;
  ExitEast = exiteast; KeyEast = keyeast;
  ExitSouth = exitsouth; KeySouth = keysouth;
  ExitWest = exitwest; KeyWest = keywest;
 
  return;
}
~CRoom()
{
  //nothing to do!
  //strings clean themselves up
 
  return;
}
};

//game class, just to keep player and room data organized
class CGame
{
public:
//an array of keys
vector <string> Key;

//an array of rooms
vector <CRoom> Room;
int CurrentRoom;
string Input;

CGame()
{
 
  //add 9 rooms using the constructor to fill them out quickly
  //123
  //456
  //789
  Room.push_back(CRoom("Room 1", "","",   "Room 2","", "Room 4","", "",""));
  Room.push_back(CRoom("Room 2", "","",   "Room 3","", "Room 5","", "Room 1",""));
  Room.push_back(CRoom("Room 3", "","",   "","",   "Room 6","", "Room 5",""));
  Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));
  Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));
  Room.push_back(CRoom("Room 6", "Room 3","", "","",   "Room 9","", "Room 5",""));
  Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","",   "",""));
  Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","",   "",""));
  Room.push_back(CRoom("Room 9", "Room 6","", "","",   "","",   "Room 8",""));
 
  //set the current room to room 5, right in the middle
  CurrentRoom = FindRoom("Room 5");
  return;
}

//a prompt function
bool Prompt()
{
  cout << "\n\n\n";
  cout << "You are in " + Room[CurrentRoom]. Name << endl;
  cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
  cout << "-> ";
 
  //read player input
  string Input;
  cin >> Input;
  int NewRoom = 0;
  switch (Input[0])
  {
   //if the user entered 'q', return false
  case 'Q':
  case 'q':
   return false;
   break;
   //user wants to go north
  case 'N':
  case 'n':
   //check to see if that room exists
   //if FindRoom found a legal room, assign it as the current room
   NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);  
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'E':
  case 'e':
   NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'S':
  case 's':
   NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'W':
  case 'w':
   NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
   //if the user enters an unrecognized command...
  default:
   cout << "Invalid command" << endl;
  }
 
  return true;
}


//Find a room index based on it's name
//returns an int that is the index of the room in the array
//or -1 if it's not found
int FindRoom(string RoomName)
{
  for (int i = 0; i < Room.size(); ++i)
  {
   //if the room name matches 'RoomName', return the room index
   if (Room[i].Name == RoomName)
   {
    return i;
   }
  }
 
  //if the code gets down here, the room with 'RoomName' wasn't found
  //so -1 is returned to indicate failure
  return -1;
}
};

int _tmain(int argc, _TCHAR* argv[])
{

//this will hold the input from the player
string Input;
//intialize a new instance of the game class
CGame Game;
while(1)
{
  //run the prompt, and break if the returns false;
  if (!Game.Prompt()) break;
}

cout << "Byeeeeeeeeee" << endl;
return 0;
}

#1Daaark

Posted 10 October 2012 - 03:15 PM

Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now.

// ExampleTextAdventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;
//This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
class CRoom
{
public:
string Name;
//a list of exit name strings, and key name strings
//if key string is empty, door is unlocked
string ExitNorth, KeyNorth;
string ExitEast, KeyEast;
string ExitSouth, KeySouth;
string ExitWest, KeyWest;
//Constrcutor, for easy room initalization!
CRoom(string name,
  string exitnorth, string keynorth,
  string exiteast, string keyeast,
  string exitsouth, string keysouth,
  string exitwest, string keywest)
{
  Name = name;
  ExitNorth = exitnorth; KeyNorth = keynorth;
  ExitEast = exiteast; KeyEast = keyeast;
  ExitSouth = exitsouth; KeySouth = keysouth;
  ExitWest = exitwest; KeyWest = keywest;
 
  return;
}
~CRoom()
{
  //nothing to do!
  //strings clean themselves up
 
  return;
}
};

//game class, just to keep player and room data organized
class CGame
{
public:
//an array of keys
vector <string> Key;

//an array of rooms
vector <CRoom> Room;
int CurrentRoom;
string Input;

CGame()
{
 
  //add 9 rooms using the constructor to fill them out quickly
  //123
  //456
  //789
  Room.push_back(CRoom("Room 1", "","",   "Room 2","", "Room 4","", "",""));
  Room.push_back(CRoom("Room 2", "","",   "Room 3","", "Room 5","", "Room 1",""));
  Room.push_back(CRoom("Room 3", "","",   "","",   "Room 6","", "Room 5",""));
  Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));
  Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));
  Room.push_back(CRoom("Room 6", "Room 3","", "","",   "Room 9","", "Room 5",""));
  Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","",   "",""));
  Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","",   "",""));
  Room.push_back(CRoom("Room 9", "Room 6","", "","",   "","",   "Room 8",""));
 
  //set the current room to room 5, right in the middle
  CurrentRoom = FindRoom("Room 5");
  return;
}

//a prompt function
bool Prompt()
{
  cout << "\n\n\n";
  cout << "You are in " + Room[CurrentRoom]. Name << endl;
  cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
  cout << "-> ";
 
  //read player input
  string Input;
  cin >> Input;
  int NewRoom = 0;
  switch (Input[0])
  {
   //if the user entered 'q', return false
  case 'Q':
  case 'q':
   return false;
   break;
   //user wants to go north
  case 'N':
  case 'n':
   //check to see if that room exists
   //if FindRoom found a legal room, assign it as the current room
   NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);  
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'E':
  case 'e':
   NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'S':
  case 's':
   NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
  case 'W':
  case 'w':
   NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
   if (NewRoom > -1) CurrentRoom = NewRoom;
   else cout << "You can't travel in that direction from here" << endl;
   break;
   //if the user enters an unrecognized command...
  default:
   cout << "Invalid command" << endl;
  }
 
  return true;
}


//Find a room index based on it's name
//returns an int that is the index of the room in the array
//or -1 if it's not found
int FindRoom(string RoomName)
{
  for (int i = 0; i < Room.size(); ++i)
  {
   //if the room name matches 'RoomName', return the room index
   if (Room[i].Name == RoomName)
   {
    return i;
   }
  }
 
  //if the code gets down here, the room with 'RoomName' wasn't found
  //so -1 is returned to indicate failure
  return -1;
}
};

int _tmain(int argc, _TCHAR* argv[])
{

//this will hold the input from the player
string Input;
//intialize a new instance of the game class
CGame Game;
while(1)
{
  //run the prompt, and break if the returns false;
  if (!Game.Prompt()) break;
}

cout << "Byeeeeeeeeee" << endl;
return 0;
}

PARTNERS