gpt4 book ai didi

c++ - 重复和空格

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:38:55 25 4
gpt4 key购买 nike

所以,我有一个基于文本的小游戏,我的大部分东西都设置好了,但如果关键字不正确,它会重置,你必须再次搜索第一个关键字才能继续沿着路径线程.此外,我在 if 语句的最后添加了 sleep ,以进一步询问如何将延迟键入效果应用于 cout 语句。如果不可能,无论如何谢谢。让我知道,我喜欢一些输入,并希望在这里摆脱困境。


char intro[] = "INTRO_TEXT";

for (int i = 0; i < sizeof(intro); i++) {
cout << intro[i];
Sleep(30);
}

char result00[] = "RESULT_TEXT00";
char result01[] = "RESULT_TEXT01";
char result02[] = "RESULT_TEXT02";

int i = 1;
while (i < 5) {

string path00;
cin >> path00;

if (path00 == "example1") {
cout << result00 << endl;

string path01;
cin >> path01;

if (path01 == "example2") {
cout << result01 << endl;

string path02;
cin >> path02;

if (path02 == "example3) {
cout << result02 << endl;
Sleep(30);
}

else {
cout << "Not a keyword." << endl;
int i = 0;
}
}
}```

最佳答案

听起来您可能想要以不同方式构建您存储游戏世界的方式。树可能是一个不错的选择,这样您就可以简单地存储对您在树中所处位置的引用。

这是一个示例实现,它可以让用户在不同的“房间”之间来回走动,并且在操作未知时不会丢失位置:

#include <map>
#include <string>
#include <iostream>

struct Room {
std::map<std::string, Room*> actions;
std::string description;
};

Room *buildRooms() {
Room *startingRoom = new Room();
startingRoom->description = "You are in a dimly lit room. You see doors to the north and south";

Room *northRoom = new Room();
northRoom->description = "You are in a closet. There is a door to the south";
northRoom->actions["south"] = startingRoom;
startingRoom->actions["north"] = northRoom;

Room *southRoom = new Room();
southRoom->description = "You are in a field. There is a building with a door to the north.";
southRoom->actions["north"] = startingRoom;
startingRoom->actions["south"] = southRoom;

return startingRoom;
}

void playGame() {
Room *currentRoom = buildRooms();

while(currentRoom != nullptr) {
std::cout << currentRoom->description << std::endl;

std::string userInput;
std::cin >> userInput;

Room *nextRoom = currentRoom->actions[userInput];

if(nextRoom == nullptr) {
std::cout << "Not a keyword" << std::endl;
} else {
currentRoom = nextRoom;
}
}
}

重要的区别在于您所在的位置(指向 currentRoom 的指针)存储在游戏循环之外。

对于延迟打印效果,您可以编写一个函数,它接受一个字符串并在字符之间延迟打印出来,然后在您想要该文本效果的任何地方使用它代替 std::cout。

例如:

void say(std::string text) {
for(unsigned int i = 0; i < text.length(); i++) {
std::cout << text.at(i);
Sleep(30);
}
}

关于c++ - 重复和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55887135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com