纯链表模拟,各种操作熟知就很简单
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int n;
struct role {
int attack;
int health;
struct role* next;
role() : attack(0), health(0), next(nullptr) {}
role(int attack, int health) : attack(attack), health(health), next(nullptr) {}
// 在指定位置插入新节点
static role* insert(role* head, int position, int attack, int health) {
role* new_node = new role(attack, health);
// 插入到头部
if (position == 1) {
new_node->next = head;
return new_node;
}
// 插入到中间或尾部
role* current = head;
for (int i = 1; i < position - 1 && current != nullptr; i++) {
current = current->next;
}
if (current != nullptr) {
new_node->next = current->next;
current->next = new_node;
}
return head;
}
// 删除指定位置的节点
static role* remove(role* head, int position) {
if (head == nullptr) return nullptr;
// 删除头节点
if (position == 1) {
role* temp = head;
head = head->next;
delete temp;
return head;
}
// 删除中间或尾部节点
role* current = head;
for (int i = 1; i < position - 1 && current->next != nullptr; i++) {
current = current->next;
}
if (current->next != nullptr) {
role* temp = current->next;
current->next = temp->next;
delete temp;
}
return head;
}
// 获取指定位置的节点
static role* get(role* head, int position) {
role* current = head;
for (int i = 1; i < position && current != nullptr; i++) {
current = current->next;
}
return current;
}
// 获取链表长度
static int size(role* head) {
int count = 0;
role* current = head;
while (current != nullptr) {
count++;
current = current->next;
}
return count;
}
// 清理死亡的随从
static role* cleanup(role* head) {
while (head != nullptr && head->health <= 0) {
role* temp = head;
head = head->next;
delete temp;
}
if (head == nullptr) return nullptr;
role* current = head;
while (current->next != nullptr) {
if (current->next->health <= 0) {
role* temp = current->next;
current->next = temp->next;
delete temp;
}
else {
current = current->next;
}
}
return head;
}
};
struct gamer {
struct role hero;
struct role* helper;
gamer() : helper(nullptr) {}
gamer(int attack, int health) : hero(attack, health), helper(nullptr) {}
int getSize() {
return role::size(helper);
}
};
struct chessboard {
struct gamer player[2];
} cb;
void init() {
cb.player[0] = gamer(0, 30);
cb.player[1] = gamer(0, 30);
}
// 召唤随从
void summon(int index, int position, int attack, int health) {
cb.player[index].helper = role::insert(cb.player[index].helper, position, attack, health);
}
// 攻击操作
bool attack(int index, int attacker, int defender) {
int d_index = index ^ 1;
// 获取攻击者
role* attacker_role = role::get(cb.player[index].helper, attacker);
if (attacker_role == nullptr) return false;
int attack_power = attacker_role->attack;
if (defender == 0) {
// 攻击对方英雄
cb.player[d_index].hero.health -= attack_power;
attacker_role->health -= cb.player[d_index].hero.attack;
// 清理死亡的随从
cb.player[index].helper = role::cleanup(cb.player[index].helper);
// 检查英雄是否死亡
if (cb.player[d_index].hero.health <= 0) {
return true;
}
}
else {
// 攻击对方随从
role* target = role::get(cb.player[d_index].helper, defender);
if (target != nullptr) {
target->health -= attack_power;
attacker_role->health -= target->attack;
// 清理死亡的随从
cb.player[index].helper = role::cleanup(cb.player[index].helper);
cb.player[d_index].helper = role::cleanup(cb.player[d_index].helper);
}
}
return false;
}
// 输出当前状态
void printState() {
// 检查游戏结果
int result = 0;
if (cb.player[0].hero.health <= 0) result = -1;
else if (cb.player[1].hero.health <= 0) result = 1;
cout << result << endl;
// 输出先手玩家信息
cout << cb.player[0].hero.health << endl;
cout << cb.player[0].getSize();
role* current = cb.player[0].helper;
while (current != nullptr) {
cout << " " << current->health;
current = current->next;
}
cout << endl;
// 输出后手玩家信息
cout << cb.player[1].hero.health << endl;
cout << cb.player[1].getSize();
current = cb.player[1].helper;
while (current != nullptr) {
cout << " " << current->health;
current = current->next;
}
cout << endl;
}
int main() {
cin >> n;
init();
int index = 0; // 先手
while (n--) {
string action;
cin >> action;
if (action == "summon") {
int position, attack, health;
cin >> position >> attack >> health;
summon(index, position, attack, health);
}
else if (action == "attack") {
int attacker, defender;
cin >> attacker >> defender;
if (attack(index, attacker, defender)) {
// 游戏结束
break;
}
}
else if (action == "end") {
index = index ^ 1;
}
}
printState();
return 0;
}