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

炉石传说 第八次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.lryc.cn/news/2402340.html

相关文章:

  • AI应用工程师面试
  • LabVIEW与Modbus/TCP温湿度监控系统
  • Cursor 1.0 版本 GitHub MCP 全面指南:从安装到工作流增强
  • 自主设计一个DDS信号发生器
  • 鸿蒙UI(ArkUI-方舟UI框架)- 使用弹框
  • 学习笔记(24): 机器学习之数据预处理Pandas和转换成张量格式[2]
  • 在不同型号的手机或平板上后台运行Aidlux
  • 【SSM】SpringBoot学习笔记1:SpringBoot快速入门
  • 1.企业可观测性监控三大支柱及开源方案的横评对比
  • Neo4j图数据库管理:原理、技术与最佳实践
  • Elasticsearch中的地理空间(Geo)数据类型介绍
  • [论文阅读] 软件工程 | 如何挖掘可解释性需求?三种方法的深度对比研究
  • 双空间知识蒸馏用于大语言模型
  • OpenCV CUDA模块特征检测------角点检测的接口createMinEigenValCorner()
  • Git 提交备注应该如何规范
  • 青少年编程与数学 02-020 C#程序设计基础 17课题、WEB与移动开发
  • Qt OpenGL 实现交互功能(如鼠标、键盘操作)
  • 【Go语言基础【3】】变量、常量、值类型与引用类型
  • 8天Python从入门到精通【itheima】-69~70(字符串的常见定义和操作+案例练习)
  • 在 Linux 中查看文件并过滤空行
  • GC1809:高性能音频接收与转换芯片
  • 项目实战——C语言扫雷游戏
  • 【Java】CopyOnWriteArrayList
  • 【JS进阶】ES6 实现继承的方式
  • mac 电脑Pycharm ImportError: No module named pip
  • C#入门学习笔记 #8(委托)
  • CSS 3D 变换中z-index失效问题
  • Vue3 中使用 i18n
  • vue:当前对象添加对应值
  • Tailwind CSS 实战:基于 Kooboo 构建 AI 对话框页面(七):消息框交互功能添加