The
Photographer
The
Techincal Artist
Text Based Game
//Main//
#include "RoomRead.h"
#include <fstream>
RoomRead::~RoomRead() {
mapRoom.clear();
}
void RoomRead::readQuestionsFromFile() {
ifstream file("map.txt"); //Checks for Map.txt
if (!file.is_open()) {
cout << "Error opening file" << endl;
return;
}
if (!file.good()) {
cout << "not good file" << endl;
return;
}
while (!file.eof()) { //Converts the Data from the file to the AdventureRoom Class
map<string, int> answerTable;
map<string,string> StringanswerTable;
vector<string> RoomDescription;
AdventureRoom *q = new AdventureRoom();
string strNumQuestion;
int numQuestion;
getline(file, strNumQuestion); // read line with number
numQuestion = atoi(strNumQuestion.c_str()); // convert string to int
q->setRoomNumber(numQuestion);
// read question
while (true) { //Parse the description
string line;
getline(file, line);
if (line == "------")
break;
else {
RoomDescription.push_back(line);
}
}
q->setRoomText(RoomDescription);
// load the map
while (true) { //Parse the answers
string lineQ;
getline(file, lineQ);
if (lineQ.length() == 0)
break;
else {
size_t sep = lineQ.find(" ");
string key = lineQ.substr(0, sep);
int value = atoi((lineQ.substr(sep + 1)).c_str());
answerTable[key] = value;
q->setAnswerTable(answerTable);
}
}
mapRoom[numQuestion] = q;
} //close EOF
}
void RoomRead::run() {
cout << "===============================================================================================" << endl;
cout << "= WELCOME TO DEATH VALLEY =" << endl;
cout << "===============================================================================================" << endl;
cout << "" << endl;
bool end = false;
int numQuestion = 1;
while (!end) {
AdventureRoom *q;
vector<string> RoomDescription;
q = mapRoom[numQuestion];
bool isGoodAnswer = false;
bool ForceRoom = false;
RoomDescription = q->getRoomText();
for (size_t i = 0; i < RoomDescription.size(); i++) {
cout << RoomDescription.at(i) << endl;;
}
string answer;
if (ForceRoom = true) {
answer = "forced";
}
else {
cout << "" << endl;
cout << "Your Choice: ";
cout << "" << endl;
getline(cin, answer);
}
// search which is the next question
while (!isGoodAnswer) {
numQuestion = q->lookupAnswer(answer);
if (numQuestion == -1 ) {
cout << "Are you even trying to go the right way? That is the wrong input try again." << endl;
isGoodAnswer = false;
cout << "Your Choice: ";
getline(cin, answer);
cout << "" << endl;
}
else if (numQuestion == 99) //Quit Program
{
exit(0);
}
else if (numQuestion == 6) { //Forced to go into next room
ForceRoom = true;
isGoodAnswer = true;
}
else if (numQuestion == 11) { //Help Option Room 1
cout << "The Available Options:" << endl;
cout << "Directions Available: North, Stay" << endl;
cout << "Other Commands: Help, Quit," << endl;
isGoodAnswer = false;
cout << "" << endl;
cout << "Your Choice: ";
getline(cin, answer);
cout << "" << endl;
}
else if (numQuestion == 12) { //Help Option Room 2
cout << "The Available Options:" << endl;
cout << "Directions Available: North, East, South, West" << endl;
cout << "Other Commands: Help, Quit," << endl;
isGoodAnswer = false;
getline(cin, answer);
}
else if (numQuestion == 13) { //Help Option Room 3
cout << "The Available Options:" << endl;
cout << "Directions Available: South" << endl;
cout << "Other Commands: Help, Quit," << endl;
isGoodAnswer = false;
getline(cin, answer);
}
else if (numQuestion == 15) { //Help Option Room 5
cout << "The Available Options:" << endl;
cout << "Directions Available: North" << endl;
cout << "Other Commands: Help, Quit," << endl;
isGoodAnswer = false;
cout << "Your Choice: ";
getline(cin, answer);
cout << "" << endl;
}
else if (numQuestion == 17) { //Help Option Room 7
cout << "The Available Options:" << endl;
cout << "Directions Available: North, Stay" << endl;
cout << "Other Commands: Help, Quit," << endl;
isGoodAnswer = false;
cout << "Your Choice: ";
getline(cin, answer);
cout << "" << endl;
}
else if (numQuestion == 98) { //Room 1 Death
cout << "Well, I guess you just rot here then." << endl;
cout << "==============================================" << endl;
cout << "= GAME OVER =" << endl;
cout << "==============================================" << endl;
end = true;
isGoodAnswer = true;
}
else if (numQuestion == 4) { //Finish Room
cout << "You have escaped Death Valley" << endl;
cout << "==============================================" << endl;
cout << "= YOU WON =" << endl;
cout << "==============================================" << endl;
end = true;
isGoodAnswer = true;
}
else
isGoodAnswer = true;
}
}
}
//CLASS//
#ifndef PLAYER_H
#define PLAYER_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <ctype.h>
using namespace std;
class Player {
private:
string Name;
int Inventory;
int NoMovements;
public:
// constructor
Player() {}
// set name
void setName(string PlayerName) {
this->Name = PlayerName;
}
string getName() {
Name = Name;
}
void setNoMovements(int NumberMovements) {
this->NoMovements = NumberMovements;
}
int getNoMovements() {
return NoMovements;
}
};
#endif