代码语法啥的都没有报错,但就是无法运行。能帮我看看问题出在哪里吗?T_T
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
class Player {
public:
Player(std::string name) : name_(name), life_(20) {}
int getLife() const { return life_; }
void setLife(int life) { life_ = life; }
std::string getName() const { return name_; }
// 从牌组中抽一张牌,加入手牌
void drawCard(std::vector<Card>& deck) {
if (deck.empty()) return;
hand_.push_back(deck.back());
deck.pop_back();
}
// 从手牌中打出一张牌,发动该牌的效果
void playCard(int index, Player& player1, Player& player2) {
if (index < 0 || index >= hand_.size()) return;
hand_[index].play(player1, player2);
hand_.erase(hand_.begin() + index);
}
virtual void chooseCardToPlay(Player& player1, Player& player2) = 0;
public:
std::string name_;
int life_;
std::vector<Card> hand_;
};
class HumanPlayer : public Player {
public:
HumanPlayer(std::string name) : Player(name) {}
void chooseCardToPlay(Player& player1, Player& player2) override {
// 让人类玩家选择要打出的牌
std::cout << "手牌:" << std::endl;
for (int i = 0; i < hand_.size(); ++i) {
std::cout << i << ": " << hand_[i].getName() << std::endl;
}
int index;
std::cout << "请输入要打出的牌的编号:";
std::cin >> index;
playCard(index, player1, player2);
}
};
class ComputerPlayer : public Player {
public:
ComputerPlayer(std::string name) : Player(name) {}
void chooseCardToPlay(Player& player1, Player& player2) override {
// 让计算机玩家随机选择要打出的牌
std::uniform_int_distribution<> dist(0, hand_.size() - 1);
int index = dist(gen_);
playCard(index, player1, player2);
}
private:
std::mt19937 gen_;
};
class Card {
public:
virtual void play(Player& player1, Player& player2) = 0;
virtual std::string getName() = 0;
};
class PainfulLessonCard : public Card {
public:
void play(Player& player1, Player& player2) override {
// 这张牌的效果:对手失去 2 点生命值并重新抽取一张牌
player2.setLife(player2.getLife() - 2);
player2.drawCard(deck_);
}
std::string getName() override { return "Painful Lesson"; }
public:
std::vector<Card> deck_;
};
class ResentmentCard : public Card {
public:
void play(Player& player1, Player& player2) override {
// 这张牌的效果:两名玩家都失去 1 点生命值
player1.setLife(player1.getLife() - 1);
player2.setLife(player2.getLife() - 1);
}
std::string getName() override { return "Resentment"; }
};
class CompleteHealingCard : public Card {
public:
void play(Player& player1, Player& player2) override {
// 这张牌的效果:将您的健康恢复到 20
player1.setLife(20);
}
std::string getName() override { return "Complete Healing"; }
};
class SwitcherooCard : public Card {
public:
void play(Player& player1, Player& player2) override {
// 这张牌的效果:将你手牌抽取一张与对手的手牌交换
if (player1.hand_.empty() || player2.hand_.empty()) return;
std::swap(player1.hand_, player2.hand_);
}
std::string getName() override { return "Switcheroo"; }
};
class RefreshCard : public Card {
public:
void play(Player& player1, Player& player2) override {
// 这张牌的效果:失去 3 点生命值并将弃牌堆洗回牌组
player1.setLife(player1.getLife() - 3);
deck_.insert(deck_.end(), discardPile_.begin(), discardPile_.end());
discardPile_.clear();
std::shuffle(deck_.begin(), deck_.end(), gen_);
}
std::string getName() override { return "Refresh"; }
private:
std::vector<Card> deck_;
std::vector<Card> discardPile_;
std::mt19937 gen_;
};
class PeekCard : public Card {
public:
void play(Player& player1, Player& player2) override {
// 这张牌的效果:查看双方牌组顶牌
if (deck1_.empty() || deck2_.empty()) return;
std::cout << "你的牌组顶牌:" << deck1_.back().getName() << std::endl;
std::cout << "对手的牌组顶牌:" << deck2_.back().getName() << std::endl;
}
std::string getName() override { return "Peek"; }
private:
std::vector<Card> deck1_;
std::vector<Card> deck2_;
};
class Game {
public:
void start() {
std::cout << "欢迎来到纸牌游戏!" << std::endl;
std::cout << "请输入你的名字:";
std::string name;
std::cin >> name;
player1_ = std::make_unique<HumanPlayer>(name);
player2_ = std::make_unique<ComputerPlayer>("Computer");
// 初始化牌组
deck1_.insert(deck1_.end(), 5, PainfulLessonCard());
deck1_.insert(deck1_.end(), 6, ResentmentCard());
deck1_.insert(deck1_.end(), 1, CompleteHealingCard());
deck1_.insert(deck1_.end(), 2, SwitcherooCard());
deck1_.insert(deck1_.end(), 2, RefreshCard());
deck1_.insert(deck1_.end(), 4, PeekCard());
deck2_ = deck1_; // 复制牌组
std::shuffle(deck1_.begin(), deck1_.end(), gen_);
std::shuffle(deck2_.begin(), deck2_.end(), gen_);
// 开始游戏循环
while (true) {
std::cout << "---新回合开始---" << std::endl;
player1_->drawCard(deck1_);
player2_->drawCard(deck2_);
player1_->chooseCardToPlay(*player1_, *player2_);
player2_->chooseCardToPlay(*player2_, *player1_);
if (player1_->getLife() <= 0 || deck1_.empty()) {
std::cout << player2_->getName() << "胜利!" << std::endl;
break;
}
if (player2_->getLife() <= 0 || deck2_.empty()) {
std::cout << player1_->getName() << "胜利!" << std::endl;
break;
}
}
}
private:
std::vector<Card> deck1_;
std::vector<Card> deck2_;
std::unique_ptr<Player> player1_;
std::unique_ptr<Player> player2_;
std::mt19937 gen_;
};
int main() {
Game game;
game.start();
return 0;
}