RPG游戏问题--连编译都通不过 T_T...

-水水- 发布于 2013/04/26 18:03
阅读 678
收藏 0

很早前在osc看到的RPG游戏,感觉很有意思,原文在这里http://www.oschina.net/code/snippet_105887_12467

我看源码只有 一种敌人,就想能不能随机选择一个敌人,但是代码有点问题(从大概60行开始),详见代码 这是主要部分,

#include <iostream>
#include <string>
using namespace std;

#include "swordsman.h"
#include "archer.h"
#include "mage.h"


int main()
{
	string tempName;
	bool success=0;		//flag for storing whether operation is successful
	cout <<"Please input player's name: ";
	cin >>tempName;		// get player's name from keyboard input
	player *human;		// use pointer of base class, convenience for polymorphism	
	int tempJob;		// temp choice for job selection

	srand((unsigned)time( NULL ) ); // generating random seed based on system time
	do
	{
		cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl;
		cin>>tempJob;
		system("cls");		// clear the screen
		switch(tempJob)
		{
		case 1:
			human=new swordsman(1,tempName);	// create the character with user inputted name and job
			success=1;		// operation succeed
			break;
		case 2:
			human = new archer(1,tempName);
			success = 1;
			break;
		case 3:
			human = new mage(1,tempName);
			success = 1;
			break;
		default:
			break;				// In this case, success=0, character creation failed
		}
	}while(success!=1);		// so the loop will ask user to re-create a character

	int tempCom;			// temp command inputted by user
	int nOpp=0;				// the Nth opponent
	for(int i=1;nOpp<5;i+=2)	// i is opponent's level
	{
		string Opp_name;
		int t=rand()%3;
		// 随机选定一个对手
		switch(t)
		{
			case 0: Opp_name="Swordsman";break;
			case 1: Opp_name="Archer";break;
			case 2: Opp_name="Mage";break;
			default: Opp_name="Mage";break;
		}

		nOpp++;
		system("cls");
		cout<<"STAGE" <<nOpp<<endl;
		cout<<"Your opponent, a Level "<<i<<" "<<Opp_name<<endl;
		system("pause");

		//if(Opp_name=="Swordsman")
			swordsman enemy(i, "Warrior");	// Initialise an opponent, level i, name "Warrior"
		//	else if(Opp_name=="Archer")
		//		archer enemy(i,"Warrior");
		//		else
		//			mage enemy(i,"Warrior");
			/* 问题就在上面的注释里,如果enemy每个单独生成 就可以运行成功,
				如果把if else的注释全去掉,连编译都过不了
				*/
		
		human->reFill();				// get HP/MP refill before start fight

		while(!human->death() && !enemy.death())	// no died
		{
			success=0;
			while (success!=1)
			{
				showinfo(*human,enemy);				// show fighter's information
				cout<<"Please give command: "<<endl;
				cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl;
				cin>>tempCom;
				switch(tempCom)
				{
				case 0:
					cout<<"Are you sure to exit? Y/N"<<endl;
					char temp;
					cin>>temp;
					if(temp=='Y'||temp=='y')
						return 0;
					else
						break;
				case 1:
					success=human->attack(enemy);
					human->isLevelUp();
					enemy.isDead();
					break;
				case 2:
					success=human->specialatt(enemy);
					human->isLevelUp();
					enemy.isDead();
					break;
				case 3:
					success=human->useHeal();
					break;
				case 4:
					success=human->useMW();
					break;
				default:
					break;
				}
			}
			if(!enemy.death())		// If AI still alive
				enemy.AI(*human);
			else							// AI died
			{
				cout<<"YOU WIN"<<endl;
				human->transfer(enemy);		// player got all AI's items
			}
			if (human->death())
			{
				system("cls");
				cout<<endl<<setw(50)<<"GAME OVER"<<endl;
				delete human;
				system("pause");
				return 0;
			}
		}
	}
	delete human;		
	system("cls");
	cout<<"Congratulations! You defeated all opponents!!"<<endl;
	system("pause");
	return 0;
}
加载中
0
-水水-
-水水-
晕死,怎么上传代码附件啊
-水水-
-水水-
好的 谢谢
红薯
红薯
代码分享里才能上传附件
0
cut
cut

弱弱问下LZ学过C++没,将那个if else去掉也能编译的才怪,都不知道为啥喜欢选剑人,那个是屌丝职业。虽然没看过那代码但看这段代码中,应该可以推出一个这样的关系

player是个抽象类,可能还有个ai的抽象类,而swordsman,archer,mage都继承了player,和ai那两个类,你想随机选敌人,就看看你选职业的那部分代码,一样的写法。不过从面向对象的角度来看,这样的类关系是有点问题,个人觉得这里用command那个设计模式来实现更好。

0
-水水-
-水水-

引用来自“cut”的答案

弱弱问下LZ学过C++没,将那个if else去掉也能编译的才怪,都不知道为啥喜欢选剑人,那个是屌丝职业。虽然没看过那代码但看这段代码中,应该可以推出一个这样的关系

player是个抽象类,可能还有个ai的抽象类,而swordsman,archer,mage都继承了player,和ai那两个类,你想随机选敌人,就看看你选职业的那部分代码,一样的写法。不过从面向对象的角度来看,这样的类关系是有点问题,个人觉得这里用command那个设计模式来实现更好。

抱歉,没有系统的学过,楼主现在大二,学过C/java,也经常写一些C++的代码,不过都是简单的,就是想试试面向对象的编程,但又不喜欢java,就试试c++,结果就写成这样了,

嗯,还是谢谢提醒,我再找找资料,争取完善

0
-水水-
-水水-

谢谢@cut  的建议,我搜了一下设计模式之commond 感觉不太懂,主要是面向对象还不是很了解,又考虑到,human也可以实现三个角色的转换,就把enemy也按照human的方式照搬了过来,改了几行代码,结果可以了;只改动了main.cpp 就是把 enemy改成player* 类型,以及相关的参数符号改变。(. 改为->  等等);  但是我还是希望有大神解释下上面代码 if else 错的原因

#include <iostream>
#include <string>
using namespace std;

#include "swordsman.h"
#include "archer.h"
#include "mage.h"


int main()
{
	string tempName;
	bool success=0;		//flag for storing whether operation is successful
	cout <<"Please input player's name: ";
	cin >>tempName;		// get player's name from keyboard input
	player *human;		// use pointer of base class, convenience for polymorphism	
	player *enemy;
	int tempJob;		// temp choice for job selection

	srand((unsigned)time( NULL ) ); // generating random seed based on system time
	do
	{
		cout <<"Please choose a job: 1 Swordsman, 2 Archer, 3 Mage"<<endl;
		cin>>tempJob;
		system("cls");		// clear the screen
		switch(tempJob)
		{
		case 1:
			human=new swordsman(1,tempName);	// create the character with user inputted name and job
			success=1;		// operation succeed
			break;
		case 2:
			human = new archer(1,tempName);
			success = 1;
			break;
		case 3:
			human = new mage(1,tempName);
			success = 1;
			break;
		default:
			break;				// In this case, success=0, character creation failed
		}
	}while(success!=1);		// so the loop will ask user to re-create a character

	int tempCom;			// temp command inputted by user
	int nOpp=0;				// the Nth opponent
	for(int i=1;nOpp<5;i+=2)	// i is opponent's level
	{
		string Opp_name;
		int t=rand()%3;
		// 随机选定一个对手
		switch(t)
		{
			case 0: Opp_name="Swordsman";
					enemy= new swordsman(i,"Warrior");// Initialise an opponent, level i, name "Warrior",job swordsman
					break;
			case 1: Opp_name="Archer";
					enemy= new archer(i,"Warrior");
					break;
			case 2: Opp_name="Mage";
					enemy= new mage(i,"Warrior");
					break;
			default: Opp_name="Mage";
					enemy= new mage(i,"Warrior");
					break;
		}

		nOpp++;
		system("cls");
		cout<<"STAGE" <<nOpp<<endl;
		cout<<"Your opponent, a Level "<<i<<" "<<Opp_name<<endl;
		system("pause");
			
		human->reFill();				// get HP/MP refill before start fight

		while(!human->death() && !enemy->death())	// no died
		{
			success=0;
			while (success!=1)
			{
				showinfo(*human,*enemy);				// show fighter's information
				cout<<"Please give command: "<<endl;
				cout<<"1 Attack; 2 Special Attack; 3 Use Heal; 4 Use Magic Water; 0 Exit Game"<<endl;
				cin>>tempCom;
				switch(tempCom)
				{
				case 0:
					cout<<"Are you sure to exit? Y/N"<<endl;
					char temp;
					cin>>temp;
					if(temp=='Y'||temp=='y')
						return 0;
					else
						break;
				case 1:
					success=human->attack(*enemy);
					human->isLevelUp();
					enemy->isDead();
					break;
				case 2:
					success=human->specialatt(*enemy);
					human->isLevelUp();
					enemy->isDead();
					break;
				case 3:
					success=human->useHeal();
					break;
				case 4:
					success=human->useMW();
					break;
				default:
					break;
				}
			}
			if(!enemy->death())		// If AI still alive
				enemy->AI(*human);
			else							// AI died
			{
				cout<<"YOU WIN"<<endl;
				human->transfer(*enemy);		// player got all AI's items
				
			}
			if (human->death())
			{
				system("cls");
				cout<<endl<<setw(50)<<"GAME OVER"<<endl;
				delete enemy;
				delete human;
				system("pause");
				return 0;
			}
		}
	}
	delete enemy;
	delete human;
	system("cls");
	cout<<"Congratulations! You defeated all opponents!!"<<endl;
	system("pause");
	return 0;
}
完整代码  RPG修改版(VS2010可运行)
0
cut
cut
if else那个其实是if (...){....}else{}的缩写形式,在里面定义的变量的域作用范围在if的{}里面,所以你在外面用肯定出问题。还有职业应设计成那样,走路,技能,那些特征行为。操作者分player和npc,分别通过发命令的方式控制职业对象进行操作。他们的关系是个比较弱的依赖关系,而不是像继承那种比较强的依赖关系。如果你游戏里npc除了敌对npc外还设置个中立npc,如果更复杂点,怪分一般怪,精英怪,boss等,你是不是还继续将那些都继承下职业再写。如果你用command的话就轻易解决那些问题。
cut
cut
回复 @水云天 : 那个我也不常用,平时不写c++只写c,面向对象还是好好学下java c#之类的吧,底层的学下C,C++是个四不像
-水水-
-水水-
谢谢,刚去图书馆借了本设计模式,感觉好多面向对象的还是不了解
返回顶部
顶部