抽卡小游戏

hello大家好啊,我是文宇,不是文字,是文宇。一段时间没写小游戏了,写了个小游戏玩玩

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

// 卡牌类
class Card {
public:
    string name;
    int rarity; // 稀有度

    Card(string name, int rarity) {
        this->name = name;
        this->rarity = rarity;
    }
};

// 玩家类
class Player {
public:
    string name;
    int coins;
    vector<Card> collection;

    Player(string name) {
        this->name = name;
        coins = 0;
    }

    // 添加卡牌到收藏中
    void addCard(Card card) {
        collection.push_back(card);
    }

    // 打印收藏中的卡牌
    void printCollection() {
        cout << "玩家 " << name << " 的收藏中有以下卡牌:" << endl;
        for (Card card : collection) {
            cout << card.name << "(稀有度:" << card.rarity << ")" << endl;
        }
    }
};

// 抽卡游戏类
class CardGame {
public:
    vector<Card> cards;
    Player player;

    CardGame(string playerName) : player(playerName) {
        srand(time(0));
    }

    // 初始化卡牌
    void initCards() {
        cards.push_back(Card("牌1", 1));
        cards.push_back(Card("牌2", 2));
        cards.push_back(Card("牌3", 3));
        cards.push_back(Card("牌4", 2));
        cards.push_back(Card("牌5", 1));
        cards.push_back(Card("牌6", 3));
        cards.push_back(Card("牌7", 2));
        cards.push_back(Card("牌8", 1));
        cards.push_back(Card("牌9", 3));
        cards.push_back(Card("牌10", 2));
    }

    // 抽卡
    void drawCard() {
        if (player.coins < 1) {
            cout << "玩家 " << player.name << " 的金币不足,无法抽卡!" << endl;
            return;
        }

        player.coins--;

        int randomIndex = rand() % cards.size();
        Card card = cards[randomIndex];
        player.addCard(card);

        cout << "玩家 " << player.name << " 抽到了 " << card.name << "(稀有度:" << card.rarity << ")" << endl;
    }

    // 打印玩家信息
    void printPlayerInfo() {
        cout << "玩家 " << player.name << " 的信息:" << endl;
        cout << "金币数量:" << player.coins << endl;
        player.printCollection();
    }
};

int main() {
    cout << "欢迎来到抽卡游戏!" << endl;

    cout << "请输入玩家名称:";
    string playerName;
    getline(cin, playerName);

    CardGame game(playerName);
    game.initCards();

    while (true) {
        cout << endl;
        cout << "请选择操作:" << endl;
        cout << "1. 抽卡" << endl;
        cout << "2. 查看玩家信息" << endl;
        cout << "0. 退出游戏" << endl;

        int choice;
        cin >> choice;
        cin.ignore(); // 忽略之前的回车键

        switch (choice) {
            case 1:
                game.drawCard();
                break;
            case 2:
                game.printPlayerInfo();
                break;
            case 0:
                cout << "感谢游玩抽卡游戏!" << endl;
                return 0;
            default:
                cout << "无效的选择!请重新输入。" << endl;
        }
    }
}

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文宇炽筱

有一个打赏就多写十篇文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值