C++#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <limits>
using namespace std;
// 卡牌基类
class Card {
public:
string name;
string description;
int cost;
bool isAttack;
bool isDefense;
bool isUtility;
Card(string n, string desc, int c, bool atk, bool def, bool util)
: name(n), description(desc), cost(c), isAttack(atk), isDefense(def), isUtility(util) {}
virtual void play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense) = 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 play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense) override {
if (enemyDefense > 0) {
int actualDamage = min(damage, enemyDefense);
enemyDefense -= actualDamage;
damage -= actualDamage;
}
enemyHealth -= damage;
cout << "你使用 " << name << " 对敌人造成了 " << damage << " 点伤害!\n";
}
};
// 防御卡牌
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 play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense) override {
playerDefense += defense;
cout << "你使用 " << name << " 获得了 " << defense << " 点护甲!\n";
}
};
// 实用卡牌
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 play(int& playerHealth, int& playerDefense, int& enemyHealth, int& enemyDefense) override {
playerHealth += healAmount;
cout << "你使用 " << name << " 恢复了 " << healAmount << " 点生命值!\n";
}
};
// 敌人基类
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";
}
if (playerDefense > 0) {
int actualDamage = min(attack, playerDefense);
playerDefense -= actualDamage;
attack -= actualDamage;
}
playerHealth -= attack;
cout << name << " 对你造成了 " << attack << " 点伤害!\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() {
// 从牌库抽5张牌
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() {
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);
}
void addCardToDeck(Card* card) {
deck.push_back(card);
}
};
// 游戏类
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("连击", "造成5点伤害*3次", 2, 15),
new DefenseCard("铁壁", "获得10点护甲", 2, 10),
new DefenseCard("反射护盾", "获得5点护甲并反弹3点伤害", 2, 5),
new DefenseCard("破盾max", "扣20点护甲获得5点能量", -5, -20),
new DefenseCard("破盾max pro", "扣50点护甲获得15点能量", -15, -50),
new DefenseCard("破盾", "扣5点护甲获得1点能量", -1, -5),
new UtilityCard("圣光", "恢复10点生命", 2, 10),
new UtilityCard("生命吸取", "恢复5点生命", 1, 5),
new UtilityCard("狂暴", "恢复5点能量,扣10生命", 1, 5),
new UtilityCard("能量转化", "恢复1点能量,扣3生命", -1, -3)
};
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,强大的巨龙")
};
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 << "最终目标是击败第5层的巨龙!\n";
cout << "================================\n\n";
while (!gameOver && player.floor <= 5) {
generateEnemy();
battle();
if (!gameOver) {
if (player.floor == 7) {
victory = true;
gameOver = true;
} else {
player.floor++;
shopPhase();
}
}
}
endGame();
}
private:
void generateEnemy() {
int index = min(player.floor - 1, static_cast<int>(allEnemies.size() - 1));
if (player.floor == 5) {
// 最终BOSS
currentEnemy = new Enemy(*allEnemies.back());
} else {
// 随机敌人
index = rand() % (static_cast<int>(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 << ")\n";
// 玩家回合
playerTurn();
if (currentEnemy->isDefeated()) {
cout << "\n你击败了 " << currentEnemy->name << "!\n";
int goldReward = 10 + player.floor * 5;
player.gold += goldReward;
cout << "获得 " << goldReward << " 金币!\n";
break;
}
// 敌人回合
enemyTurn();
}
player.discardHand();
if (player.health <= 0) {
gameOver = true;
}
}
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 <= player.hand.size()) {
Card* selectedCard = player.hand[choice - 1];
if (selectedCard->cost > player.energy) {
cout << "能量不足!\n";
} else {
player.energy -= selectedCard->cost;
selectedCard->play(player.health, player.defense, currentEnemy->health, currentEnemy->defense);
// 将使用过的卡牌移到弃牌堆
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() {
cout << "\n================================\n";
cout << " 商店与休息区 \n";
cout << "================================\n";
cout << "金币: " << player.gold << "\n";
cout << "1. 休息 (恢复20点生命)\n";
cout << "2. 升级卡牌 (50金币)\n";
cout << "3. 购买新卡牌 (30金币)\n";
cout << "4. 提升最大生命值 (40金币)\n";
cout << "5. 提升最大能量 (100金币)\n";
cout << "0. 继续前进\n";
cout << "选择: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
player.heal(20);
cout << "你休息了一会,恢复了20点生命。当前生命: " << player.health << "/" << player.maxHealth << "\n";
break;
case 2:
if (player.gold >= 50) {
player.gold -= 50;
upgradeCard();
} else {
cout << "金币不足!\n";
}
break;
case 3:
if (player.gold >= 30) {
player.gold -= 30;
buyNewCard();
} else {
cout << "金币不足!\n";
}
break;
case 4:
if (player.gold >= 40) {
player.gold -= 40;
player.maxHealth += 10;
player.health = player.maxHealth;
cout << "最大生命值提升至 " << player.maxHealth << "!\n";
} else {
cout << "金币不足!\n";
}
break;
case 5:
if (player.gold >= 100) {
player.gold -= 100;
player.maxEnergy++;
cout << "最大能量提升至 " << player.maxEnergy << "!\n";
} else {
cout << "金币不足!\n";
}
break;
case 0:
cout << "你继续前进...\n";
break;
default:
cout << "无效选择。\n";
}
}
void buyNewCard() {
cout << "\n可购买的卡牌:\n";
vector<Card*> availableCards;
// 随机选择3张卡牌
for (int i = 0; i < 3; i++) {
int index = rand() % allCards.size();
availableCards.push_back(allCards[index]);
}
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 << "选择一张卡牌添加到你的牌组: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= availableCards.size()) {
// 创建卡牌副本加入牌组
Card* selected = availableCards[choice - 1];
if (dynamic_cast<AttackCard*>(selected)) {
player.addCardToDeck(new AttackCard(*dynamic_cast<AttackCard*>(selected)));
} else if (dynamic_cast<DefenseCard*>(selected)) {
player.addCardToDeck(new DefenseCard(*dynamic_cast<DefenseCard*>(selected)));
} else if (dynamic_cast<UtilityCard*>(selected)) {
player.addCardToDeck(new UtilityCard(*dynamic_cast<UtilityCard*>(selected)));
}
cout << "你获得了 " << selected->name << " 卡牌!\n";
} else {
cout << "购买取消。\n";
}
}
void upgradeCard() {
if (player.deck.empty()) {
cout << "你的牌组中没有卡牌可以升级!\n";
return;
}
cout << "\n选择要升级的卡牌:\n";
for (int i = 0; i < min(5, static_cast<int>(player.deck.size())); i++) {
cout << i + 1 << ". " << player.deck[i]->name << " - " << player.deck[i]->description << "\n";
}
cout << "0. 取消\n";
cout << "选择: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= min(5, static_cast<int>(player.deck.size()))) {
Card* card = player.deck[choice - 1];
if (dynamic_cast<AttackCard*>(card)) {
AttackCard* attackCard = dynamic_cast<AttackCard*>(card);
attackCard->damage += 3;
cout << attackCard->name << " 伤害提升至 " << attackCard->damage << "!\n";
} else if (dynamic_cast<DefenseCard*>(card)) {
DefenseCard* defenseCard = dynamic_cast<DefenseCard*>(card);
defenseCard->defense += 3;
cout << defenseCard->name << " 护甲提升至 " << defenseCard->defense << "!\n";
} else if (dynamic_cast<UtilityCard*>(card)) {
UtilityCard* utilityCard = dynamic_cast<UtilityCard*>(card);
utilityCard->healAmount += 3;
cout << utilityCard->name << " 治疗量提升至 " << utilityCard->healAmount << "!\n";
}
} else {
cout << "升级取消。\n";
}
}
void endGame() {
cout << "\n================================\n";
if (victory) {
cout << " 恭喜你击败了巨龙! \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;
}