It's not that I find it HARD to code a knock off of ZORK but one thing after another keeps adding up and my main.cpp is getting so huge im started to get lost! ...I know, i know, its not THAT big, but for someone like me who is used to coding little things like calculators and other basic things, its pretty long ;p
I know I've probably coded this in a stupid way so If anyone has some suggestions as to how I can split it into other files or reuse parts of code so I don't have to manually re-write things, etc. Maybe some new methods I could check into because idk if this is normal but I feel like I used if, if else, and else statements for practically everything! xD
any advice / critic is appreciated. Thanks!
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
//strings
string name;
string input;
string output;
string confirm;
string currentArea;
string area;
bool confused;
bool dark;
//commands
string about;
string help;
string n;
string e;
string s;
string w;
string look;
string open;
string close;
//objects
string door1 = "closed";
bool checkArea(string area)
{
if (area == "area1")
{
if (input == "n")
{
look = "There is a mountain range to the North.";
cout << look;
confused = false;
}
else if (input == "e")
{
look = "The door seems to be unlocked.";
cout << look;
confused = false;
}
else if (input == "s")
{
look = "*I watch the waves run up the shore.*";
cout << look;
confused = false;
}
else if (input == "w")
{
look = "There seems to be what appears as a chain-link fence running from the coast to the mountain line. The fence has a gate but it looks as if it is locked.";
cout << look;
confused = false;
}
else
{
confused = true;
}
}
if (area == "area2")
{
if (input == "n")
{
look = "There is a painting of a whealthy looking man with a mustache on this wall.";
cout << look;
confused = false;
}
else if (input == "e")
{
look = "It is dark inside but I see a desk...";
cout << look;
confused = false;
}
else if (input == "s")
{
look = "Nothing here.";
cout << look;
confused = false;
}
else if (input == "w")
{
}
else
{
confused = true;
}
}
return false;
}
void checkInput()
{
cout << endl << "> ";
cin >> input;
if (input == "about")
{
output = "You are playing a text based RPG coded by Mark Rodgers.";
cout << output << endl;
checkInput();
}
else if (input == "help")
{
output = "COMMAND LIST:\n help - welcome!\n about - info about the game\n look - describe surroundings\n name - reminds you of your name\n open_OBJECTNAME - opens specified object\n close_OBJECTNAME - closes specified object\n exit - close game (alternative: quit)\n\nMOVEMENT:\n n - Move North\n e - Move East\n s - Move South\n w - Move West";
cout << output << endl;
checkInput();
}
else if (input == "look")
{
cout << look << endl;
checkInput();
}
else if (input == "name")
{
output = name;
cout << "My name is " << name << "." << endl;
checkInput();
}
else if (input == "open_door")
{
if (look == "The door seems to be unlocked.")
{
if (door1 == "closed")
{
output = "*I open the door*";
cout << output << endl;
door1 = "opened";
checkInput();
}
else if (door1 == "opened")
{
output = "The door is already open.";
cout << output << endl;
checkInput();
}
}
else
{
output = "There is nothing here to open.";
cout << output << endl;
checkInput();
}
}
else if (input == "close_door")
{
if (look == "The door seems to be unlocked.")
{
if (door1 == "opened")
{
output = "*I close the door*";
cout << output << endl;
door1 = "closed";
checkInput();
}
else if (door1 == "closed")
{
output = "The door is already closed.";
cout << output << endl;
checkInput();
}
}
else
{
output = "There is nothing here to open.";
cout << output << endl;
checkInput();
}
}
else if (input == "exit" || input == "quit")
{
cout << "Are you sure?(y/n)" << endl << endl << "> ";
cin >> confirm;
if (confirm == "yes" || confirm == "YES" || confirm == "Yes" || confirm == "y" || confirm == "Y")
{
return;
}
else if (confirm == "no" || confirm == "NO" || confirm == "No" || confirm == "n" || confirm == "N")
{
checkInput();
}
else
{
output = "Invalid response.";
cout << output << endl;
checkInput();
}
}
else
{
checkArea(currentArea);
if (confused = true)
{
output = "Huh?";
cout << output << endl;
}
checkInput();
}
}
void area2()
{
currentArea = "area2";
dark = true;
look = "It is dark inside but I see a desk...";
cout << look << endl;
checkInput();
}
void area1()
{
currentArea = "area1";
look = "I am standing on the beach.\nThere is a small shack to the east.";
cout << look << endl;
checkInput();
}
void main()
{
SetConsoleTitle( "ZORK" );
cout << "What is my name?" << endl << endl << "> ";
cin >> name;
area1();
}