zork项目

这是一个关于ZorkUL游戏的实现,包括Command类的使用,用于处理用户输入的命令。游戏中的房间和物品管理,以及导航功能如'go'和'teleport'的实现被详细描述。当用户输入未知命令时,游戏会提示无效输入。此外,游戏还提供了帮助菜单和地图显示。
摘要由CSDN通过智能技术生成

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();
        }
    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python因其简洁易读的语法和丰富的库支持,非常适合用来开发各种类型的游戏,无论是简单的命令行游戏、桌面应用、2D/3D图形界面游戏,还是基于网络的多人游戏。下面是一些用Python编写的游戏示例: 1. **文本冒险游戏(Text-based adventure games)**: 如《Zork》系列,玩家通过交互式的命令行输入探索游戏世界。Python库如**parser**或**argparse**可以帮助处理玩家输入。 2. **棋盘游戏(Chess and other board games)**: 如使用**pygame**库开发的简单版国际象棋,它提供了创建游戏界面和处理棋子移动的基本功能。 3. **2D游戏(如Flappy Bird风格)**: **Pygame**是一个流行的选择,可以构建基础的2D游戏框架,比如使用**Panda3D**或**Kivy**创建更复杂的游戏。 4. **像素艺术游戏(Pixel art games)**: Python的游戏开发框架**Panda3D**适合做像素艺术风格的游戏。 5. **网络对战游戏(Multiplayer games)**: **socket programming**可以用于构建多人在线游戏,例如使用**Twisted**或**asyncio**进行网络通信。 6. **教育游戏(Educational games)**: Python的编程教学工具,如**Code.org**的项目,教授孩子们基础编程概念。 **相关问题--:** 1. Python中的pygame库主要用来做什么? 2. 如何利用Python构建网络对战游戏? 3. Python开发游戏时如何处理用户输入和游戏逻辑? 如果你对某个特定类型的Python游戏或者想学习如何开始,可以说详细一些,我会提供更具体的指导。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值