我正在开发一个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);
}
}
room.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
room.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;
}
新的main.cpp snipet
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;
}
对于连接的房间有单独的命名成员会使编写广义算法变得不那么方便,如*“对于所有连接的房间,请执行X”*。从广义方向枚举(NORTH,SOUTH,EAST,WEST,UP LADDER,UP ROPE,DOWN MANHOLE,NORTHEAST,[DENNIS](http://www.hrwiki.org/wiki)获得某种地图会更好。/Thy_Dungeonman)等) – HostileFork
感谢您的回复。如果项目进一步发展,我可以看到这项工作非常成功,但考虑到我对项目的限制,Keith的答案效果更好。 – malibubts
@HostileFork我不同意这一点。 “UP LADDER”不是一个方向。这是一个行动。并没有地图应该参与其中。当游戏处理“UP LADDER”时,它应该简单地将玩家移动到另一个位置。一般的主要方向属性可以处理简单的“GO UP”命令,而不是“CLIMB LADDER”。 –