// 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;
}