M
Matthew
I am working on a simple text adventure game. I have implemented one
word commands and a bit for two words. I have a problem though the
movement commands n and s (north and south respectivly) don't work. I
can't figure out why!
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct command_v
{
string name;
bool (*vfunc)();
bool (*dfunc)();
};
struct command_vd
{
string name;
bool (*vfunc)(vector<string> input);
bool (*dfunc)(vector<string> input);
};
enum dirName{ n, s, e, w, north, south, east, west };
class Exit;
class Item
{
public:
string name;
vector<command_vd> theVDCmds;
};
class Character
{
public:
vector<Item> inven;
string name;
string desc;
vector<command_vd> theVDCmds;
};
class Location
{
public:
string name;
string desc;
vector<Exit> exits;
vector<Item> items;
vector<Character> chars;
vector<command_vd> theVDCmds;
};
class Exit
{
public:
Location leadsTo;
int shortName;
int longName;
string name;
vector<command_vd> theVDCmds;
};
void interpreter();
vector<string> split(string input);
bool parse(string input);
void setup();
bool vquit();
bool dquit();
bool vlook();
bool dlook();
bool vn();
bool dn();
bool vs();
bool ds();
vector<command_v> theVCmds;
Character mainChar;
Location currentRoom;
Location room1;
Location room2;
Exit ex1;
Exit ex2;
int main (int argc, const char * argv[])
{
setup();
interpreter();
return 0;
}
void interpreter()
{
cout << "Parser v0\n";
bool quit = false;
bool un;
string input;
dlook();
while(!quit)
{
cout << ":> ";
getline(cin, input);
un = parse(input);
if(!un)
{
cout << "Huh?\n";
continue;
}
}
}
void setup()
{
// set up standalone commands
command_v quit;
quit.name = "quit";
quit.vfunc = vquit;
quit.dfunc = dquit;
theVCmds.push_back(quit);
command_v look;
look.name = "look";
look.vfunc = vlook;
look.dfunc = dlook;
theVCmds.push_back(look);
command_v north1;
north1.name = "n";
north1.vfunc = vn;
north1.dfunc = dn;
theVCmds.push_back(north1);
command_v south1;
south1.name = "s";
south1.vfunc = vs;
south1.dfunc = ds;
theVCmds.push_back(south1);
mainChar.name = "Matthew";
mainChar.desc = "A valiant warrior!";
ex1.leadsTo = room1;
ex1.shortName = s;
ex1.longName = south;
ex1.name = "To the kitchen";
ex2.leadsTo = room2;
ex2.shortName = n;
ex2.longName = north;
ex2.name = "To the family room";
room1.name = "Family Room";
room1.desc = "A cozy room with a fireplace.";
room1.exits.push_back(ex1);
room2.name = "Kitchen";
room2.desc = "A warm kitchen.";
room2.exits.push_back(ex2);
currentRoom = room2;
}
vector<string> split(string input)
{
input += ' ';
vector<string> theStrings;
for(int i = 0, j = input.size(); i < j; ++i)
{
if(input == ' ' || input == '\n')
{
string temp(input.substr(0, i));
input.erase(0, i+1);
theStrings.push_back(temp);
i = 0;
j = input.size();
}
}
return theStrings;
}
bool parse(string input)
{
vector<string> theInput = split(input);
if(theInput[0] == "say" && theInput.size() > 1)
{
for(int i = 1; i < theInput.size(); ++i)
cout << theInput << " ";
cout << endl;
return true;
}
if(theInput.size() == 2)
{
for(int i = 0; i < currentRoom.items.size(); ++i)
{
for(int j = 0; j < currentRoom.items.theVDCmds.size();
++j)
{
if(theInput[0] ==
currentRoom.items.theVDCmds[j].name)
{
if(currentRoom.items.theVDCmds[j].vfunc(theInput))
return
currentRoom.items.theVDCmds[j].dfunc(theInput);
}
}
}
}
else if (theInput.size() == 1)
{
for(int i = 0; i < theVCmds.size(); ++i)
{
if(theInput[0] == theVCmds.name)
{
if(theVCmds.vfunc())
return theVCmds.dfunc();
}
}
}
return false;
}
bool vquit()
{
return true;
}
bool dquit()
{
string c;
cout << "Are you sure you want to quit? (yn): ";
cin >> c;
if(c == "y" || c == "yes")
{
cout << "Bye!\n";
exit(0);
return true;
}
if(c == "n" || c == "no")
{
getchar();
return true;
}
}
bool vlook()
{
return true;
}
bool dlook()
{
cout << currentRoom.name << "\n";
cout << "\n" << currentRoom.desc << "\n";
cout << "Exits: \n";
for(int i = 0; i < currentRoom.exits.size(); i++)
{
cout << "\t";
cout << currentRoom.exits.name << " (" <<
currentRoom.exits.shortName << ") " << endl;
}
cout << "Items: \n";
for(int i = 0; i < currentRoom.items.size(); i++)
{
cout << "\t";
cout << currentRoom.items.name << endl;
}
cout << "\nCharacters: \n";
for(int i = 0; i < currentRoom.chars.size(); i++)
{
cout << "\t";
cout << currentRoom.chars.name << endl;
}
cout << endl;
return true;
}
bool vn()
{
return true;
}
bool dn()
{
for(int i = 0; i < currentRoom.exits.size(); ++i)
{
if(currentRoom.exits.shortName == n)
{
currentRoom = currentRoom.exits.leadsTo;
break;
}
else
{
cout << "You can't go there!\n";
return true;
}
}
dlook();
return true;
}
bool vs()
{
return true;
}
bool ds()
{
for(int i = 0; i < currentRoom.exits.size(); ++i)
{
if(currentRoom.exits.shortName == s)
{
currentRoom = currentRoom.exits.leadsTo;
break;
}
else
{
cout << "You can't go there!\n";
return true;
}
}
dlook();
return true;
}
word commands and a bit for two words. I have a problem though the
movement commands n and s (north and south respectivly) don't work. I
can't figure out why!
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct command_v
{
string name;
bool (*vfunc)();
bool (*dfunc)();
};
struct command_vd
{
string name;
bool (*vfunc)(vector<string> input);
bool (*dfunc)(vector<string> input);
};
enum dirName{ n, s, e, w, north, south, east, west };
class Exit;
class Item
{
public:
string name;
vector<command_vd> theVDCmds;
};
class Character
{
public:
vector<Item> inven;
string name;
string desc;
vector<command_vd> theVDCmds;
};
class Location
{
public:
string name;
string desc;
vector<Exit> exits;
vector<Item> items;
vector<Character> chars;
vector<command_vd> theVDCmds;
};
class Exit
{
public:
Location leadsTo;
int shortName;
int longName;
string name;
vector<command_vd> theVDCmds;
};
void interpreter();
vector<string> split(string input);
bool parse(string input);
void setup();
bool vquit();
bool dquit();
bool vlook();
bool dlook();
bool vn();
bool dn();
bool vs();
bool ds();
vector<command_v> theVCmds;
Character mainChar;
Location currentRoom;
Location room1;
Location room2;
Exit ex1;
Exit ex2;
int main (int argc, const char * argv[])
{
setup();
interpreter();
return 0;
}
void interpreter()
{
cout << "Parser v0\n";
bool quit = false;
bool un;
string input;
dlook();
while(!quit)
{
cout << ":> ";
getline(cin, input);
un = parse(input);
if(!un)
{
cout << "Huh?\n";
continue;
}
}
}
void setup()
{
// set up standalone commands
command_v quit;
quit.name = "quit";
quit.vfunc = vquit;
quit.dfunc = dquit;
theVCmds.push_back(quit);
command_v look;
look.name = "look";
look.vfunc = vlook;
look.dfunc = dlook;
theVCmds.push_back(look);
command_v north1;
north1.name = "n";
north1.vfunc = vn;
north1.dfunc = dn;
theVCmds.push_back(north1);
command_v south1;
south1.name = "s";
south1.vfunc = vs;
south1.dfunc = ds;
theVCmds.push_back(south1);
mainChar.name = "Matthew";
mainChar.desc = "A valiant warrior!";
ex1.leadsTo = room1;
ex1.shortName = s;
ex1.longName = south;
ex1.name = "To the kitchen";
ex2.leadsTo = room2;
ex2.shortName = n;
ex2.longName = north;
ex2.name = "To the family room";
room1.name = "Family Room";
room1.desc = "A cozy room with a fireplace.";
room1.exits.push_back(ex1);
room2.name = "Kitchen";
room2.desc = "A warm kitchen.";
room2.exits.push_back(ex2);
currentRoom = room2;
}
vector<string> split(string input)
{
input += ' ';
vector<string> theStrings;
for(int i = 0, j = input.size(); i < j; ++i)
{
if(input == ' ' || input == '\n')
{
string temp(input.substr(0, i));
input.erase(0, i+1);
theStrings.push_back(temp);
i = 0;
j = input.size();
}
}
return theStrings;
}
bool parse(string input)
{
vector<string> theInput = split(input);
if(theInput[0] == "say" && theInput.size() > 1)
{
for(int i = 1; i < theInput.size(); ++i)
cout << theInput << " ";
cout << endl;
return true;
}
if(theInput.size() == 2)
{
for(int i = 0; i < currentRoom.items.size(); ++i)
{
for(int j = 0; j < currentRoom.items.theVDCmds.size();
++j)
{
if(theInput[0] ==
currentRoom.items.theVDCmds[j].name)
{
if(currentRoom.items.theVDCmds[j].vfunc(theInput))
return
currentRoom.items.theVDCmds[j].dfunc(theInput);
}
}
}
}
else if (theInput.size() == 1)
{
for(int i = 0; i < theVCmds.size(); ++i)
{
if(theInput[0] == theVCmds.name)
{
if(theVCmds.vfunc())
return theVCmds.dfunc();
}
}
}
return false;
}
bool vquit()
{
return true;
}
bool dquit()
{
string c;
cout << "Are you sure you want to quit? (yn): ";
cin >> c;
if(c == "y" || c == "yes")
{
cout << "Bye!\n";
exit(0);
return true;
}
if(c == "n" || c == "no")
{
getchar();
return true;
}
}
bool vlook()
{
return true;
}
bool dlook()
{
cout << currentRoom.name << "\n";
cout << "\n" << currentRoom.desc << "\n";
cout << "Exits: \n";
for(int i = 0; i < currentRoom.exits.size(); i++)
{
cout << "\t";
cout << currentRoom.exits.name << " (" <<
currentRoom.exits.shortName << ") " << endl;
}
cout << "Items: \n";
for(int i = 0; i < currentRoom.items.size(); i++)
{
cout << "\t";
cout << currentRoom.items.name << endl;
}
cout << "\nCharacters: \n";
for(int i = 0; i < currentRoom.chars.size(); i++)
{
cout << "\t";
cout << currentRoom.chars.name << endl;
}
cout << endl;
return true;
}
bool vn()
{
return true;
}
bool dn()
{
for(int i = 0; i < currentRoom.exits.size(); ++i)
{
if(currentRoom.exits.shortName == n)
{
currentRoom = currentRoom.exits.leadsTo;
break;
}
else
{
cout << "You can't go there!\n";
return true;
}
}
dlook();
return true;
}
bool vs()
{
return true;
}
bool ds()
{
for(int i = 0; i < currentRoom.exits.size(); ++i)
{
if(currentRoom.exits.shortName == s)
{
currentRoom = currentRoom.exits.leadsTo;
break;
}
else
{
cout << "You can't go there!\n";
return true;
}
}
dlook();
return true;
}