角色扮演类游戏中添加随机函数
											使得游戏中随机出现三个设定好的角色做对手 程序代码:
程序代码:
//=======================
//        main.cpp
//=======================
// main function for the RPG style game
//主要功能为RPG游戏风格
#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
                       //临时工作选择的选择
    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);    // create the character with user inputted name and job
                                                 //创建角色与用户输入的姓名和工作
            success=1;        // operation succeed操作成功
            break;
            case 3:
            human=new mage(1,tempName);    // create the character with user inputted name and job
                                                 //创建角色与用户输入的姓名和工作
            success=1;        // operation succeed操作成功
            break;
        default:
            break;                // In this case, success=0, character creation failed
                                //在这种情况下,成功= 0,人物创建失败了
        }
    }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第n个对手
    for(int i=1;nOpp<5;i+=2)    // i is opponent's level     是对手的水平
    {srand(0);
for( int i = 0; i < 10; i++)
{
cout<<rand()<<' ';
}
        nOpp++;
        system("cls");
        cout<<"STAGE" <<nOpp<<endl;
        cout<<"Your opponent, a Level "<<i<<" Swordsman."<<endl;
        system("pause");
        swordsman enemy(i, "Warrior");    // Initialise an opponent, level i, name "Junior"
                                        //初始化一个对手,i  水平,name“初级”
        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如果ai还活着
                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;//6_???????????        // player is dead, program is getting to its end, what should we do here?玩家死了,结束程序,我们应该做些什么呢?
                system("pause");
                return 0;
            }
        }
    }
    delete human;//7_?????????            // You win, program is getting to its end, what should we do here?你赢了,程序结束,我们该怎么办呢?
    system("cls");
    cout<<"Congratulations! You defeated all opponents!!"<<endl;
    system("pause");
    return 0;
}
        
[local]1[/local]
							
	 game.rar
			(1.16 MB)
game.rar
			(1.16 MB)
			
			
		


 
											





 
	    

 
	





