当前位置: 首页 > 知识库问答 >
问题:

在冒险游戏中检查空房间?

裴姚石
2023-03-14

我正在为一个C项目做一个冒险游戏。目标基本上是拥有一组由类定义的房间。然后使用地图将它们链接在一起。除了检查是否没有房间和确保没有为空房间接受输入之外,我的一切都正常工作。例如,它会说“那里什么都没有”,然后重新提示用户移动的方向。当前,如果某个方向上没有空间,并且选择了该方向,我的程序就会崩溃。我现在已经设置好了它,至少确保输入了一个有效的方向(北、南、东或西),但它没有检查这个方向是否可用。有人知道这样做的好方法吗?

main.cpp

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

    #include "room.h"

    using namespace std;

    int main()
    {
        /* ---------- Variable Declerations ---------- */
        string exitChoice;

        /* ---------- Room Intialization ---------- */
        room *kitchen = new room("Kitchen", "You are in the Kitchen. Pots and pans dangle above your head as you look across the room.");
        room *diningRoom = new room("Dining Room", "You are in the Dining Room. You see a large table in the center of the room complete with a set of chairs. It seems no one has ate here in quite som time.");
        room *garage = new room("Garage", "You are in the Garage. There are tools spread across the concerte floor complete with a Jeep Grand Cherokee on jack stands.");
        room *masterBed = new room("Master Bed Room", "You are in the Bed Room. A large Master Bed greets you as you walk into the room. You can see a large master bath as weel in the backround");
        room *hallway = new room("Hallway", "You are in the Hallway. A large set of stairs leads to the second floor, complete with a set to the basement. You also see a grand front door.");
        room *familyRoom = new room("Family Room", "You are in the Family Room. You see a dark leather couch in front of you as well as a brand new LCD TV. It aappears South Park is on TV.");
        room *bathRoom = new room("Bath Room", "You are in the Bath Room. A small room containing just a toilet is in front of you.");
        room *frontLawn = new room("Front Lawn", "You are in the Front Lawn. You are on a pathway and observe freshly cut grass as well as several trees scattered across the yard.");
        room *backLawn = new room("Back Lawn", "You are in the Back Lawn. You see 'Spot' running around chasing a tennis ball, as well as his dog house. A large wooden fence keeps him in the yard.");

        /* ----------Room Links---------- */

        /* Kitchen */
        kitchen->link(diningRoom, "North");
        kitchen->link(garage, "East");
        kitchen->link(masterBed, "South");
        kitchen->link(hallway, "West");

        /* Dining Room */
        diningRoom->link(kitchen, "South");
        diningRoom->link(familyRoom, "West");

        /* Master Bed Room */
        masterBed->link(kitchen, "North");
        masterBed->link(bathRoom, "West");

        /* Garage */
        garage->link(kitchen, "West");
        garage->link(backLawn, "East");

        /* Back Lawn */
        backLawn->link(garage, "West");

        /* Family Room */
        familyRoom->link(diningRoom, "East");
        familyRoom->link(hallway, "South");

        /* Hallway */
        hallway->link(familyRoom, "North");
        hallway->link(kitchen, "East");
        hallway->link(bathRoom, "South");
        hallway->link(frontLawn, "West");

        /* Front Lawn */
        frontLawn->link(hallway, "East");

        /* Bath Room */
        bathRoom->link(hallway, "North");
        bathRoom->link(masterBed, "East");

        /* ----------Gameplay---------- */
        room *currentRoom = kitchen;

        while (exitChoice != "quit")
        {
            currentRoom->printRoom();
            cout << endl;

            currentRoom->printLiked();

            cout << "Which exit? (Or 'quit'):";
            cin >> exitChoice;

            if(exitChoice != "quit" && exitChoice != "North" && exitChoice != "South" && exitChoice != "East" && exitChoice != "West")
            {
                cout << "Invalid Entry!" << endl;
                cout << "Which exit? (Or 'quit'):";
                cin >> exitChoice;
            }

            cout << "You move to the " << exitChoice << "..." << endl;
            currentRoom->getLinked(exitChoice);

            currentRoom = currentRoom->getLinked(exitChoice);
        }
    }

房间H

#ifndef ROOM_H
#define ROOM_H

using namespace std;

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

class room
{
    private:
        string name;
        string description;

    public:

    /* Constructor Prototypes */
    room(string, string);
    room(string);

    /* Get Name */
    string getName()
    {
        return name;
    }

    /* Get Description */
    string getDescription()
    {
        return description;
    }

    /* Print Room Information */
    void room :: printRoom()
    {
        cout << "--" << getName() << "--" << endl;
        cout << getDescription() << endl;
    }

    /* Map */
    map<string, room*> exits;

    /* Link Function*/
    void link(room *room, string direction)
    {
        exits[direction] = room;
    }

    /* Print Linked Rooms */
    void printLiked()
    {
        map<string, room*> :: iterator it;

        cout << endl;

        for(it = exits.begin(); it != exits.end(); ++it)
        {
            cout << "Exit: ";
            cout << it->first << " (" << it->second->getName() << ")" << endl;

        }

        cout << endl;
    }

    /* Get linked room */
    room* getLinked(string direction)
    {
        map<string, room*> :: iterator it;

        it = exits.find(direction);

        if(it != exits.end())
        {
            return it->second;
        }
        else
        {
            return NULL;
        }


    }





};

#endif

房间cpp

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

    using namespace std;

    #include "room.h"

        /* Constructor with Name and Description */
        room :: room(string _name, string _description)
        {
            name = _name;
            description = _description;
        }


        /* Contrsuctor with Name */
        room :: room(string _name)
        {
            name = _name;
        }

新干线。雪碧

        room* possibleNewRoom = currentRoom->getLinked(exitChoice);

        if (possibleNewRoom != 0)
        {
            currentRoom = possibleNewRoom;

            cout << "You move to the " << exitChoice << "..." << endl;
            currentRoom->getLinked(exitChoice);

            currentRoom = currentRoom->getLinked(exitChoice);
        }
        else
        {
            cout << "There are no exits in that direction." << endl;
        }

共有2个答案

薄哲
2023-03-14

最常见的方法是将定向成员引入房间类:

class Room {
    // ...
    Room *north, *south, *east, *west;
    // ...
    Room() : north(NULL), south(NULL), east(NULL), west(NULL)
    { /* ... */ }
};

然后像这样将房间连接起来:

Room* kitchen = new Room // ...
Room* hallway = new Room // ...
kitchen->north = hallway;
hallway->south = kitchen;

当您尝试向某个方向移动时,只需检查该方向的成员是否为NULL

// Player wants to go north.
if (this->north == NULL) {
    // Tell the player that's not possible.
} else {
    // OK, we can go to this->north.
}

此设置将允许您设置更高级的连接。例如,如果再向北再向南不应该把你带到你开始的地方(可能是一个神奇的门户或是其中的一些东西)

通过查看TADS(文本冒险开发系统),您可以了解一些关于如何设计它的设计思想。

殳宸
2023-03-14

您需要:

room* possibleNewRoom = currentRoom->getLinked(exitChoice);

if (possibleNewRoom != 0)
{
    currentRoom = possibleNewRoom;
}
else
{
    std::cout << "Ouch - you cannot move that way" << std::endl;
}
 类似资料:
  • 在我的游戏中有几个类我写过,包括房间,灯,胸,爪哇,玩家,钥匙和地图。这些都经过了测试,并且是正确的,所以现在我正在编写我的adventure类,它是程序的驱动程序。我需要设置球员的房间位置[0][0],但我不知道怎么做。这是我到目前为止在我的房间和冒险课。

  • 我目前正在创建一个冒险游戏,你通过输入命令(北,南)通过一个地牢的房间移动。所以我试图用字典把每个房间连接在一起,这样我就可以用“北”、“南”等键来移动这些房间。但我似乎不明白。有什么想法吗?

  • 所以我是Java编码的新手,我对C#有很好的经验,我知道它们非常相似。我目前正在通过创建一个文本冒险游戏来处理Java,游戏使用案例(案例1、案例2、默认等),目前我正在开发一个保存和加载功能,但我不知道如何保存一个使用案例来进一步编码的分支游戏,有人有什么想法吗?

  • 问题内容: 我对Java中的一些中间概念还很陌生。最近,我制作了一款名为DazzleQuest的文字冒险游戏,该游戏完全在开发者控制台/终端中运行。它以我的朋友为角色,因此我想向他们展示它,并通过将命令行和控制台的输出功能转移到一个简单的Swing界面(包括一个用于显示游戏输出的a和一个带有,处理来自用户的命令。 我的主类包含名为和的方法,我认为我需要将其与我的类及其子类[扩展] 集成在一起。 总

  • 我正在Java制作一个基于文本的冒险游戏。我需要让用户能够拿起物品并将其放入库存中,但我不确定如何做到! 这是我的项目当前的设置方式: 我需要能够在某些房间里有特定的物品。有人有什么建议吗?

  • 本文向大家介绍基于javascript实现泡泡大冒险网页版小游戏,包括了基于javascript实现泡泡大冒险网页版小游戏的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了一个很有趣的网页版游戏,有点类似金山打字游戏的青蛙过河,供大家参考,具体内容如下 效果图: 实现思路: 益智类小游戏,主要练习打字能力,基于jq开发。 1.在输入框输入泡泡对应文字,点击enter提交 2.与泡泡文字