注册 登录
编程论坛 C++教室

一个古堡救美的文字游戏

野人王 发布于 2008-04-03 21:27, 4582 次点击
古堡救美游戏

描述
一个命令行游戏,让玩家在一个有很多楼层和很多房间的古堡中探险,玩家的任
务是找到公主并带她离开古堡。房间有很多种,不同种类的房间有不同的出口,
有的房间里有怪物。如果遇到怪物,游戏就失败了。
游戏启动时,玩家位于古堡一楼的大厅,程序显示大厅的信息:房间的名字、有
几个出口、每个出口的名字(如"east"、"south"、"up")。
    Welcome to the lobby. There are 3 exits as: east, west and up.
    Enter your direction to go:
然后玩家可以输入出口的名字来进入那个出口联接的房间,如
    east
则游戏进入那个房间,程序显示那个房间的信息,如大厅一样。玩家则可以继续
选择下一个房间。
一旦进入有怪物的房间,则显示怪物的信息并结束游戏。
一旦进入有公主的房间,则显示公主的信息,玩家还必须寻找出去的路直到大
厅,然后从大厅寻找出口离开古堡。
所有的显示信息和用户输入都采用英文,以简化程序。

要求
1. 至少三种不同种类的房间;
2. 至少五个房间;
3. 怪物和公主所在的房间是随机的。
一下是我写的代码,但是在编译play_game.cpp的时候出现错误:
Compiling...
paly_game.cpp
E:\编程\gubaojiumei\paly_game.cpp(221) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

paly_game.obj - 1 error(s), 0 warning(s)
望牛人帮忙改下错,不胜感激;
20 回复
#2
野人王2008-04-03 21:28
//room.h

//build the class of room
//room header that prevents re-definition
#ifndef ROOM_H
#define ROOM_H
#include <string>
using namespace std;
class room
{
protected:
    string name;      //the name of the room
    int exits_num;    // the number of the exits
    string exits[6];  // store the exits
public:
    room();      //
    ~room();
    void getname(string name);
    virtual void set_exits();
    void show();
    friend void play_game();
};
#endif
#3
野人王2008-04-03 21:28
//room1.h

#ifndef ROOM1_H
#define ROOM1_H
#include "room.h"
class room1:public room
{
public:
    room1()
    {
        getname("lobby");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 2;
        exits[0] = "east";
        exits[1] = "south";
    }
    friend void play_game();
};
#endif
#4
野人王2008-04-03 21:31
//room2.h

#ifndef ROOM2_H
#define ROOM2_H
#include "room.h"
class room2:public room
{
public:
    room2()
    {
        getname("sunroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 2;
        exits[0] = "north";
        exits[1] = "up";
    }
    friend void play_game();
};
#endif

//room3.h

#ifndef ROOM3_H
#define ROOM3_H
#include "room.h"
class room3:public room
{
public:
    room3()
    {
        getname("WC");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 1;
        exits[0] = "north";
    }
    friend void play_game();
};
#endif

//room4.h

#ifndef ROOM4_H
#define ROOM4_H
#include "room.h"
class room4:public room
{
public:
    room4()
    {
        getname("rosegarden");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 3;
        exits[0] = "south";
        exits[1] = "west";
        exits[2] = "up";
    }
    friend void play_game();
};
#endif
#5
野人王2008-04-03 21:32
//room5.h
#ifndef ROOM5_H
#define ROOM5_H
#include "room.h"
class room5:public room
{
public:
    room5()
    {
        getname("storeroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 2;
        exits[0] = "east";
        exits[1] = "south";
    }
    friend void play_game();
};
#endif
#6
野人王2008-04-03 21:32
//room6.h

#ifndef ROOM6_H
#define ROOM6_H
#include "room.h"
class room6:public room
{
public:
    room6()
    {
        getname("funroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 3;
        exits[0] = "east";
        exits[1] = "west";
        exits[2] = "down";
    }
    friend void play_game();
#endif
#7
野人王2008-04-03 21:33
//room7.h
#ifndef ROOM7_H
#define ROOM7_H
#include "room.h"
class room7:public room
{
public:
    room7()
    {
        getname("babyroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 2;
        exits[0] = "west";
        exits[1] = "north";
    }
    friend void play_game();
};
#endif
#8
野人王2008-04-03 21:33
#ifndef ROOM8_H
#define ROOM8_H
//room8.h
#include "room.h"
class room8:public room
{
public:
    room8()
    {
        getname("readroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 2;
        exits[0] = "south";
        exits[1] = "north";
    }
    friend void play_game();
};
#endif
#9
野人王2008-04-03 21:34
//room9.h

#ifndef ROOM9_H
#define ROOM9_H
#include "room.h"
class room9:public room
{
public:
    room9()
    {
        getname("readroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 3;
        exits[0] = "south";
        exits[1] = "west";
        exits[2] = "down";
    }
    friend void play_game();
};
#endif
#10
野人王2008-04-03 21:35
//room.cpp

#include <iostream>
#include "room.h"
using namespace std;

room::room()
{

}

room::~room()
{

}
 void room::getname(string name)  //get the name of the room
{
    this->name = name;
}


void room::set_exits()  //get the exits of the room
{
    exits_num = 0;
}

void room::show()   //to show the information of the room
{
    int i;
    cout << "Welcome to the " << name << " ! There are " << exits_num <<" exits as:";
    for (i = 0; i < exits_num; i++)
        cout << exits[i] << ' ';
    cout << endl;
    cout << "Enter your direction to go:" << endl;
}
#11
野人王2008-04-03 21:35
//rand.cpp

//get the stochastic number
#include <iostream>
#include <ctime>
using namespace std;
int Rand()
{
    int a;
    srand( (unsigned)time(NULL) );
    a = rand() % 9 + 1;
    return a;
}
#12
野人王2008-04-03 21:36
//paly_game.cpp

#include <iostream>
#include "room1.h"
#include "room2.h"
#include "room3.h"
#include "room4.h"
#include "room5.h"
#include "room6.h"
#include "room7.h"
#include "room8.h"
#include "room9.h"

void play_game()
{
    int princess,bugbear;
    int m = 1;                  //remark the room number
    string direct;
    room *pt;
    int Rand();
    princess = Rand();    //get the room number which the princess in
    do
    {
        bugbear = Rand();       //get the room number which the bugbear in
    }while(bugbear == princess); //and the bugbear is not in the same room with princess
    while (1)
    {
        if ( m == princess)   //you get the princess and you win
        {
            cout << "Here is the princess and you extricate her!" << endl;
            cout << "*******************************************" << endl;
            cout << "* win && The princeess falls love in you..." << endl;
            cout << "*******************************************" << endl;
            exit(1);
        }
        if (m == bugbear)  // the bugbear kills you and you lost
        {
            cout << "Here is the bugbear and you are killed!" << endl;
            cout << "*************GAME***OVER***************" << endl;
            exit(0);
        }
        switch(m)
        {
        case 1:    // you are in the
            pt = new room1;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "east")
                    m = 4;
                else if (direct == "south")
                          m = 2;
                     else
                     {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 2:
            pt = new room2;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "north")
                    m = 1;
                else if (direct == "up")
                          m = 6;
                     else
                         {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            break;
        case 3:
            pt = new room3;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "north")
                    m = 4;
                else
                    {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 4:
            pt = new room4;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "south")
                    m = 3;
                else if (direct == "west")
                         m = 1;
                    else if (direct == "up")
                             m = 9;
                         else
                             {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 5:
            pt = new room5;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "east")
                    m = 9;
                else if (direct == "south")
                          m = 6;
                     else
                          {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 6:
            pt = new room6;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "east")
                    m = 7;
                else if (direct == "north")
                          m = 5;
                    else if (direct == "down")
                            m = 2;
                        else
                            {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 7:
            pt = new room7;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "west")
                    m = 6;
                else if (direct == "north")
                          m = 8;
                     else
                     {
                         m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 8:
            pt = new room8;
            pt->show();
            room8.show();
            do
            {
                cin >> direct;
                if ( direct == "south")
                    m = 7;
                else if (direct == "north")
                          m = 9;
                     else
                     {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                     }
            }while(m == 0);
            delete pt;
            break;
        case 9:
            pt = new room9;
            pt->show();
            do
            {
                cin >> direct;
                if ( direct == "south")
                    m = 8;
                else if (direct == "west")
                          m = 5;
                    else if (direct == "down")
                            m = 4;
                        else
                        {
                          m = 0;
                          cout << "The direction is wrong!" << endl;
                          cout << "Please enter your direction to go:" << endl;
                        }
            }while(m == 0);
            delete pt;
            break;
        }
   }
}
#13
野人王2008-04-03 21:37
//main.cpp

#include <iostream>
#include "room1.h"
#include "room2.h"
#include "room3.h"
#include "room4.h"
#include "room5.h"
#include "room6.h"
#include "room7.h"
#include "room8.h"
#include "room9.h"

int main()
{
    void play_game();
    play_game();
    system("PAUSE");
    return 0;
}

[[it] 本帖最后由 野人王 于 2008-4-3 21:38 编辑 [/it]]
#14
zjl1382008-04-03 23:37
根据错误提示应该是少了大括号,你仔细查一下吧!
#15
野人王2008-04-04 14:06
括号我都检查了好几遍了
#16
zjl1382008-04-04 14:42
//room6.h

#ifndef ROOM6_H
#define ROOM6_H
#include "room.h"
class room6:public room
{
public:
    room6()
    {
        getname("funroom");
        set_exits();
    }
    void set_exits()
    {
        exits_num = 3;
        exits[0] = "east";
        exits[1] = "west";
        exits[2] = "down";
    }
    friend void play_game();
};/////////////////////////////////////////////////这里少!!!!!
#endif
#17
zjl1382008-04-04 14:45
在//paly_game.cpp中
case 8:

            pt = new room8;
            pt->show();
            room8.show();//////////////////////明显多了这句///////////
#18
zjl1382008-04-04 14:46
你自已一下看符不符合你要求,我也要仔细研究一下这代码,写得很不错,呵呵!!!
#19
野比2008-04-05 04:21
记得很早以前有个朋友在这里问了关于做R PG的问题,和这个差不多。当时我帮他想的是做任意房间怪物的,但是还很不完善,研究下你的代码。
P.S.可不可以打个包放在1楼呢?那样会方便很多的。3Q
#20
hanpengqd2008-07-08 15:16
研究中
#21
hanpengqd2008-07-09 11:24
哈哈,仿写了一个,效果还不错
谢谢楼主!
1