473,657 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My first project

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.

Dec 20 '06 #1
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.

Dec 20 '06 #2
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

Dec 20 '06 #3
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

Dec 20 '06 #4
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
Dec 20 '06 #5

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.

Dec 20 '06 #6
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. :)

Dec 20 '06 #7
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?

Dec 21 '06 #8
LR
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
Dec 21 '06 #9

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.

Dec 21 '06 #10

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

Similar topics

19
2173
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....
6
1996
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...
10
4285
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.
11
1856
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>
6
2291
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:
7
4654
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...
4
1208
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,...
7
32347
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...
10
2069
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).
2
5556
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...
0
8421
marktang
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...
0
8844
Oralloy
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...
0
8742
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
8518
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,...
1
6177
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
5643
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();...
0
4173
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...
1
2743
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
1971
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.