在冒险游戏中检查空房间
Checking for Empty Rooms in an Adventure Game?
我正在为一个C++项目开发冒险游戏。目标基本上是拥有一堆由类定义的房间。然后使用地图将它们全部链接在一起。除了检查是否有空间并确保没有为空房间输入之外,我什么都工作。例如,它会说"那里什么都没有",然后重新要求用户移动方向。目前,如果某个方向上没有空间并且选择了该方向,我的程序就会崩溃。我目前已对其进行设置,至少确保输入了有效的方向(北、南、东或西),但它甚至没有检查该方向是否可用。有人知道一个好方法吗?
主.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;
}
新主.cpp狙击
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;
}
您需要:
room* possibleNewRoom = currentRoom->getLinked(exitChoice);
if (possibleNewRoom != 0)
{
currentRoom = possibleNewRoom;
}
else
{
std::cout << "Ouch - you cannot move that way" << std::endl;
}
执行此操作的最常见方法是将定向成员引入 Room 类:
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(文本冒险开发系统)来获取一些有关如何设计的设计想法。
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- C++模板来检查友元函数的存在
- boost::进程间消息队列引发错误
- 检查输入是否不是整数或数字
- 试图让变量检查数组中的某些内容
- 检查值是否在集合p1和p2中,但不在p3中
- C++概念:如何使用'concept'检查模板化结构的属性?
- 概念TS检查忽略私有访问修饰符
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- 如何在C++中检查2D数组中负值的输入验证
- C++:正在检查LinkedList中的回文-递归方法-错误
- 使用for循环检查数组中的重复项
- 如何检查一个c++字符串中有多少相同的字符/数字
- 检查不带转换的扫描格式
- 如何检查线程是否锁定
- 清除前检查矢量
- 如何处理来自核心指南检查器的关于gsl::at的静态分析警告
- 检查"提升进程间managed_shared_memory"是否已存在?
- 提高进程间互斥和检查放弃