473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

What is wrong here?

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[i] == ' ' || input[i] == '\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[i] << " ";
cout << endl;
return true;
}

if(theInput.size() == 2)
{
for(int i = 0; i < currentRoom.items.size(); ++i)
{
for(int j = 0; j < currentRoom.items[i].theVDCmds.size();
++j)
{
if(theInput[0] ==
currentRoom.items[i].theVDCmds[j].name)
{
if(currentRoom.items[i].theVDCmds[j].vfunc(theInput))
return
currentRoom.items[i].theVDCmds[j].dfunc(theInput);
}
}
}
}
else if (theInput.size() == 1)
{
for(int i = 0; i < theVCmds.size(); ++i)
{
if(theInput[0] == theVCmds[i].name)
{
if(theVCmds[i].vfunc())
return theVCmds[i].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[i].name << " (" <<
currentRoom.exits[i].shortName << ") " << endl;
}
cout << "Items: \n";
for(int i = 0; i < currentRoom.items.size(); i++)
{
cout << "\t";
cout << currentRoom.items[i].name << endl;
}
cout << "\nCharacters: \n";
for(int i = 0; i < currentRoom.chars.size(); i++)
{
cout << "\t";
cout << currentRoom.chars[i].name << endl;
}
cout << endl;
return true;
}

bool vn()
{
return true;
}

bool dn()
{
for(int i = 0; i < currentRoom.exits.size(); ++i)
{
if(currentRoom.exits[i].shortName == n)
{
currentRoom = currentRoom.exits[i].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[i].shortName == s)
{
currentRoom = currentRoom.exits[i].leadsTo;
break;
}
else
{
cout << "You can't go there!\n";
return true;
}
}
dlook();
return true;
}
Jul 19 '05 #1
1 2208
Matthew wrote:
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.
What exactly does "don't work" mean?
Don't they compile? What did the compiler say?
Are they doing something unexpected? What?
Are they crashing? Where?
I can't figure out why!


Reduce your code to the minimum possible, but complete program that
shows the error. It often happens that, while reducing the code, you
find the error yourself. If not, post the result again. Your current
program is too long, so probably nobody will bother reading through it.

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
17
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
38
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - What books cover EcmaScript? ----------------------------------------------------------------------- Most CLJ...
16
by: John Doe | last post by:
Hi, I wrote a small class to enumerate available networks on a smartphone : class CNetwork { public: CNetwork() {}; CNetwork(CString& netName, GUID netguid): _netname(netName),...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.