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
|
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct room{
string name;
string description;
string examine;
room* west;
room* east;
room* north;
room* south;
room(string Name, string Description, string Examine){
name = Name;
description = Description;
examine = Examine;
}
void setDirection(room& West, room& East, room& North, room& South){
west = &West;
east = &East;
north = &North;
south = &South;
}
};
void getUserInput(string& x){
cout << ">";
getline(cin, x);
transform(x.begin(), x.end(), x.begin(), ::tolower);
}
int main(){
string userInput;
room* currentRoom;
room emptyRoom("empty room", "This room is empty/blocked off.", "It's just black.");
room kitchen("kitchen","You're standing in a kitchen.", "It's a well-lit kitchen. It offers everything a kitchen might need.");
room bathroom("bathroom", "You're standing in a bathroom.", "It's a stinky toilet. Somebody must have forgotten to flush.");
room livingRoom("living room", "You're standing a living room.", "You can't get your eyes of that big televison.");
room bedroom("bedroom", "You're'standing in a bedroom.","That bed looks comfy.");
kitchen.setDirection(emptyRoom, bathroom, emptyRoom, livingRoom);
bathroom.setDirection(kitchen, emptyRoom, emptyRoom, bedroom);
livingRoom.setDirection(emptyRoom, bedroom, kitchen, emptyRoom);
bedroom.setDirection(livingRoom, emptyRoom, bathroom, emptyRoom);
currentRoom = &kitchen;
while(true){
cout << endl;
cout << currentRoom->description;
cout << endl;
getUserInput(userInput);
if(userInput == "west" && currentRoom->west != &emptyRoom){
currentRoom = currentRoom->west;
}
else if(userInput == "east" && currentRoom->east != &emptyRoom){
currentRoom = currentRoom->east;
}
else if(userInput == "north" && currentRoom->north != &emptyRoom){
currentRoom = currentRoom->north;
}
else if(userInput == "south" && currentRoom->south != &emptyRoom){
currentRoom = currentRoom->south;
}
else if(userInput == "examine"){
cout << currentRoom->examine;
}
else{
cout << "You can't go that way!" << endl;
}
}
return 0;
}
|