1. 程式人生 > >CCF爐石傳說題目程式碼詳解C++版(類封裝通俗易懂)

CCF爐石傳說題目程式碼詳解C++版(類封裝通俗易懂)

#include<iostream>
#include <string>
#include <vector>
#include<iterator>
using namespace std;
static bool state=true;//誰出招?1先手,0後手


class role //基類
{
public:
int health;
int attack;
role();
role(int x, int y) : attack(x),health(y){};
void showHealthNumofassister();
};


class commond
{
public:
string comd;
vector<int> args;
};


class assister : public role//隨從
{
public:
int position;
assister(int x, int y, int z) :role(x, y), position(z){};
void showHealthNumofassister();
};


class hero : public role//英雄
{
public:
vector<assister> ass;
hero();
void summon(int position, int atk, int helh);
void delAssister();//刪除死亡隨從
void showHealthNumofassister();
};




void excuteCommond(hero& A, hero& B, commond& com); //A作用於B
void attacking(assister& A, assister& B);
void attacking(assister& A, hero& B);
bool noInt(double x);//是否是整數
istream & operator >> (istream & in, commond &A);//只檢查命令,未檢查引數
ostream & operator << (ostream & out, commond &A);


role::role()
{
;
}


bool noInt(double x)//是否是整數
{
if (int(x) == x)
{
return false;
}
else
return true;
}


hero::hero()
{
health = 30;
attack = 0;
}


void hero::summon(int position, int atk, int helh)
{
if (ass.size() == 7)//隨從已滿
{
return;
}
if (helh<1||helh>100||atk<0||atk>100)
{
return;
}
if (noInt(atk)||noInt(helh))
{
return;
}
if (position<0||(position>ass.size()))
{
return;//位置錯誤
}
assister  temp = assister(atk, helh, position);
int i=0;
if (ass.size() == 0)
{
ass.push_back(temp);
}
else
{
for (i = ass.size() - 1; i >= 0; i--)//i是編號0~6
{
if (temp.position > ass[i].position)//右邊插值
{
ass.insert(ass.begin()+i+1,temp);
break;
}
}
if (i<0)
{
ass.insert(ass.begin(),temp);
}
}
}


void excuteCommond(hero& A, hero& B, commond& com) //A作用於B
{
//cout << "locate in " << com << endl;
if (com.comd=="summon")
{

if (noInt(com.args[0]) || noInt(com.args[1]) || noInt(com.args[2]))
{
return;
}
else
{
if (state==true)
{
A.summon(com.args[0], com.args[1], com.args[2]);
//cout << "summon" << endl;
}
else
{
B.summon(com.args[0], com.args[1], com.args[2]);
//cout << "summon" << endl;
}
}
}
else if (com.comd == "attack")
{
if (noInt(com.args[0]) || noInt(com.args[1]))
{
return;
}
else
{

if (com.args[1] == -1)//本來以該是0的
{
//cout << "attack hero" << endl;
if (state == true)
{

attacking(A.ass[com.args[0]], B);//攻擊英雄
}
else
{
attacking(B.ass[com.args[0]],A);
}
}
else
{
//cout << "attack assister" << endl;
if (state == true)
{
attacking(A.ass[com.args[0]], B.ass[com.args[1]]);//攻擊英雄
}
else
{
attacking(B.ass[com.args[0]], A.ass[com.args[1]]);
}
}
}
}
else if (com.comd == "end")
{
state = !state;
}
}


void attacking(assister& A, assister& B)
{
if (A.attack<=0)
{
return;
}
A.health = A.health - B.attack;
B.health = B.health - A.attack;
}


void attacking(assister& A, hero& B)
{
if (A.attack <= 0)
{
return;
}
A.health = A.health - B.attack;
B.health = B.health - A.attack;
}


void hero::delAssister()
{
int i,j;

for (i = ass.size()-1; i >=0; i--)
{
assister *it = &(ass[ass.size() - 1]);//remember the last one
if (ass[i].health<=0)
{
if (i == ass.size() - 1)
{
ass.pop_back();
}
else
{
for (j = i; j < ass.size() - 1; j++)
{
{
ass[j] = ass[j + 1];//往前移動
}
}
ass.pop_back();
}

}
}
}


void hero::showHealthNumofassister()
{
cout << health << endl;
cout << ass.size()<<' ';
vector<assister>::iterator assIt = ass.begin(), assEnd = ass.end();
while (assIt != assEnd)
{
cout << (*assIt).health<<' ';
assIt++;
}
cout << endl;
return;
}


ostream & operator << (ostream & out, commond &A)
{
out << A.comd << ' ';
if (A.args.size()!=0)
{
for (int i = 0; i < A.args.size();i++)
{
out << A.args[i]<<' ';
}
}
out << endl;
return out;
}
istream & operator >> (istream & in, commond &A)//只檢查命令,未檢查引數ostream & operator << (ostream & out, commond &A)
{
A.args.clear();
string temStr;
in >> temStr;
int temp;
if (temStr=="end")
{
A.comd = temStr;
return in;
}
else if (temStr == "summon")
{
A.comd = temStr;
for (int i = 0; i < 3;i++)
{
in >> temp;
A.args.push_back(i == 0 ? temp - 1:temp);
}
return in;
}
else if (temStr == "attack")
{
A.comd = temStr;
for (int i = 0; i < 2; i++)
{
in >> temp;
A.args.push_back(temp-1);
}
return in;
}
else
{
return in;
}
}


bool  isEmptyCommond(commond& com)
{
if (com.comd.size() == 0)
{
return true;
}
else
return false;
}


int main()
{
int n,num;
cin >> n;
hero Begin = hero();
hero End = hero();
vector<commond> mingLing;
commond tempCommond;
if (n<0||n>1000)
{
return 0;
}
for (int i = 0; i < n;i++)//輸入命令
{
cin >> tempCommond;
if (!isEmptyCommond(tempCommond))
{
mingLing.push_back(tempCommond);
}
}
if (mingLing.size()!=n)
{
return 0;//命令錯誤
}
for (num = 0; num < n;num++)
{
excuteCommond(Begin,End,mingLing[num]);
Begin.delAssister();
End.delAssister();


if (Begin.health<=0)
{
cout << -1 << endl;
break;
}
if(End.health <= 0)
{
cout << 1 << endl;
break;
}
}
if (num==n)
{
cout << 0 << endl;
}
Begin.showHealthNumofassister();
End.showHealthNumofassister();
return 0;
}