473,785 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Characte r> 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_b ack(quit);

command_v look;
look.name = "look";
look.vfunc = vlook;
look.dfunc = dlook;
theVCmds.push_b ack(look);

command_v north1;
north1.name = "n";
north1.vfunc = vn;
north1.dfunc = dn;
theVCmds.push_b ack(north1);

command_v south1;
south1.name = "s";
south1.vfunc = vs;
south1.dfunc = ds;
theVCmds.push_b ack(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.pus h_back(ex1);

room2.name = "Kitchen";
room2.desc = "A warm kitchen.";
room2.exits.pus h_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.subs tr(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.siz e() == 2)
{
for(int i = 0; i < currentRoom.ite ms.size(); ++i)
{
for(int j = 0; j < currentRoom.ite ms[i].theVDCmds.size ();
++j)
{
if(theInput[0] ==
currentRoom.ite ms[i].theVDCmds[j].name)
{
if(currentRoom. items[i].theVDCmds[j].vfunc(theInput ))
return
currentRoom.ite ms[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.nam e << "\n";
cout << "\n" << currentRoom.des c << "\n";
cout << "Exits: \n";
for(int i = 0; i < currentRoom.exi ts.size(); i++)
{
cout << "\t";
cout << currentRoom.exi ts[i].name << " (" <<
currentRoom.exi ts[i].shortName << ") " << endl;
}
cout << "Items: \n";
for(int i = 0; i < currentRoom.ite ms.size(); i++)
{
cout << "\t";
cout << currentRoom.ite ms[i].name << endl;
}
cout << "\nCharacte rs: \n";
for(int i = 0; i < currentRoom.cha rs.size(); i++)
{
cout << "\t";
cout << currentRoom.cha rs[i].name << endl;
}
cout << endl;
return true;
}

bool vn()
{
return true;
}

bool dn()
{
for(int i = 0; i < currentRoom.exi ts.size(); ++i)
{
if(currentRoom. exits[i].shortName == n)
{
currentRoom = currentRoom.exi ts[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.exi ts.size(); ++i)
{
if(currentRoom. exits[i].shortName == s)
{
currentRoom = currentRoom.exi ts[i].leadsTo;
break;
}
else
{
cout << "You can't go there!\n";
return true;
}
}
dlook();
return true;
}
Jul 19 '05 #1
1 2228
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
14855
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 software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
72
5898
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 example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
17
2649
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 directly. so I need a way doing this. so I check to see if the ifp value is null and if so then assign it a value. is this correct?
121
10180
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 support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
28
3279
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(); fopen("c:\\autoexec.bat","r")&&printf("success") || printf("error opeing
13
5058
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
26220
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
3
2149
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 Record (i) is shown and modified, the change will come to Record (i+1). Can anyone provide suggestion? thanks.
38
2005
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - What books cover EcmaScript? ----------------------------------------------------------------------- Most CLJ regulars believe the best book to be: JavaScript: The Definitive Guide, 5th Edition By David Flanagan ISBN:0-596-10199-6 The errata should be read along with the book.
16
3450
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), _netguid(netguid) {}
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.