1. vector的使用
push_back( 参数 ) :在vector最后新添加一个元素
command.h
#ifndef COMMAND_H_
#define COMMAND_H_
#include <string>
using namespace std;
class Command {
private:
string commandWord;
string secondWord;
public:
Command(string firstWord, string secondWord);
string getCommandWord();
string getSecondWord();
bool isUnknown();
bool hasSecondWord();
void setSecondWord(string);
};
#endif /*COMMAND_H_*/
zork.h
#ifndef ZORKUL_H_
#define ZORKUL_H_
#include "Command.h"
#include "Parser.h"
#include "Room.h"
#include "item.h"
#include <iostream>
#include <string>
using namespace std;
class ZorkUL {
private:
Parser parser;
Room *currentRoom;
void createRooms();
void printWelcome();
bool processCommand(Command command);
void printHelp();
void goRoom(Command command);
void goRandom(Command command);
void goDirection1(Command command,string);
void goDirection(Command command,string);
void createItems();
void displayItems();
public:
ZorkUL();
void play();
string go(string direction);
};
#endif /*ZORKUL_H_*/
command.cpp
#include "Command.h"
#include <cstdlib>
/**
* Create a command object. First and second word must be supplied, but
* either one (or both) can be null. The command word should be null to
* indicate that this was a command that is not recognised by this game.
*/
Command::Command(string firstWord, string secondWord) {
this->commandWord = firstWord;
this->secondWord = secondWord;
}
/**
* Return the command word (the first word) of this command. If the
* command was not understood, the result is null.
*/
string Command::getCommandWord() {
return this->commandWord;
}
/**
* Return the second word of this command. Returns null if there was no
* second word.
*/
string Command::getSecondWord() {
return this->secondWord;
}
void Command::setSecondWord(string a){
this->secondWord=a;
}
/**
* Return true if this command was not understood.
*/
bool Command::isUnknown() {
return (commandWord.empty());
}
/**
* Return true if the command has a second word.
*/
bool Command::hasSecondWord() {
return (!secondWord.empty());
}
zorkul.cpp
#include <iostream>
using namespace std;
#include "ZorkUL.h"
int main() {
ZorkUL temp;
temp.play();
return 0;
}
ZorkUL::ZorkUL() {
createRooms();
}
void ZorkUL::createRooms() {
Room *a, *b, *c, *d, *e, *f, *g, *h, *i,*m;
a = new Room("a");
a->addItem(new Item("x", 1, 11));
a->addItem(new Item("y", 2, 22));
b = new Room("b");
b->addItem(new Item("xx", 3, 33));
b->addItem(new Item("yy", 4, 44));
//新加房间room:m,新加武器xxx,yyy
m = new Room("m");
m->addItem(new Item("xxx",5,55));
m->addItem(new Item("yyy",6,66));
c = new Room("c");
d = new Room("d");
e = new Room("e");
f = new Room("f");
g = new Room("g");
h = new Room("h");
i = new Room("i");
// (N, E, S, W)
a->setExits(f, b, d, c);
b->setExits(NULL, NULL, NULL, a);
c->setExits(NULL, a, NULL, NULL);
//改变房间d的方向
d->setExits(a, e, m, i);
e->setExits(NULL, NULL, NULL, d);
f->setExits(NULL, g, a, h);
g->setExits(NULL, NULL, NULL, f);
h->setExits(NULL, f, NULL, NULL);
i->setExits(NULL, d, NULL, NULL);
//设置m房间的方向
m->setExits(d,NULL,NULL,NULL);
//当下所处房间位置
currentRoom = a;
}
/**
* Main play routine. Loops until end of play.
*/
void ZorkUL::play() {
printWelcome();
// Enter the main command loop. Here we repeatedly read commands and
// execute them until the ZorkUL game is over.
bool finished = false;
while (!finished) {
// Create pointer to command and give it a command.
Command* command = parser.getCommand();
// Pass dereferenced command and check for end of game.
finished = processCommand(*command);
// Free the memory allocated by "parser.getCommand()"
// with ("return new Command(...)")
delete command;
}
cout << endl;
cout << "end" << endl;
}
void ZorkUL::printWelcome() {
cout << "start"<< endl;
cout << "info for help"<< endl;
cout << endl;
cout << currentRoom->longDescription() << endl;
}
/**
* Given a command, process (that is: execute) the command.
* If this command ends the ZorkUL game, true is returned, otherwise false is
* returned.
*/
bool ZorkUL::processCommand(Command command) {
if (command.isUnknown()) {
cout << "invalid input"<< endl;
return false;
}
string commandWord = command.getCommandWord();
if (commandWord.compare("info") == 0)
printHelp();
else if (commandWord.compare("map") == 0)
{
cout << "[h] --- [f] --- [g]" << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << "[c] --- [a] --- [b]" << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << "[i] --- [d] --- [e]" << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " [m] " << endl;
}
else if (commandWord.compare("go") == 0)
goRoom(command);
else if (commandWord.compare("take") == 0)
{
if (!command.hasSecondWord()) {
cout << "incomplete input"<< endl;
}
else
if (command.hasSecondWord()) {
cout << "you're trying to take " + command.getSecondWord() << endl;
int location = currentRoom->isItemInRoom(command.getSecondWord());
if (location < 0 )
cout << "item is not in room" << endl;
else
cout << "item is in room" << endl;
cout << "index number " << + location << endl;
cout << endl;
cout << currentRoom->longDescription() << endl;
}
}
else if (commandWord.compare("put") == 0)
{
}
//为teleport命令添加操作:跳跃到随机房间
/*
{
if (!command.hasSecondWord()) {
cout << "incomplete input"<< endl;
}
else
if (command.hasSecondWord()) {
cout << "you're adding " + command.getSecondWord() << endl;
itemsInRoom.push_Back;
}
}
*/
else if (commandWord.compare("quit") == 0) {
if (command.hasSecondWord())
cout << "overdefined input"<< endl;
else
return true; /**signal to quit*/
}
else if (commandWord.compare("teleport")==0){
cout << "you're trying to teleport to a random room" << endl;
goRandom(command);
}
return false;
}
/** COMMANDS **/
void ZorkUL::printHelp() {
cout << "valid inputs are; " << endl;
parser.showCommands();
}
void ZorkUL::goRoom(Command command) {
if (!command.hasSecondWord()) {
cout << "incomplete input"<< endl;
return;
}
string direction = command.getSecondWord();
// Try to leave current room.
Room* nextRoom = currentRoom->nextRoom(direction);
if (nextRoom == NULL)
cout << "underdefined input"<< endl;
else {
currentRoom = nextRoom;
cout << currentRoom->longDescription() << endl;
}
}
void ZorkUL::goDirection1(Command command,string a) {
command.setSecondWord(a);
string direction = command.getSecondWord();
// Try to leave current room.
Room* nextRoom = currentRoom->nextRoom(direction);
currentRoom = nextRoom;
}
void ZorkUL::goDirection(Command command,string a) {
command.setSecondWord(a);
string direction = command.getSecondWord();
// Try to leave current room.
Room* nextRoom = currentRoom->nextRoom(direction);
currentRoom = nextRoom;
cout << currentRoom->longDescription() << endl;
}
void ZorkUL::goRandom(Command command) {
int r= 1+rand()%9;
switch (r) {
case 1:goDirection(command,"east");break;
case 2:goDirection(command,"west");break;
case 3:goDirection(command,"south");break;
case 4:goDirection1(command,"south");goDirection(command,"east");break;
case 5:goDirection(command,"north");break;
case 6:goDirection1(command,"north");goDirection(command,"east");break;
case 7:goDirection1(command,"north");goDirection(command,"west");break;
case 8:goDirection1(command,"south");goDirection(command,"west");break;
default:goDirection1(command,"south");goDirection(command,"south");break;
}
}
string ZorkUL::go(string direction) {
//Make the direction lowercase
//transform(direction.begin(), direction.end(), direction.begin(),:: tolower);
//Move to the next room
Room* nextRoom = currentRoom->nextRoom(direction);
if (nextRoom == NULL)
return("direction null");
else
{
currentRoom = nextRoom;
return currentRoom->longDescription();
}
}