-1

我基本上想说,如果当前房间包含我在下面分配的钥匙,那么告诉用户,房间有钥匙,其余的我会弄清楚。

int main()
{

    Room mainHall;
    Room sittingRoom;

    mainHall.setNorthExit(&sittingRoom);
    mainHall.setEastExit(&playRoom);

    mainHall.setDescription("The main hall has 2 exits, one to the north, one to the east");
    mainHall.setName("Main Hall");

    sittingRoom.setEastExit(&kitchen);
    sittingRoom.setSouthExit(&mainHall);

    sittingRoom.setDescription("The sitting room has 2 exits, one to the east and one to the south");
    sittingRoom.setName("Sitting Room");

    string userInput;

    Room* currentRoom;
    currentRoom = &mainHall;

    Keys& key1 = playRoom.getKey();
    key1.setName("Sitting Room");

    Keys& key2 = sittingRoom.getKey();
    key2.setName("Play Room");

    if(////need code here) ///I want it to be something like...if(currentRoom "has a key"
    {
        string takeKey;
        cout << "There is still a key in this room, would you like to take it? " << endl;
        cin >> takeKey;
        if(takeKey == "yes")
        {
               //Havent assigned anything yet
        }
    }

这只是我的 keye 类的 cpp 文件,以防万一你需要它

#include"Keys.h"
#include<iostream>

Keys::Keys()
{
    name = "";
}

Keys::Keys(string nameParam)
{
    name = nameParam;
}

void Keys::setName(string nameParam)
{
    name = nameParam;
}

string Keys::getName() const
{
    return name;
}

void Keys::setKey(string keyParam)
{
    key = keyParam;
}

string Keys::getKey() const
{
    return key;
}

以及我的房间类中的getter方法

Keys& Room::getKey()
{
    return key;
}

我会很感激任何帮助

4

1 回答 1

0

房间类上的“HasKey()”方法怎么样,然后使用

if( sittingRoom.HasKey()) { ... }

在房间类中使用布尔值来确定房间是否有钥匙。

于 2013-04-23T21:14:41.207 回答