我们需要编写一款小型游戏(Game),该游戏包括了一幅平面地图(Map)和若干个游戏对象(GameObject)。
而游戏对象分为人(Person)和动物(Animal)。动物还包括兔子(Rabit)、小狗(Dog)等多种。
这款游戏的规则包括:
(1)地图为一个有宽度(Width)和高度(Height)的平面。
(2)人和动物都可以在地图上按照Right、Left、Up、Down四个方向移动(Move),并且人和每种动物一次移动的步长(Step)是不同的, 但不能超出地图的宽高。
(3)人可以将动物领养(Raise)成自己的宠物(Pets),一次只能领养1只动物。同时可以通过喂养(Feed)宠物一些食物(Food,包括蔬菜“Vegetables”和肉“Meats”)使得动物长大,并使得动物的成长值(GrowthValue)按照不同食物的卡路里(Calories)累加计算。如蔬菜的计算公式:“log10(calogies) + 2*calories”,肉的计算公式:“calories * calories + 10 * calories”。
(4)整个游戏在程序运行期间只能被初始化一次,即在主函数中只能实 例化一个游戏对象。
(5)地图可以有多种初始化方式,其中标准地图的构建是在地图上放置若干个人和动物对象即可,但是移动地图的构建除了在地图上放置若
干个人和动物对象之外,还可以在放置这些游戏对象的时候将其按照用户指定的方向移动一个步长。
下面是主函数文件代码:
#include "stdafx.h"
void InputMove(CGameObject& obj, Direct& dir)
{
cout << "Please input moving direct of " <<
typeid(obj).name() << " "
<< obj.GetID() << " :" << endl <<
" 1. Right " << endl <<
" 2. Left " << endl <<
" 3. Up " << endl <<
" 4. Down " << endl <<
"(1~4): ";
int x; cin >> x;
dir = (Direct)x;
}
int main(int argc, char* argv[])
{
CGame& game = CGame::GetGame();
CMap* map = 0;
cout <<
"Would you like which map be built?" << endl <<
" 1. Standard Map " << endl <<
" 2. Move Map " << endl <<
"Please input 1 or 2: ";
int x; cin >> x;
if(x == 1)
{
CStandardMapBuilder builder;
map = &game.CreateMap(builder);
}
else
{
CMoveMapBuilder builder;
builder.SetOnBuilding(InputMove);
map = &game.CreateMap(builder);
}
if(map)
{
cout <<
"Please input the object's id: ";
int id; cin >> id;
while(id >= 0 && id < CGameObject::GetCount())
{
CGameObject* gobj = map->GetGameObject(id);
if(gobj){
string typeName = typeid(*gobj).name();
cout <<
" " << typeid(*gobj).name() << endl <<
" X: " << gobj->GetX() << endl <<
" Y: " << gobj->GetY() << endl <<
" V: ";
if(typeName.find("CPerson") == string::npos)
{
CAnimal* animal = (CAnimal*)gobj;
cout << animal->GetGrowthValue() << endl;
}
else
{
cout << -1.0 << endl;
CPerson* p = (CPerson*)gobj;
CAnimal* pets = &(p->GetPets());
if(pets)
{
cout << " Feed:" << endl <<
" 1. Vegetables" << endl <<
" 2. Meats" << endl <<
" Please choose food: ";
int fid; cin >> fid;
if(fid == 1)
{
CVegetables food;
p->FeedPets(food);
}
else
{
CMeat food;
p->FeedPets(food);
}
}
else
{
cout << " Raise pets'id: ";
int pid; cin >> pid;
CAnimal* pets = map->GetAnimal(pid);
if(pets)
{
p->RaisePets(*pets);
}
}
}
}
cout <<"Please input the object's id: ";
cin >> id;
}
}
return 0;
}
求服务代码!