C++#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <limits>
#include <memory>
using namespace std;
// 卡牌基类
class Card {
public:
string name;
string description;
int cost;
bool isAttack;
bool isDefense;
bool isUtility;
bool isRefresh;
Card(string n, string desc, int c, bool atk = false, bool def = false, bool util = false, bool refresh = false)
: name(n), description(desc), cost(c), isAttack(atk), isDefense(def), isUtility(util), isRefresh(refresh) {}
virtual bool canPlay(int playerHealth, int playerDefense) {
return true;
}
virtual void play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, class Player& player) = 0;
// 添加升级方法
virtual void upgrade() = 0;
virtual ~Card() {}
};
// 攻击卡牌
class AttackCard : public Card {
public:
int damage;
AttackCard(string n, string desc, int c, int dmg)
: Card(n, desc, c, true, false, false), damage(dmg) {}
// 实现升级方法
void upgrade() override {
damage += 3;
description = "造成" + to_string(damage) + "点伤害";
}
void play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, class Player& player) override;
};
// 防御卡牌
class DefenseCard : public Card {
public:
int defense;
DefenseCard(string n, string desc, int c, int def)
: Card(n, desc, c, false, true, false), defense(def) {}
// 实现升级方法
void upgrade() override {
if (defense > 0) {
defense += 3;
description = "获得" + to_string(defense) + "点护甲";
} else {
defense -= 3; // 负效果卡牌升级效果更强
description = "扣" + to_string(-defense) + "点护甲获得" + to_string(-cost) + "点能量";
}
}
bool canPlay(int playerHealth, int playerDefense) override {
if (defense < 0) {
return playerDefense >= -defense;
}
return true;
}
void play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, class Player& player) override;
};
// 实用卡牌
class UtilityCard : public Card {
public:
int healAmount;
UtilityCard(string n, string desc, int c, int heal)
: Card(n, desc, c, false, false, true), healAmount(heal) {}
// 实现升级方法
void upgrade() override {
if (healAmount > 0) {
healAmount += 3;
description = "恢复" + to_string(healAmount) + "点生命";
} else {
healAmount -= 3; // 负效果卡牌升级效果更强
description = "恢复" + to_string(-cost) + "点能量,扣" + to_string(-healAmount) + "生命";
}
}
bool canPlay(int playerHealth, int playerDefense) override {
if (healAmount < 0) {
return playerHealth > -healAmount;
}
return true;
}
void play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, class Player& player) override;
};
// 刷新手牌卡牌
class RefreshCard : public Card {
public:
RefreshCard(string n, string desc, int c)
: Card(n, desc, c, false, false, false, true) {}
void upgrade() override {
cost = max(0, cost - 1);
description = "丢弃所有手牌,然后抽取相同数量的牌(升级后消耗" + to_string(cost) + ")";
}
void play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, class Player& player) override;
};
// 敌人基类
class Enemy {
public:
string name;
int health;
int attack;
int defense;
string description;
Enemy(string n, int hp, int atk, int def, string desc)
: name(n), health(hp), attack(atk), defense(def), description(desc) {}
virtual void takeTurn(int& playerHealth, int& playerDefense) {
if (defense > 0) {
cout << name << " 有 " << defense << " 点护甲!\n";
}
int actualAttack = attack;
if (playerDefense > 0) {
int blocked = min(actualAttack, playerDefense);
playerDefense -= blocked;
actualAttack -= blocked;
}
playerHealth -= actualAttack;
cout << name << " 对你造成了 " << actualAttack << " 点伤害!\n";
}
bool isDefeated() const {
return health <= 0;
}
};
// 玩家类
class Player {
public:
int health;
int maxHealth;
int defense;
int energy;
int maxEnergy;
int gold;
vector<Card*> deck;
vector<Card*> hand;
vector<Card*> discardPile;
int floor;
Player() : health(50), maxHealth(50), defense(0), energy(3), maxEnergy(3), gold(50), floor(1) {
// 初始卡组
deck.push_back(new AttackCard("打击", "造成5点伤害", 1, 5));
deck.push_back(new AttackCard("打击", "造成5点伤害", 1, 5));
deck.push_back(new AttackCard("打击", "造成5点伤害", 1, 5));
deck.push_back(new AttackCard("打击", "造成5点伤害", 1, 5));
deck.push_back(new DefenseCard("防御", "获得5点护甲", 1, 5));
deck.push_back(new DefenseCard("防御", "获得5点护甲", 1, 5));
deck.push_back(new DefenseCard("防御", "获得5点护甲", 1, 5));
deck.push_back(new UtilityCard("治疗", "恢复5点生命", 1, 5));
}
~Player() {
for (Card* card : deck) delete card;
for (Card* card : hand) delete card;
for (Card* card : discardPile) delete card;
}
void drawHand() {
for (int i = 0; i < 5; i++) {
if (deck.empty()) {
shuffleDiscardIntoDeck();
if (deck.empty()) break;
}
hand.push_back(deck.back());
deck.pop_back();
}
}
void shuffleDiscardIntoDeck() {
if (discardPile.empty()) return;
random_shuffle(discardPile.begin(), discardPile.end());
for (Card* card : discardPile) {
deck.push_back(card);
}
discardPile.clear();
}
void discardHand() {
for (Card* card : hand) {
discardPile.push_back(card);
}
hand.clear();
}
void resetEnergy() {
energy = maxEnergy;
}
void heal(int amount) {
health = min(maxHealth, health + amount);
// 确保生命值不低于0
if (health < 0) health = 0;
}
void addCardToDeck(Card* card) {
deck.push_back(card);
}
void removeCardFromDeck(int index) {
if (index >= 0 && index < static_cast<int>(deck.size())) {
delete deck[index];
deck.erase(deck.begin() + index);
}
}
};
// 攻击卡牌的play方法实现
void AttackCard::play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, Player& player) {
int actualDamage = damage; // 使用临时变量
if (enemyDefense > 0) {
int defenseDamage = min(actualDamage, enemyDefense);
enemyDefense -= defenseDamage;
actualDamage -= defenseDamage;
}
enemyHealth -= actualDamage;
cout << "你使用 " << name << " 对敌人造成了 " << actualDamage << " 点伤害!\n";
if (enemyDefense > 0) {
cout << "敌人护甲剩余: " << enemyDefense << "\n";
}
}
// 防御卡牌的play方法实现
void DefenseCard::play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, Player& player) {
playerDefense += defense;
if (defense < 0) {
cout << "你使用 " << name << " 消耗了 " << -defense << " 点护甲!\n";
} else {
cout << "你使用 " << name << " 获得了 " << defense << " 点护甲!\n";
}
}
// 实用卡牌的play方法实现
void UtilityCard::play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, Player& player) {
playerHealth += healAmount;
// 确保生命值不低于0
if (playerHealth < 0) playerHealth = 0;
if (healAmount < 0) {
cout << "你使用 " << name << " 消耗了 " << -healAmount << " 点生命值!\n";
} else {
cout << "你使用 " << name << " 恢复了 " << healAmount << " 点生命值!\n";
}
}
// 刷新卡牌的play方法实现
void RefreshCard::play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense, Player& player) {
cout << "你使用 " << name << " 刷新了手牌!\n";
// 丢弃当前手牌
int handSize = player.hand.size();
for (Card* card : player.hand) {
player.discardPile.push_back(card);
}
player.hand.clear();
// 重新抽取相同数量的牌
for (int i = 0; i < handSize; i++) {
if (player.deck.empty()) {
player.shuffleDiscardIntoDeck();
if (player.deck.empty()) break;
}
player.hand.push_back(player.deck.back());
player.deck.pop_back();
}
}
// 游戏类
class RogueCardGame {
private:
Player player;
Enemy* currentEnemy;
bool gameOver;
bool victory;
vector<Card*> allCards = {
new AttackCard("强力打击", "造成8点伤害", 2, 8),
new AttackCard("致命一击", "造成20点伤害", 3, 20),
new AttackCard("忘本", "造成50点伤害", 5, 50),
new AttackCard("权威", "造成100点伤害", 7, 100),
new AttackCard("权威 max pro 觉醒", "造成至少1e9点伤害", 50, 0x3f3f3f3f),
new DefenseCard("铁壁", "获得10点护甲", 2, 10),
new DefenseCard("振金屏障", "获得25点护甲", 7, 50),
new DefenseCard("莫比乌斯盾", "获得50点护甲", 10, 100),
new DefenseCard("莫比乌斯盾 max pro 觉醒", "获得100点护甲", 10, 100),
new DefenseCard("反射护盾", "获得5点护甲并反弹3点伤害", 2, 5),
new DefenseCard("破盾max", "扣20点护甲获得5点能量", -5, -20),
new DefenseCard("破盾max pro", "扣50点护甲获得15点能量", -15, -50),
new DefenseCard("破盾max pro 觉醒", "扣100点护甲获得50点能量", -50, -100),
new DefenseCard("破盾", "扣5点护甲获得1点能量", -1, -5),
new UtilityCard("圣光", "恢复10点生命", 2, 10),
new UtilityCard("高压灯泡圣光", "恢复100点生命", 10, 100),
new UtilityCard("生命吸取", "恢复5点生命", 1, 5),
new UtilityCard("能量转化", "恢复1点能量,扣3生命", -1, -3),
new RefreshCard("刷新", "丢弃所有手牌,然后抽取相同数量的牌", 2) // 刷新手牌卡牌
};
vector<Enemy*> allEnemies = {
new Enemy("哥布林", 30, 5, 0, "弱小但数量众多的生物"),
new Enemy("兽人战士", 50, 8, 3, "强壮而野蛮的战士"),
new Enemy("骷髅法师", 40, 10, 0, "能够施展黑暗魔法的亡灵"),
new Enemy("巨魔", 70, 12, 5, "再生能力强大的大型怪物"),
new Enemy("暗影刺客", 35, 15, 0, "高伤害但脆弱的刺客"),
new Enemy("黄浩chen", 10, 5, 0, "你的朋友,适合用来练手"),
new Enemy("黄浩chen max", 30, 15, 3, "你的朋友加强版,不是很适合用来练手"),
new Enemy("黄浩chen max pro", 50, 25, 5, "你的朋友最终版,绝对不适合用来练手"),
new Enemy("龙", 100, 20, 10, "最终BOSS,强大的巨龙"),
new Enemy("黄浩chen max pro 觉醒", 150, 75, 15, "你的朋友最终觉醒版,我觉得你不能胜利"),
new Enemy("黄浩chen max pro 觉醒 极限", 300, 150, 30, "你的朋友最终觉醒版极限,我觉得你胜利的可能渺茫"),
new Enemy("黄浩chen max pro 觉醒 极限 神威", 1000000000, 250, 50, "你的朋友最终觉醒版极限神威,你胜利了他倒立吃三斤")
};
public:
RogueCardGame() : gameOver(false), victory(false), currentEnemy(nullptr) {
srand(time(0));
}
~RogueCardGame() {
for (Card* card : allCards) delete card;
for (Enemy* enemy : allEnemies) delete enemy;
delete currentEnemy;
}
void startGame() {
cout << "================================\n";
cout << " 肉鸽卡牌回合制冒险游戏 \n";
cout << "================================\n";
cout << "在每一层地牢中,你需要击败敌人来前进\n";
cout << "使用卡牌需要消耗能量,每回合恢复所有能量\n";
cout << "击败敌人后,你可以获得金币并选择新卡牌\n";
cout << "最终目标是击败第15层的最终BOSS!\n";
cout << "================================\n\n";
while (!gameOver && player.floor <= 15) {
generateEnemy();
battle();
if (!gameOver) {
if (player.floor == 15) {
victory = true;
gameOver = true;
} else {
player.floor++;
shopPhase();
}
}
}
endGame();
}
private:
void generateEnemy() {
if (player.floor == 15) {
// 最终BOSS - 龙
currentEnemy = new Enemy("龙", 100, 20, 10, "最终BOSS,强大的巨龙");
} else {
// 随机敌人,确保索引在范围内
int index = rand() % allEnemies.size();
currentEnemy = new Enemy(*allEnemies[index]);
}
cout << "\n================================\n";
cout << " 第 " << player.floor << " 层 - " << currentEnemy->name << "\n";
cout << "================================\n";
cout << currentEnemy->description << "\n";
}
void battle() {
player.defense = 0;
while (!currentEnemy->isDefeated() && player.health > 0) {
player.resetEnergy();
player.drawHand();
cout << "\n--------------------------------\n";
cout << "你的状态: 生命(" << player.health << "/" << player.maxHealth << ") ";
cout << "护甲(" << player.defense << ") ";
cout << "能量(" << player.energy << "/" << player.maxEnergy << ")\n";
cout << currentEnemy->name << ": 生命(" << currentEnemy->health << ") 护甲(" << currentEnemy->defense << ")\n";
// 玩家回合
playerTurn();
if (currentEnemy->isDefeated()) {
cout << "\n你击败了 " << currentEnemy->name << "!\n";
int goldReward = 10 + player.floor * 5;
player.gold += goldReward;
cout << "获得 " << goldReward << " 金币!\n";
break;
}
// 敌人回合
if (player.health > 0) {
enemyTurn();
// 确保护甲不为负
if (player.defense < 0) player.defense = 0;
}
}
player.discardHand();
if (player.health <= 0) {
gameOver = true;
}
delete currentEnemy;
currentEnemy = nullptr;
}
void playerTurn() {
bool turnEnded = false;
while (!turnEnded && player.health > 0 && !currentEnemy->isDefeated()) {
cout << "\n你的手牌:\n";
for (int i = 0; i < player.hand.size(); i++) {
cout << i + 1 << ". " << player.hand[i]->name;
cout << " (" << player.hand[i]->cost << "能量)";
cout << " - " << player.hand[i]->description << "\n";
}
cout << "0. 结束回合\n";
cout << "选择一张卡牌使用 (0-" << player.hand.size() << "): ";
int choice;
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请重新选择。\n";
continue;
}
if (choice == 0) {
turnEnded = true;
} else if (choice > 0 && choice <= static_cast<int>(player.hand.size())) {
Card* selectedCard = player.hand[choice - 1];
if (selectedCard->cost > player.energy) {
cout << "能量不足!\n";
} else if (!selectedCard->canPlay(player.health, player.defense)) {
if (dynamic_cast<DefenseCard*>(selectedCard) && dynamic_cast<DefenseCard*>(selectedCard)->defense < 0) {
cout << "护甲不足,无法使用此技能!\n";
} else if (dynamic_cast<UtilityCard*>(selectedCard) && dynamic_cast<UtilityCard*>(selectedCard)->healAmount < 0) {
cout << "生命值不足,无法使用此技能!\n";
} else {
cout << "无法使用此卡牌!\n";
}
} else {
player.energy -= selectedCard->cost;
selectedCard->play(player.health, player.defense, currentEnemy->health, currentEnemy->defense, player);
player.discardPile.push_back(selectedCard);
player.hand.erase(player.hand.begin() + choice - 1);
if (currentEnemy->isDefeated() || player.health <= 0) {
turnEnded = true;
}
}
} else {
cout << "无效选择,请重新输入。\n";
}
}
}
void enemyTurn() {
if (!currentEnemy->isDefeated() && player.health > 0) {
cout << "\n--- " << currentEnemy->name << " 的回合 ---\n";
currentEnemy->takeTurn(player.health, player.defense);
}
}
void shopPhase() {
bool shopping = true;
while (shopping) {
cout << "\n================================\n";
cout << " 商店与休息区 \n";
cout << "================================\n";
cout << "金币: " << player.gold << "\n";
cout << "生命: " << player.health << "/" << player.maxHealth << "\n";
cout << "1. 休息 (恢复20点生命, 花费20金币)\n";
cout << "2. 升级卡牌 (花费50金币)\n";
cout << "3. 购买新卡牌 (花费30金币)\n";
cout << "4. 提升最大生命值 (花费40金币)\n";
cout << "5. 提升最大能量 (花费100金币)\n";
cout << "6. 出售卡牌 (获得20金币)\n";
cout << "0. 继续前进\n";
cout << "选择: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
if (player.gold >= 20) {
player.gold -= 20;
player.heal(20);
cout << "你休息了一会,恢复了20点生命。当前生命: " << player.health << "/" << player.maxHealth << "\n";
} else {
cout << "金币不足!需要20金币。\n";
}
break;
case 2:
if (player.gold >= 50) {
player.gold -= 50;
upgradeCard();
} else {
cout << "金币不足!需要50金币。\n";
}
break;
case 3:
if (player.gold >= 30) {
player.gold -= 30;
buyNewCard();
} else {
cout << "金币不足!需要30金币。\n";
}
break;
case 4:
if (player.gold >= 40) {
player.gold -= 40;
player.maxHealth += 10;
player.health = player.maxHealth;
cout << "最大生命值提升至 " << player.maxHealth << "!\n";
} else {
cout << "金币不足!需要40金币。\n";
}
break;
case 5:
if (player.gold >= 100) {
player.gold -= 100;
player.maxEnergy++;
player.energy = player.maxEnergy;
cout << "最大能量提升至 " << player.maxEnergy << "!\n";
} else {
cout << "金币不足!需要100金币。\n";
}
break;
case 6: // 出售卡牌
sellCard();
break;
case 0:
shopping = false;
cout << "你继续前进...\n";
break;
default:
cout << "无效选择。\n";
}
// 检查玩家是否还有金币进行其他操作
if (shopping && player.gold < 20) {
cout << "\n你的金币已不足以进行任何操作,自动继续前进...\n";
shopping = false;
}
}
}
void buyNewCard() {
vector<Card*> availableCards;
// 初始生成3张卡牌
for (int i = 0; i < 3; i++) {
int index = rand() % allCards.size();
availableCards.push_back(allCards[index]);
}
bool inBuyMenu = true;
while (inBuyMenu) {
cout << "\n可购买的卡牌 (刷新花费10金币):\n";
for (int i = 0; i < availableCards.size(); i++) {
cout << i + 1 << ". " << availableCards[i]->name;
cout << " (" << availableCards[i]->cost << "能量)";
cout << " - " << availableCards[i]->description << "\n";
}
cout << "0. 取消购买\n";
cout << "99. 刷新卡牌列表 (花费10金币)\n"; // 刷新选项
cout << "选择一张卡牌添加到你的牌组,或进行刷新: ";
int choice;
cin >> choice;
if (choice == 0) {
inBuyMenu = false;
player.gold += 30; // 退回金币
cout << "购买取消。\n";
} else if (choice == 99) {
// 刷新
if (player.gold >= 10) {
player.gold -= 10;
// 清空availableCards,重新生成3张
availableCards.clear();
for (int i = 0; i < 3; i++) {
int index = rand() % allCards.size();
availableCards.push_back(allCards[index]);
}
cout << "已刷新!花费10金币。\n";
} else {
cout << "金币不足!需要10金币刷新。\n";
}
} else if (choice > 0 && choice <= static_cast<int>(availableCards.size())) {
Card* selected = availableCards[choice - 1];
// 复制卡牌并添加到牌组
if (dynamic_cast<AttackCard*>(selected)) {
player.addCardToDeck(new AttackCard(
selected->name,
selected->description,
selected->cost,
dynamic_cast<AttackCard*>(selected)->damage
));
} else if (dynamic_cast<DefenseCard*>(selected)) {
player.addCardToDeck(new DefenseCard(
selected->name,
selected->description,
selected->cost,
dynamic_cast<DefenseCard*>(selected)->defense
));
} else if (dynamic_cast<UtilityCard*>(selected)) {
player.addCardToDeck(new UtilityCard(
selected->name,
selected->description,
selected->cost,
dynamic_cast<UtilityCard*>(selected)->healAmount
));
} else if (dynamic_cast<RefreshCard*>(selected)) {
player.addCardToDeck(new RefreshCard(
selected->name,
selected->description,
selected->cost
));
}
cout << "你获得了 " << selected->name << " 卡牌!\n";
inBuyMenu = false; // 结束购买
} else {
cout << "无效选择,请重新输入。\n";
}
}
}
void upgradeCard() {
if (player.deck.empty()) {
cout << "你的牌组中没有卡牌可以升级!\n";
player.gold += 50; // 退回金币
return;
}
cout << "\n选择要升级的卡牌:\n";
for (int i = 0; i < player.deck.size(); i++) {
cout << i + 1 << ". " << player.deck[i]->name;
cout << " - " << player.deck[i]->description << "\n";
}
cout << "0. 取消\n";
cout << "选择: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= static_cast<int>(player.deck.size())) {
Card* card = player.deck[choice - 1];
card->upgrade(); // 直接调用卡牌的升级方法
cout << card->name << " 已升级! 新效果: " << card->description << "\n";
} else {
player.gold += 50; // 退回金币
cout << "升级取消。\n";
}
}
void sellCard() {
if (player.deck.empty()) {
cout << "你的牌组中没有卡牌可以出售!\n";
return;
}
cout << "\n选择要出售的卡牌 (出售获得20金币):\n";
for (int i = 0; i < player.deck.size(); i++) {
cout << i + 1 << ". " << player.deck[i]->name;
cout << " - " << player.deck[i]->description << "\n";
}
cout << "0. 取消\n";
cout << "选择: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= static_cast<int>(player.deck.size())) {
cout << "你出售了 " << player.deck[choice - 1]->name << " 卡牌,获得20金币。\n";
player.gold += 20;
player.removeCardFromDeck(choice - 1);
} else {
cout << "出售取消。\n";
}
}
void endGame() {
cout << "\n================================\n";
if (victory) {
cout << " 恭喜你击败了最终BOSS! \n";
cout << " 你成功通关了游戏! \n";
} else if (player.health <= 0) {
cout << " 你被击败了... \n";
cout << " 游戏结束 \n";
}
cout << "================================\n";
cout << "最终层数: " << player.floor << "\n";
cout << "最终金币: " << player.gold << "\n";
cout << "牌组大小: " << player.deck.size() << "\n";
}
};
int main() {
RogueCardGame game;
game.startGame();
return 0;
}