炉石传说
不知道现在题目公开了没有,最近考完试比较闲,所以开通了博客,写写自己考试时候这道题的思路吧。
根据真实“魔兽世界——炉石传说”的游戏建模改编,以下是题目的回忆(若有不准,会继续更新,敬请期待):
- 有两个玩家同时对阵,分别拥有一个英雄
- 玩家可以召唤随从帮助对阵,最多召唤7个,每个随从从左向右排列,每次召唤都可以放在任意位置,若放置的位置右边有以召唤的随从,则右边的随从顺次右移
- 英雄和随从都有攻击力attack和生命值health属性(我代码中写作了life,不要紧)
- 英雄的attack值为0,health值为30;随从的attack和health在召唤时给定
- 召唤随从的格式为summon <position> <attack> <health>。position是召唤随从要加入的位置,attack是召唤随从的攻击力值的大小,health是召唤随从的生命值大小
- 命令随从攻击的格式为attack <attacker> <defender>。attacker是本方的随从,defender是要攻击的对方的随从。攻击即为本方attacker的heath值减去对方defender的attack值,对方defender的health值减去本方attacker的attack值
- 结束本次回合的格式为end,表明将出手权交给对方,轮到对方召唤或者发起攻击
- 当随从生命值小于或等于0时,随从死亡,剩余随从依次向左移弥补空位。只有当一方随从全部死亡后,才可向英雄发起攻击。
- 当英雄生命值小于或等于0时,英雄死亡,当某方英雄死亡时,游戏结束
输入样式
输入一共有n行
- 第一行 输入n,代表以下有n条命令
- 接下来n行为命令行,有以下三种格式
summon <position> <attack> <health>
attack <attacker> <defender>`
end
注: 不考虑输入不按格式以及输入错误的情况
0<=n<=1000 , 0<=攻击值<=100,生命值<=100
输出样式
输出一共有五行
- 第一行 输出游戏输赢情况,1代表先手赢,-1代表后手赢,0代表游戏未分出胜负
- 第二行 输出先手方英雄生命值
- 第三行 先输出先手方目前随从个数 后面输出按从左往右顺序排列的随从的生命值
- 第四行 输出后手方英雄生命值
- 第五行 先输出后手方目前随从个数 后面输出按从左往右顺序排列的随从的生命值
输入举例
8
summon 1 3 6
summon 2 4 2
end
summon 1 4 5
summon 1 2 1
attack 1 2
end
attack 1 1
解释:
- 有8条命令
- 先手方召唤随从a,放在位置1,攻击力为3,生命值为6
- 先手方召唤随从b,放在位置2,攻击力为4,生命值为2
- 本回合结束,轮到后手方
- 后手方召唤随从c,放在位置1,攻击力为4,生命值为5
- 后手方召唤随从d,放在位置1,这时随从c向右移一位,变成了位置2,随从d的攻击力为2,生命值为1
- 后手方发起攻击,命令站在位置1的随从d攻击先手方站在位置2的随从b,攻击结束后,随从d的生命值变为-3,随从b的生命值变为0,双方的随从d和b全部死亡。
- 本轮回合结束,轮到先手方
- 先手方发起攻击,命令站在位置1的随从a攻击后手方站在位置1的随从c,攻击结束后,随从a的生命值变为2,随从c的生命值变为2。
输出举例
0
30
1 2
30
1 2
解释:
- 游戏中先手后手方英雄均未死亡,游戏尚未结束,输出0
- 先手方英雄生命值为30
- 先手方有随从1个,生命值为2
- 后手方英雄生命值为30
- 后手方有随从1个,生命值为2
代码块
代码块语法遵循标准markdown代码,例如:
class suicong{
public:
suicong(){ life = 0; attack = 0;}
int life;
int attack;
suicong *next;
};
class suilink{
public:
suilink(){ hero = NULL; suinum = 0; }
void create();
suicong* gethero(){ return hero; }
int getsuinum(){ return suinum; }
void setsuinum(int num){ suinum=num; }
int getherolife(){ return hero->life; }
void setherolife(int hl){ hero->life=hl; }
int getsuilife(int pos);
int getsuiattack(int pos);
void setsuilife(int l, int pos);
void summon(int attack, int life);
void summon(int attack, int life, int pos);
void kill(int pos);
private:
suicong *hero;
int suinum;
};
int player = 0;
void suilink::create()
{
suicong *s= new suicong();
s->life = 30;
s->attack = 0;
hero=s;
hero->next = NULL;
}
int suilink::getsuiattack(int pos)
{
suicong *s = hero;
for (int i = 0; i < pos; i++)
{
s = s->next;
}
return s->attack;
}
int suilink::getsuilife(int pos)
{
suicong *s = hero;
for (int i = 0; i < pos; i++)
{
s = s->next;
}
return s->life;
}
void suilink::setsuilife(int l, int pos)
{
suicong *s = hero;
for (int i = 0; i < pos; i++)
{
s = s->next;
}
s->life=l;
}
void suilink::summon(int attack, int life)
{
suicong *s=hero;
suicong *t = new suicong();
t->life = life;
t->attack = attack;
if (s->next == NULL)
{
s->next = t;
}
else
{
for (int i = 0; i < suinum; i++)
{
s = s->next;
}
s->next = t;
}
suinum++;
}
void suilink::summon(int attack, int life, int pos)
{
suicong *s = hero;
suicong *t = new suicong();
t->life = life;
t->attack = attack;
for (int i = 0; i < pos-1; i++)
{
s = s->next;
}
t->next = s->next;
s->next = t;
suinum++;
}
void suilink::kill(int pos)
{
suicong *s,*r = hero;
for (int i = 0; i < pos-1; i++)
{
r = r->next;
}
s = r->next;
r->next = s->next;
delete s;
suinum--;
}
int main(int argc, char *argv[]) {
int n;
suilink a,b;
string str;
a.create();
b.create();
cin >> n;
for (int i = 0; i<n; i++)
{
cin >> str;
if (str == "summon")
{
int p,l,at;
cin >> p;
cin >> at;
cin >> l;
if (player == 0)
{
if (p>a.getsuinum())
{
a.summon(at,l);
}
else
{
a.summon(at,l,p);
}
}
else if (player == 1)
{
if (p>b.getsuinum())
{
b.summon(at, l);
}
else
{
b.summon(at, l, p);
}
}
}
else if (str == "attack")
{
int apos, bpos;
if (player = 0)
{
cin >> apos;
cin >> bpos;
if (bpos == 0)
a.setherolife(a.getherolife()- b.getsuiattack(bpos));
else
{
a.setsuilife(a.getsuilife(apos) - b.getsuiattack(bpos), apos);
b.setsuilife(b.getsuilife(bpos) - a.getsuiattack(apos), bpos);
if (a.getsuilife(apos) <= 0)
{
a.kill(apos);
}
if (b.getsuilife(bpos) <= 0)
{
b.kill(bpos);
}
}
}
else if (player = 1)
{
cin >> bpos;
cin >> apos;
if (apos == 0)
b.setherolife(b.getherolife() - a.getsuiattack(apos));
else
{
a.setsuilife(a.getsuilife(apos) - b.getsuiattack(bpos), apos);
b.setsuilife(b.getsuilife(bpos) - a.getsuiattack(apos), bpos);
if (a.getsuilife(apos) <= 0)
{
a.kill(apos);
}
if (b.getsuilife(bpos) <= 0)
{
b.kill(bpos);
}
}
}
}
else if (str == "end")
{
if (player == 0)
player = 1;
else if (player == 1)
player = 0;
}
}
if (a.getherolife()<=0)
cout << "1" << endl;
else if (b.getherolife()<=0)
cout << "-1" << endl;
else
cout << "0" << endl;
cout << a.getherolife() << endl << a.getsuinum() << " ";
for (int k = 0; k < a.getsuinum(); k++)
{
cout << a.getsuilife(k+1) << " ";
}
cout << endl;
cout << b.getherolife() << endl << b.getsuinum() << " ";
for (int k = 0; k < b.getsuinum(); k++)
{
cout << b.getsuilife(k+1) << " ";
}
cout << endl;
return 0;
}
考试时本人用了个笨办法,既然最多有7个随从,就直接图简单开了数组,之后左移右移一个循环就可以了,但是效率低下,而且程序不灵活。考完试整理了一下,用链表动态实现,大家可以参考一下,若有错误请私信我更新,谢谢!
本小白第一次写技术blog,为即将走上coding之路打好基础,也请大牛多多指教~~
//撒花★,°:.☆( ̄▽ ̄)/$:.°★
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)