I've just learned the basics of C++, and as my first real project I am
attempting to construct a text adventure, like back in the good old
days of Commodore 64 BASIC. This is my code so far:
#include <iostream>
#include <string>
using namespace std;
struct room {
int number;
int numofexits;
int special;
string description;
room (int a, int b, int c, string d);
};
room::room (int a, int b, int c, string d) {
number=a;
numofexits=b;
special=c;
description=d;
}
room Foyer(1,3,0,"Yo u are in the foyer of the cathedral.\nIt' s hard to
believe you've just entered for any purpose other than worship
or\nsacrifice.\ n");
room* allrooms[50]={&Foyer};
int location=0;
int main () {
cout << "************** **\n";
cout << "** The Depths **\n";
cout << "************** **\n\n\n";
cout << allrooms[location]->description;
}
Pretty straightforward , I think. My problem is this: Each room is
going to have a number of exits (defined by "numofexits "). Obviously,
those exits will lead to different rooms based on which room you are
standing in. I've tried to make a dynamic array with different sizes
for different objects of the "room" class, but that didn't work out.
What would be the best way to do this? Sorry if I've been confusing in
any way; just point it out and I'll clarify. Thanks in advance for any
help. 28 1728
Caleb wrote:
I've just learned the basics of C++, and as my first real project I am
attempting to construct a text adventure, like back in the good old
days of Commodore 64 BASIC. This is my code so far:
#include <iostream>
#include <string>
using namespace std;
struct room {
int number;
int numofexits;
int special;
string description;
room (int a, int b, int c, string d);
};
room::room (int a, int b, int c, string d) {
number=a;
numofexits=b;
special=c;
description=d;
}
room Foyer(1,3,0,"Yo u are in the foyer of the cathedral.\nIt' s hard to
believe you've just entered for any purpose other than worship
or\nsacrifice.\ n");
room* allrooms[50]={&Foyer};
int location=0;
int main () {
cout << "************** **\n";
cout << "** The Depths **\n";
cout << "************** **\n\n\n";
cout << allrooms[location]->description;
}
Pretty straightforward , I think. My problem is this: Each room is
going to have a number of exits (defined by "numofexits "). Obviously,
those exits will lead to different rooms based on which room you are
standing in. I've tried to make a dynamic array with different sizes
for different objects of the "room" class, but that didn't work out.
What would be the best way to do this? Sorry if I've been confusing in
any way; just point it out and I'll clarify. Thanks in advance for any
help.
I've seen it where there is no "numofexits ". Instead, there are always
the 6 standard exits NSEWUD. This means your room structure doesn't
use dynamic arrays. If a room only uses 3 of the exits, the others are
set to 0 and the program prints "you can't go that way" if the player
tries to take one of those exits. Negative room numbers were used
for special effects such as -99 being GAME OVER.
The extra memory needed to hold six exits for every room as opposed
to holding a variable number of room exits worked well enough on
an Apple ][, so I doubt you'll have a problem with it.
On Dec 20, 6:12 am, "Mensanator " <mensana...@aol .comwrote:
Caleb wrote:
Pretty straightforward , I think. My problem is this: Each room is
going to have a number of exits (defined by "numofexits "). Obviously,
those exits will lead to different rooms based on which room you are
standing in. I've tried to make a dynamic array with different sizes
for different objects of the "room" class, but that didn't work out.
What would be the best way to do this? Sorry if I've been confusing in
any way; just point it out and I'll clarify. Thanks in advance for any
help.
I've seen it where there is no "numofexits ". Instead, there are always
the 6 standard exits NSEWUD. This means your room structure doesn't
use dynamic arrays. If a room only uses 3 of the exits, the others are
set to 0 and the program prints "you can't go that way" if the player
tries to take one of those exits. Negative room numbers were used
for special effects such as -99 being GAME OVER.
The extra memory needed to hold six exits for every room as opposed
to holding a variable number of room exits worked well enough on
an Apple ][, so I doubt you'll have a problem with it.
But it would be so much better C++ to use a vector, else one might
almost as well use C.
--
Erik Wikström
Caleb wrote:
I've just learned the basics of C++, and as my first real project I am
attempting to construct a text adventure, like back in the good old
days of Commodore 64 BASIC. This is my code so far:
#include <iostream>
#include <string>
using namespace std;
struct room {
int number;
int numofexits;
int special;
string description;
room (int a, int b, int c, string d);
};
room::room (int a, int b, int c, string d) {
number=a;
numofexits=b;
special=c;
description=d;
}
room Foyer(1,3,0,"Yo u are in the foyer of the cathedral.\nIt' s hard to
believe you've just entered for any purpose other than worship
or\nsacrifice.\ n");
room* allrooms[50]={&Foyer};
int location=0;
int main () {
cout << "************** **\n";
cout << "** The Depths **\n";
cout << "************** **\n\n\n";
cout << allrooms[location]->description;
}
Pretty straightforward , I think. My problem is this: Each room is
going to have a number of exits (defined by "numofexits "). Obviously,
those exits will lead to different rooms based on which room you are
standing in. I've tried to make a dynamic array with different sizes
for different objects of the "room" class, but that didn't work out.
What would be the best way to do this? Sorry if I've been confusing in
any way; just point it out and I'll clarify. Thanks in advance for any
help.
Use a list type like vector<>, and put pointers in it; don't put
objects in it directly, by value.
You can't store objects of different sizes in a vector<>. The only way
to do that would be to put objects of different types in the vector,
and you can't do that directly. But you can do it if you put in
pointers of some base class.
So, don't use (for instance) vector<Room-- use vector<Room*ins tead.
Then you can store not only Rooms, but subclasses of Rooms.
The main disadvantage of this scheme is that you must be careful to
delete these pointers when you remove them from the list, because
vector<doesn't do that for you. And things get hairy when you have
Rooms stored in several different lists; that's when reference-counting
begins to look attractive. But that's the price of polymorphism.
(By the way, you might consider using 'class' here instead of 'struct'.
In C++, a struct *is* a class -- the only difference is that its
members are public by default, while the members of a class are private
by default. But most people think 'C-style struct with no methods' when
they see the 'struct' keyword, so a struct with methods and access
specifiers, while perfectly legit, looks odd.)
Good luck! --mpa
Michael Ashton a écrit :
Caleb wrote:
>I've just learned the basics of C++, and as my first real project I am attempting to construct a text adventure, like back in the good old days of Commodore 64 BASIC. This is my code so far:
#include <iostream> #include <string> using namespace std;
struct room { int number; int numofexits; int special; string description; room (int a, int b, int c, string d);
};
room::room (int a, int b, int c, string d) { number=a; numofexits=b; special=c; description=d;
}
room Foyer(1,3,0,"Yo u are in the foyer of the cathedral.\nIt' s hard to believe you've just entered for any purpose other than worship or\nsacrifice. \n"); room* allrooms[50]={&Foyer}; int location=0;
int main () { cout << "************** **\n"; cout << "** The Depths **\n"; cout << "************** **\n\n\n"; cout << allrooms[location]->description;
}
Pretty straightforward , I think. My problem is this: Each room is going to have a number of exits (defined by "numofexits "). Obviously, those exits will lead to different rooms based on which room you are standing in. I've tried to make a dynamic array with different sizes for different objects of the "room" class, but that didn't work out. What would be the best way to do this? Sorry if I've been confusing in any way; just point it out and I'll clarify. Thanks in advance for any help.
Use a list type like vector<>, and put pointers in it; don't put
objects in it directly, by value.
You can't store objects of different sizes in a vector<>. The only way
to do that would be to put objects of different types in the vector,
and you can't do that directly. But you can do it if you put in
pointers of some base class.
So, don't use (for instance) vector<Room-- use vector<Room*ins tead.
Then you can store not only Rooms, but subclasses of Rooms.
The main disadvantage of this scheme is that you must be careful to
delete these pointers when you remove them from the list, because
vector<doesn't do that for you. And things get hairy when you have
Rooms stored in several different lists; that's when reference-counting
begins to look attractive. But that's the price of polymorphism.
When using pointers in this way, Boost's shared_ptr<make s your life
easier with this kind of design:
typedef boost::shared_p tr<RoomRoomShar edPtr
std::vector< RoomSharedPtr allrooms;
....
allrooms.push_b ack(RoomSharedP tr(new room(1,3,0,"You are in the foyer of
the cathedral.\nIt' s hard tobelieve you've just entered for any purpose
other than worship or\nsacrifice.\ n")));
Michael
Mensanator wrote:
The extra memory needed to hold six exits for every room as opposed
to holding a variable number of room exits worked well enough on
an Apple ][, so I doubt you'll have a problem with it.
Very true. But I had hoped that by learning C++ I could expand the
program into more advanced stuff, such as not being limited to one exit
in each direction. For example, a large room with three evenly-spaced
northward exits that branch into different hallways.
I will look into this vector business. Thanks for all the suggestions,
and I will report back after I find out about the vector stuff. :)
std::vector does exactly what I want it to do. It seems to be
extremely advantageous over regular arrays, too. There's just one
thing I'm wondering....is there a way to push_back three elements at
once? For example, I'm going to have a list of door initializations
that look like this:
Foyer.destinati on.push_back(2) ;Foyer.destinat ion.push_back(3 );Foyer.destina tion.push_back( 6);
And that's only for a 3-door room....is there a more efficient way to
code that?
Caleb wrote:
Mensanator wrote:
>>The extra memory needed to hold six exits for every room as opposed to holding a variable number of room exits worked well enough on an Apple ][, so I doubt you'll have a problem with it.
Very true. But I had hoped that by learning C++ I could expand the
program into more advanced stuff, such as not being limited to one exit
in each direction. For example, a large room with three evenly-spaced
northward exits that branch into different hallways.
How would someone using the program distinguish between those exits? IE
what command would they give that would allow them to go to a particular
exit?
LR
LR wrote:
How would someone using the program distinguish between those exits? IE
what command would they give that would allow them to go to a particular
exit?
LR
Not that I understand how that's significant to my question, hehe :),
but I'm going to be adding menus of exits/objects in the room to the
room descriptions. If the player wants to move, they'll just input the
number corresponding with the door they want.
I thought of a way I could set it up where I can add multiple elements
with one statement....ra ther simple, really. Just overload an
"add_doors" function with one integer, two integers, five integers, and
whatever amounts of integers that I need, and write it to push_back the
elements that were provided one at a time. Contemplating whether that
would really be worth it, though. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: DotNetIsHorrible |
last post by:
I write CRUD database applications for a living for an audience of
about 100 users per application using classic ASP. I maintain and
frequently change on user's request 22 different applications by
myself.
Recently we have had .NET 1.1 then 2.0 installed and have been told to
use it.
After reading the books about it, and trying it for a couple of
months, I got so mad I decided to write down all of my issues and
rants and post them here....
|
by: Mark Broadbent |
last post by:
this might sound like an obvious question but I have found that usually
these two evolve at the same time.
One of the biggest reasons for creating the abstraction in the first place
(in my opinion) is to create a reusable framework that can be applied to
similar projects. However I have found that if an abstraction is created
first during the development phase, when the implementation is occurring,
the functionality or intended behaviour...
|
by: Mike L |
last post by:
This is for a Win form.
Currently, the first record is shown in the combo box.
I want the combo box to be blank and when the user clicks on the down arrow
to choose from the list and clicks on a item in the list then that item is
shown in the combo box.
|
by: Larry Bird |
last post by:
I'm attempting to build my first VC++ .net project and I'm unable to get a
clean compile. Please view code below:
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#using <mscorlib.dll>
|
by: Frank Wilson |
last post by:
Tom,
It sounds to me like ASP, not ASP.NET is handling the
request for WebForm1.aspx. This is most likely an IIS
config issue that may have been caused by order of
installation or reinstallation, or possibly even
something that happened in IIS before .NET was
installed.
Here is a way to prove or disprove my theory:
| |
by: tshad |
last post by:
I have a problem with a VS 2003 project.
This project was designed and works fine in VS 2003.
But trying to open the project I get the following error.
************************************************************
The class EmailPoller can be designed, but is not the first class in the
file. Visual Studio requires that designers use the first class in the
file. Move the class code so that it is the first class in the file and try...
|
by: nospam |
last post by:
I have been running .NET 2003 for some time now. I have been working on
the same C++ project for about a year now. All of a sudden, the first
file in the project is being built every time I build the project. I
tried deleting all of the OBJ files. I removed and re-added the
offending file which moved it (temporarily) to the bottom of the list
and the next file in the list was being recompiled every time. When I
reopened the project,...
|
by: michigaki |
last post by:
hello,
we are having problems in compiling a 'slightly' altered x264. We are always receiving a 'helloWorld' undeclared (first use this function) error. We may be commiting a very simple error but we just have to ask after looking through possible solutions that have not worked.
So far, we are able to successfully compile the x263 source (an unaltered one from the VideoLAN downloads section) using the instructions found at...
|
by: KDawg44 |
last post by:
Hi,
I am new to Python and am trying to write a little front end to
another application in Python.
What I want is to have a gui pop up listing some items with several
buttons. The guts of the program I am not having any trouble with but
the GUI part I am (or more accurately, the transition between GUI
pieces).
|
by: =?Utf-8?B?SmltIE93ZW4=?= |
last post by:
Hi John,
Hopefully this post will find its way back to you - or perhaps be answered
by someone else. As I mentioned in my last post on the earlier portion of
this thread, changing the serialization settings for the build handled the
initial slows we encountered when invoking the web service.
Since that time, we ported the original VB.net code over to C# - this was
done to make it cleaner easier to include the project in the rest of...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |