当前位置: 首页 > ops >正文

炉石传说 第八次CCF-CSP计算机软件能力认证

纯链表模拟,各种操作熟知就很简单

#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;
}

http://www.xdnf.cn/news/12218.html

相关文章:

  • 【大模型推理加速】MOE加速比与batchsize 关系
  • 某药监局药品详情sign值逆向
  • 第12期_网站搭建_几时网络验证1.3二改源码包2024 软件卡密系统 虚拟主机搭建笔记
  • linux下覆盖率测试总结
  • SQL Server相关的sql语句
  • React Hooks 指南:何时使用 useEffect ?
  • 鸿蒙APP测试实战:从HDC命令到专项测试
  • 【连接器专题】案例:FPC焊接金手指顶层和底层开窗/焊盘为什么要错位?
  • 《计算机是怎么跑起来的》第二章读后感
  • LeetCode 70 爬楼梯(Java)
  • 【深度学习】为什么2个3×3的卷积可以相当于一个5×5的卷积核?为什么3个3×3的卷积相当于一个7×7的卷积核,到底区别在哪里?我们该如何使用?
  • ESP32C3中BLE开发问题汇总
  • 数字图像处理第二次实验
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(32):そうやすいにくいすぎ(過ぎ)
  • 链表相关知识
  • 一键切换不同状态,3D数字孪生场景搭建更便捷!
  • 【iOS】cache_t分析
  • Qt 按钮类控件(Push Button 与 Radio Button)(1)
  • COMSOL学习笔记-静电场仿真
  • 可视化图解算法48:有效括号序列
  • DFORMER: RETHINKING RGBD REPRESENTATION LEARNING FOR SEMANTIC SEGMENTATION 论文浅析
  • 电厂数字孪生:智能优化助力碳中和
  • 【定昌linux开发板】设置用户密码过期时间
  • eNSP实现WDS手拉手业务
  • 如何做好一份技术文档?(上篇)
  • Spring AI(11)——SSE传输的MCP服务端
  • Spring Plugin框架应用实践:医院多租户客户端动态路由方案解析
  • App使用webview套壳引入h5(二)—— app内访问h5,顶部被手机顶部菜单遮挡问题,保留顶部安全距离
  • 两个错误教训记录--java变量作用域问题导致变量值异常
  • calico/node is not ready: BIRD is not ready: BGP not established with xxx