我的第一个项目 [英] My first project

查看:61
本文介绍了我的第一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学会了C ++的基础知识,并且作为我的第一个真正的项目,我试图构建一个文本冒险,就像回到古老的
$ b $ Commodore 64 BASIC的b天。这是我到目前为止的代码:


#include< iostream>

#include< string>

using namespace std ;


struct room {

int number;

int numofexits;

int special;

字符串描述;

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;


}


房间门厅(1,3,0,你在大教堂的门厅里。但是很难

相信你刚刚进入礼拜以外的任何目的

或\ nacacrifice.\ n");

房间* allrooms [50] = {& Foyer};

int location = 0;


int main(){

cout<< " **************** \ n";

cout<< " ** The Depths ** \ n";

cout<< **************** \ n \ n \ nn;

cout<< allrooms [location] - > description;


}


我觉得很简单。我的问题是这样的:每个房间都会有许多出口(由numofexits定义)。显然,

那些出口会导致不同的房间根据你所在的房间而建立。我试图制作一个不同尺寸的动态阵列

用于房间的不同对象。上课但是没有成功。

最好的方法是什么?对不起,如果我以任何方式混淆

;只是指出它,我会澄清。预先感谢任何

帮助。

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,"You 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.

推荐答案



Caleb写道:

Caleb wrote:

我刚刚学会了C ++的基础知识,作为我的第一个真正的项目我是试图构建文本冒险的
,就像回到Commodore 64 BASIC的好日子一样。这是我到目前为止的代码:


#include< iostream>

#include< string>

using namespace std ;


struct room {

int number;

int numofexits;

int special;

字符串描述;

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;


}


房间门厅(1,3,0,你在大教堂的门厅里。但是很难

相信你刚刚进入礼拜以外的任何目的

或\ nacacrifice.\ n");

房间* allrooms [50] = {& Foyer};

int location = 0;


int main(){

cout<< " **************** \ n";

cout<< " ** The Depths ** \ n";

cout<< **************** \ n \ n \ nn;

cout<< allrooms [location] - > description;


}


我觉得很简单。我的问题是这样的:每个房间都会有许多出口(由numofexits定义)。显然,

那些出口会导致不同的房间根据你所在的房间而建立。我试图制作一个不同尺寸的动态阵列

用于房间的不同对象。上课但是没有成功。

最好的方法是什么?对不起,如果我以任何方式混淆

;只是指出它,我会澄清。在此先感谢任何

帮助。
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,"You 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.



我见过没有numofexits的地方。相反,总是有6个标准退出NSEWUD

。这意味着你的房间结构不会使用动态数组。如果一个房间只使用了3个出口,那么其他房间将被设置为0并且程序打印出你不能这样做。如果玩家

试图采取其中一个出口。使用负面房间号码

用于特殊效果,例如-99为GAME OVER。


为每个房间保留六个出口所需的额外内存,而不是

持有可变数量的房间出口工作得很好

一个Apple] [,所以我怀疑你会遇到问题。

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.


12月20日上午6:12,Mensanator < mensana ... @ aol.comwrote:
On Dec 20, 6:12 am, "Mensanator" <mensana...@aol.comwrote:

Caleb写道:
Caleb wrote:

非常简单,我认为。我的问题是这样的:每个房间都会有许多出口(由numofexits定义)。显然,

那些出口会导致不同的房间根据你所在的房间而建立。我试图制作一个不同尺寸的动态阵列

用于房间的不同对象。上课但是没有成功。

最好的方法是什么?对不起,如果我以任何方式混淆

;只是指出它,我会澄清。在此先感谢任何

帮助。
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.



我见过没有numofexits的地方。相反,总是有6个标准退出NSEWUD

。这意味着你的房间结构不会使用动态数组。如果一个房间只使用了3个出口,那么其他房间将被设置为0并且程序打印出你不能这样做。如果玩家

试图采取其中一个出口。使用负面房间号码

用于特殊效果,例如-99为GAME OVER。


为每个房间保留六个出口所需的额外内存,而不是<持有可变数量的房间出口的
在苹果公司工作得好...... [,所以我怀疑你会遇到问题。


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.



但是使用矢量会更好C ++,否则可能会使用C. b / b
。使用C.


-

Erik Wikstr?m

But it would be so much better C++ to use a vector, else one might
almost as well use C.

--
Erik Wikstr?m


Caleb写道:
Caleb wrote:

我刚刚学会了C ++的基础知识,作为我的第一个真正的项目,我试图构建文本冒险,就像回到古老的文本冒险中一样

天Commodore 64 BASIC。这是我到目前为止的代码:


#include< iostream>

#include< string>

using namespace std ;


struct room {

int number;

int numofexits;

int special;

字符串描述;

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;


}


房间门厅(1,3,0,你在大教堂的门厅里。但是很难

相信你刚刚进入礼拜以外的任何目的

或\ nacacrifice.\ n");

房间* allrooms [50] = {& Foyer};

int location = 0;


int main(){

cout<< " **************** \ n";

cout<< " ** The Depths ** \ n";

cout<< **************** \ n \ n \ nn;

cout<< allrooms [location] - > description;


}


我觉得很简单。我的问题是这样的:每个房间都会有许多出口(由numofexits定义)。显然,

那些出口会导致不同的房间根据你所在的房间而建立。我试图制作一个不同尺寸的动态阵列

用于房间的不同对象。上课但是没有成功。

最好的方法是什么?对不起,如果我以任何方式混淆

;只是指出它,我会澄清。在此先感谢任何

帮助。
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,"You 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.



使用像vector<>这样的列表类型,并在其中添加指针;不要按价值直接放入

对象。


你不能在矢量<>中存储不同大小的物体。这样做的唯一方法就是将不同类型的对象放在向量中,

并且你不能直接这样做。但如果你输入一些基类的
指针就可以做到。


所以,不要使用(例如)vector< Room- - 使用vector< Room *代替。

然后你不仅可以存储房间,还可以存储房间的子类。


这个方案的主要缺点是你当你从列表中删除它们时,一定要小心

删除这些指针,因为

vector<不能为你做这些。当你有
房间储存在几个不同的清单中时,事情变得多毛了;当参考计数

开始看起来很有吸引力时。但这是多态性的代价。


(顺便说一下,你可以考虑在这里使用''class''而不是''struct''。

在C ++中,struct *是*类 - 唯一的区别是它的

成员默认是公共的,而类的成员是私有的

默认情况下。但是当大多数人认为没有方法的C风格的结构时,他们会看到''struct''关键字,所以一个带有方法和访问权限的结构

说明者,虽然完全合法,但看起来很奇怪。)


祝你好运! --mpa

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*instead.
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


这篇关于我的第一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆