题意
- 测试结果
思路
- 英雄和随从用
People
表示,有生命值和攻击力 - 棋盘
board
用2*8的People
指针数组构成,初始化board[0][0]
和board[1][0]
为英雄的属性 - 按输入构成主程序框架,用
plr
表示当前玩家 “summon”
操作时,先向后移动随从位置,再插入“attack”
操作时,更新生命值,分别判断。若随从生命值<=0,随从向前移;若被攻方英雄生命值<=0,且当前比赛没有结果,则记录plr"end"
操作时,plr=1-plr
更新当前玩家- 最后按输出格式输出
代码
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct People{
int hlth, atck;
People(){}
People(int h,int a):hlth(h),atck(a){}
};
People* board[2][8];
void clear(){
for(int i=0;i<2;i++){
for(int j=0;j<8;j++){
board[i][j]=NULL;
}
}
for(int i=0;i<2;i++)
board[i][0] = new People(30,0);
}
int main(void){
int n,plr=0,chess=-1;
cin>>n;
clear();
while(n--){
string action;
cin>>action;
if(action=="summon"){
int pos,atk,hlth;
cin>>pos>>atk>>hlth;
//
for(int j=7;j>pos;j--)
board[plr][j] = board[plr][j-1];
board[plr][pos] = new People(hlth,atk);
}
else if(action=="attack"){
int attacker,defender;
cin>>attacker>>defender;
//
board[plr][attacker]->hlth -= board[1-plr][defender]->atck;
board[1-plr][defender]->hlth -= board[plr][attacker]->atck;
//
if(board[plr][attacker]->hlth <= 0){
for(int j=attacker;j<7;j++)
board[plr][j] = board[plr][j+1];
board[plr][7] = NULL;
}
//
if(board[1-plr][defender]->hlth <= 0){
if(defender==0 && chess==-1)
chess = plr;
else if(defender!=0){
for(int j=defender;j<7;j++)
board[1-plr][j] = board[1-plr][j+1];
board[1-plr][7] = NULL;
}
}
}
else if(action=="end"){
//
plr = 1-plr;
}
}
//
if(chess==-1) cout<<0<<endl;
else if(chess==0) cout<<1<<endl;
else if(chess==1) cout<<-1<<endl;
//
int p1=0;
vector<int> v1;
cout<<board[0][0]->hlth<<endl;
for(int i=1;i<=7;i++){
if(board[0][i]!=NULL){
p1++;
v1.push_back(board[0][i]->hlth);
}
}
cout<<p1<<" ";
for(auto h:v1) cout<<h<<" ";
cout<<endl;
//
int p2=0;
vector<int> v2;
cout<<board[1][0]->hlth<<endl;
for(int i=1;i<=7;i++){
if(board[1][i]!=NULL){
p2++;
v2.push_back(board[1][i]->hlth);
}
}
cout<<p2<<" ";
for(auto h:v2) cout<<h<<" ";
cout<<endl;
return 0;
}