Privet!
I want to check my code, and make it
simpler.
My code is supposed allow the user to navigate this matrix.
The NORTH, SOUTH, EAST,WEST are the boundaries.
If the user attempts to move in a direction outside these (N,S,E,W), he gets an error message.
Here's a digrammatic representation:
___________________________________________________________
|Bd'room 1 |Bd'room 2 |Porch |
| EAST (Upstairs) |SOUTH, EAST, WEST |WEST |
|______________________ |_______________________|___________|
|Kitchen |Stairs |Ba'room |
|SOUTH |NORTH, SOUTH |SOUTH |
|_______________________|_______________________|___________ |
|Dining | ENTRANCE |Living |
|NORTH & EAST |NORTH, EAST, WEST |NORTH, WEST |
|_______________________|_______________________|___________|
Here's my code for an adventure game:
Код:
#include <iostream>
#include <string>
using namespace std;
struct room
{
string data;
room* next;
room* prev;
};
void entrance(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void stairCase(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void bedRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void living(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void masterRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void porch1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void kitchen1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void bath(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
void dining(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom);
int main()
{
room* start;
room* livingRoom;
room* stairs;
room* masterRoom;
room* bedRoom;
room* porch;
room* kitchen;
room* bathRoom;
room* diningRoom;
cout << "Welcome to the Cabin!!!" << endl;
start = new room;
start->data = "Entrance of Cabin";
cout << start->data << endl;
livingRoom = new room;
start->next = livingRoom;
livingRoom->data = "This is the living room";
diningRoom = new room;
start->next = diningRoom;
diningRoom->data = "This is the dining room";
stairs = new room;
start->next = stairs;
stairs->data = "This is the staircase";
kitchen = new room;
diningRoom->next = kitchen;
kitchen->data = "This is the kitchen";
bathRoom = new room;
livingRoom->next = bathRoom;
bathRoom->data = "This is the bathroom";
bedRoom = new room;
stairs->next = bedRoom;
bedRoom->data = "This is the bedroom (upstairs)";
masterRoom = new room;
bedRoom->next = masterRoom;
masterRoom->data = "This is the master room ";
porch = new room;
bedRoom->next = porch;
porch->data = "This is the porch (upstairs)";
entrance(start, livingRoom, stairs, masterRoom, bedRoom, porch, kitchen, bathRoom, diningRoom);
}
void entrance(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are located in the entrance \n";
cout << "You could only move the in the following directions: \n North,\t East,\t or West \n";
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << stairs->data << endl;
stairCase(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << livingRoom->data << endl;
living(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << diningRoom->data << endl;
dining(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void stairCase(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the staircase\n";
cout << "You can only move in the following directions: \n North, \t South" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << bedRoom->data << endl;
bedRm(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << start->data << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void bedRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the upstairs bedroom\n";
cout << "You can only move in the following directions: \n South, \t East, \t West" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << stairs->data << endl;
stairCase(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << porch->data << endl;
porch1(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << masterRoom->data << endl;
masterRm(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void masterRm(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the upstairs master room\n" ;
cout << "You can only move in the following directions: \n East" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << bedRoom->data << endl;
bedRm(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch,kitchen, bathRoom, diningRoom); }
};
void kitchen1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the kitchen \n";
cout << "You can only move in the following directions: \n South" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom); //make method for bedroom
}
if (userInput == "S" || userInput == "SOUTH") {
cout << diningRoom->data << endl;
dining(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void dining(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the dining room" ;
cout << "You can only move in the following directions: \n North, \t East" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << kitchen->data << endl;
kitchen1(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << start->data << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void living(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the living room" << endl;
cout << "You can only move in the following directions: \n North, \t West" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << bathRoom->data << endl;
bath(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << start->data << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void bath(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are in the bathroom" << endl;
cout << "YOu can only move in the following directions: \n South" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << livingRoom->data << endl;
living(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
void porch1(room* start, room* livingRoom, room* stairs, room* masterRoom, room* bedRoom, room* porch, room* kitchen, room* bathRoom, room* diningRoom)
{
string userInput;
cout << "You are on the porch" << endl;
cout << "You can only move in the following directions: \n West" << endl;
cin >> userInput;
for(int i = 0; i < userInput.length(); i++) {
userInput[i] = toupper(userInput[i]);
}
if (userInput == "N" || userInput == "NORTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "S" || userInput == "SOUTH") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "E" || userInput == "EAST") {
cout << "I can't move in that direction" << endl;
entrance(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
if (userInput == "W" || userInput == "WEST") {
cout << bedRoom->data << endl;
bedRm(start, livingRoom, stairs,masterRoom, bedRoom,porch ,kitchen, bathRoom, diningRoom);
}
};
Spasibo!