【发布时间】:2015-02-02 10:35:42
【问题描述】:
在编程方面我有点菜鸟,但作为一个小项目,我决定尝试制作一个非常简单的基于文本的冒险游戏,只是为了一些乐趣和练习。我不知道这些类型的游戏通常是如何制作的,但我决定制作一个包含所有实际文本的文本文件,而不是在代码中全部输入,所以这就是我的“gamelocationdata.txt”文件目前的样子。
[castleStart]
{
=castleStart=
You find yourself in a dark room inside a castle.
The walls are made of mossy stone and the entire room has a very eerie atmosphere.
There is a green bottle on the floor.
There are 2 exits, east and south.
Both exits are large doorways with grand arches.
You can see a large room through the south exit, but the east exit looks very dark and
somewhat frightening.
What will you do?
#"look arch" castleStartLookArch
#"look wall" castleStartLookWall
#"look bottle" castleStartLookBottle itemcond: hasBottle "0"
#"take bottle" castleStartLookBottle itemcond: hasBottle "0"
#"pick bottle" castleStartLookBottle itemcond: hasBottle "0"
#"go south" castleHall
#"go east" castleDark loccond: hasBeenCastleDark "0"
#"wait" castleStartWait
}
[castleStartLookArch]
{
=castleStart=
The arches above the doors look very fancy.
You can't quite figure out why this tiny room deserves to be decorated as such.
#(castleStart)
}
[castleStartLookWall]
{
=castleStart=
The wall is made of stone masonry in an old-fashioned way.
Some stones are covered in moss and there are cobwebs in the corners.
#(castleStart)
}
[castleStartWait]
{
=castleStart=
You sit still and admire the wall for a while.
Nothing happens.
#(castleStart)
}
[castleStartLookBottle]
{
=castleStart=
You pick the bottle up. It is covered in cobwebs.
The bottle is green and the label reads "1337". It contains a purple liquid.
Do you want to put the bottle in your backpack?
#"yes" castleStartTakeBottle
#"no" castleStartNoTakeBottle
}
[castleStartTakeBottle]
{
=castleStart=
You take the bottle and put it in your backpack.
+item: Bottle1337
+itemcond: hasBottle "1"
#(castleStart)
}
[castleStartNoTakeBottle]
{
=castleStart=
You put the bottle back down again.
#(castleStart)
}
[useBottle1337]
{
=curLocation=
You open the bottle and drink its contents.
It tastes very sweet.
You suddenly feel slightly stronger and more powerful.
+strength: 5
+remove_item: Bottle1337
#(castleStart)
}
[castleHall]
{
=castleHall=
You walk though the southern doorway and enter the grand hall of the castle.
It seems like the entire castle is just as old and worn out as the walls in that room,
though still very nicely decorated.
There are only a few candles on the walls, and they are somehow lit despite
the castle seeming very empty. There is not not a person to be seen.
You can go back north or proceed south through the hall.
#(castleStart)
}
[castleDark]
{
=castleStart=
You slowly tread into the dark room to the east, looking
around you as your surroundings get darker and darker.
Suddenly, you hear a noise. It sounds like the growling of an angry dog!
Horrified, you hastily turn around and run back.
+loccond: hasBeenCastleDark "1"
#(castleStart)
}
我意识到我咬的东西可能比我能咀嚼的要多,但我编的格式应该是这样工作的:
示例:[castleStart] 是“位置”的名称,而大括号 后面的大括号封装了与此有关的所有内容 位置。
示例:=castleStart= 是播放器在何时打印的位置 他们问他们目前在哪里。
之后的内容是播放器在屏幕上打印的内容 “进入”那个位置。
位置文字之后,有一堆选项都开始 带有“#”。
示例:#"wait" castleStartWait 如果玩家键入“wait”,他们将 被带到名为 [castleStartWait] 的“位置”。
示例:#"look bottle" castleStartLookBottle itemcond: hasBottle "0" 如果玩家输入“看瓶”,他们将被带到该位置 命名为 [castleStartLookBottle] 只要满足“item 要求”他们还没有瓶子。
示例:#"go east" castleDark loccond: hasBeenCastleDark "0" 如果 玩家输入“向东”,他们将被带到指定的位置 [castleDark] 只要满足他们的“位置要求” 还没去过。
示例:#(castleStart) 这将使用与 [castleStart] 中列出的选项相同的选项。
示例:+strength: 5 这应该会在玩家进入该位置并打印一些硬编码消息,例如“您已获得 5 个力量点!”时为玩家的“力量”统计数据增加 5 点。 p>
现在,问题来了:如何编写读取和解析特定位置的数据并将它们存储在特定 std::strings 中的函数?
例如,如果我这样做
readAndParseLocationData( castleStart );
它应该在文本文件中查找 [castleStart],然后读取等号 (=castleStart=) 之间的内容并将其存储在“std::string printLoc”中,然后读取文本并存储在“std ::string locText" 等等。
这是我目前的所有代码:
//main.cpp
#include <iostream>
#include <string>
#include "ClearScreen.h"
#include "LocationData.h"
int main()
{
ClearScreen();
std::cout << "I am a banana!\n\n"; // this is just a test
readAndParseLocationData( "castleHall" );
printLocationData( "castleStart" ); // this is supposed to be used for debugging the location data by printing it.
return 0;
}
--
//LocationData.cpp
#include "LocationData.h"
#include <fstream>
#include <iostream>
#include <string>
void readAndParseLocationData( std::string location )
{
location.insert( 0,"[" );
location.append( "]" );
std::ifstream locfile( "gamelocationdata.txt" );
if( locfile.is_open() )
{
std::string line;
bool foundFile = false;
for( unsigned int curLine = 0; getline( locfile,line ); curLine++ )
{
if( line.find( location ) != std::string::npos )
{
std::cout << "found: " << location << ", line: " << curLine << "\n";
foundFile = true;
}
}
if( !foundFile )
{
std::cout << "\nERROR: Location " << location << " not found in data file!\n";
}
locfile.close();
}
else
{
std::cout << "\nERROR: Unable to open location data file!\n";
}
}
void printLocationData( std::string location )
{
//TODO: Implement
}
我所做的一切(通过广泛的谷歌搜索)是查找位置名称并将其打印到控制台上的哪一行。
我在 Windows 7 上使用 Visual Studio Express 2013。
我也很想知道是否有任何方法可以改进我的代码或一般格式!
【问题讨论】:
标签: c++ windows file parsing file-io