1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
//file libraries
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
#define GAMEFILE "world.txt"//read in for the world data
#define CONTINUE 1
#define QUIT 0
//user libraries
struct tArea{//structure that holds the info for the main area
string strCurrentArea;//name of current area
string strDescription;//description of current area
string strNorth;//name of area to the north
string strSouth;//name of area to the south
string strEast;//name of area to the east
string strWest;//name of area to the west
};
//global constants
//function prototypes
void DisplayArea(tArea &area);
void GetAreaInfo(ifstream&,tArea &area);
void Move(ifstream&,tArea &area,string);
int Input(ifstream&,tArea &area);
//execution
int main(){
ifstream fin;//pointer that opens and reads from file
tArea area;//area structure data
fin.open(GAMEFILE);//
if(fin.fail()){
cout<<"UNABLE TO FIND GAME FILE\n";
return -1;
}
fin>>area.strCurrentArea>>area.strCurrentArea;//reads in the start point for the game
GetAreaInfo(fin,area);//calls function to receive info from the file
DisplayArea(area);//calls function to display the area to the user
while(1){//main game loop
if(Input(fin,area)==QUIT){//if user enters quit, game ends
break;
}
}
fin.close();//closes file
Sleep(1000);//1 second delay before ending
return 0;
}
/**********************************************************/
//*****************DisplayArea*****************************/
//******Function is called to display area description*****/
/**********************************************************/
void DisplayArea(tArea &area)
{
cout<<area.strDescription<<endl<<endl;
}
/**********************************************************/
//*****************GetAreaInfo*****************************/
//******Function is called to read in the area*************/
//************information from the file********************/
/**********************************************************/
void GetAreaInfo(ifstream &fin,tArea &area){
string strTemp="";
string strTemp2="";//temporary for reading in info
string strArea="<"+area.strCurrentArea+">";//looks for the room name in brackets to find easier i.e instead of main it reads in <main>
fin.seekg(NULL,ios::beg);//starts the header search from the beginning of the file
fin.clear();//allows the file to be read through multiple times
while(getline(fin, strTemp, '\n')){//while loop reads file til it finds the correct area heading
if(strTemp==strArea){
getline(fin, area.strDescription, '*');//if it finds the correct area heading, it reads its info
// Read past the direction blocks (I.E. <north>) and store the room name for that direction
fin>>strTemp>>area.strNorth;
fin>>strTemp>>area.strEast;
fin>>strTemp>>area.strSouth;
fin>>strTemp>>area.strWest;//it then read in the surrounding area titles by skipping their <area> descriptors
return;
}
}
}
/**********************************************************/
//*********************Move********************************/
//******Function is called to move through the*************/
//****game if there is an area in that direction***********/
/**********************************************************/
void Move(ifstream &fin,tArea &area, string strArea){
if(strArea=="None"){//detects if there is no area in the direction inputted
cout<<"You are unable to travel that way\n";//displays error message and returns the function
return;
}
else{
area.strCurrentArea=strArea;// Sets the current area to the new one
GetAreaInfo(fin, area); // Passes in the file pointer so the new area data is read
DisplayArea(area);// Displays current area
}
}
/**********************************************************/
//*********************Input*******************************/
//******Main game mechanic feature. Receives **************/
//****user input and reacts to it accordingly to **********/
//*******progress through the game*************************/
/**********************************************************/
int Input(ifstream &fin, tArea &area){
string strInput="";//holds user input
cout<<endl<<":";//displays prompt
cin>>strInput;//reads in the user input
if(strInput=="look"){
DisplayArea(area);//calls function to give current area description
}
else if(strInput=="north"){
Move(fin,area,area.strNorth);//calls function to move north if possible
}
else if(strInput=="east"){
Move(fin,area,area.strEast);//calls function to move east if possible
}
else if(strInput=="west"){
Move(fin,area,area.strWest);//calls function to move west is possible
}
else if(strInput=="south"){//calls function to move south if possible
Move(fin,area,area.strSouth);
}
else if(strInput=="help"||strInput=="?"){//displays commands to user
cout<<"Commands: north east west south look quit help\n";
}
else if(strInput=="quit"){//ends game
cout<<"Quit game?\n";
return QUIT;
}
else{//displays when invalid input is received
cout<<"Invalid input\n";
}
return CONTINUE;
}
|