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

卡牌收集者1.0

我与boer_joker联合开发,后面会更新1.1版本,敬请期待! 

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <limits>
#include <algorithm>

using namespace std;

struct Card {
    int number;
    string rank;
  
    int getRankValue() const {
        const vector<pair<string, int>> rankValues = {
            {"F", 10}, {"E", 20}, {"D", 30}, 
            {"C", 40}, {"B", 50}, {"A", 60},
            {"S", 70}, {"SS", 80}, {"SSS", 90}
        };
        auto it = find_if(rankValues.begin(), rankValues.end(),
            [this](const pair<string, int>& p){ return p.first == rank; });
        return (it != rankValues.end()) ? it->second : 0;
    }
};

struct CardPack {
    string name;
    int price;
    int cardCount;
    int minRarity;
    int maxRarity;
    vector<int> weights;
};

vector<Card> inventory;
int money = 1;  // 初始资金设为1
const vector<string> RANKS = {"F","E","D","C","B","A","S","SS","SSS"};

const vector<CardPack> PACKS = {
    {"菜鸡包",  1, 1, 0, 2, {50, 30, 20}},
    {"普通包",  2, 2, 3, 4, {70, 30}},
    {"高级包",  5, 3, 5, 6, {80, 20}},
    {"神级包", 10, 5, 7, 8, {95, 5}}
};

void clearScreen() {
#ifdef _WIN32
    system("cls");
#else
    system("clear");
#endif
}

void showMainMenu() {
    clearScreen();
    cout << "★☆★ 卡牌收集者 ★☆★\n";
    cout << "当前资金: ¥" << money << "\n";
    cout << "════════════════════\n";
    cout << "1. 进入商店\n";
    cout << "2. 查看库存\n";
    cout << "3. 前往交易所\n";
    cout << "0. 退出游戏\n";
    cout << "════════════════════\n";
    cout << "请选择操作: ";
}

void showStore() {
    clearScreen();
    cout << "?? 卡牌商店 ??\n";
    cout << "当前资金: ¥" << money << "\n\n";
  
    for (size_t i = 0; i < PACKS.size(); ++i) {
        cout << i+1 << ". 【" << PACKS[i].name << "】"
             << "\n   价格: ¥" << PACKS[i].price
             << "  获得卡牌: " << PACKS[i].cardCount << "张"
             << "\n   稀有范围: " << RANKS[PACKS[i].minRarity] 
             << "~" << RANKS[PACKS[i].maxRarity]
             << "\n   概率分布: ";
        for (size_t w = 0; w < PACKS[i].weights.size(); ++w) {
            cout << RANKS[PACKS[i].minRarity + w] << " " 
                 << PACKS[i].weights[w] << "%";
            if (w != PACKS[i].weights.size()-1) cout << " / ";
        }
        cout << "\n   ──────────────────────────\n";
    }
    cout << "\n0. 返回主菜单\n";
    cout << "请选择卡包: ";
}

Card drawCard(const CardPack& pack) {
    int totalWeight = accumulate(pack.weights.begin(), pack.weights.end(), 0);
    int random = rand() % totalWeight;
  
    int accumulated = 0;
    for (size_t i = 0; i < pack.weights.size(); ++i) {
        accumulated += pack.weights[i];
        if (random < accumulated) {
            int rankIndex = pack.minRarity + i;
            return {rand() % 10 + 1, RANKS[rankIndex]};
        }
    }
    return {rand() % 10 + 1, RANKS[pack.minRarity]};
}

void purchasePack(int index) {
    const CardPack& pack = PACKS[index];
  
    if (money < pack.price) {
        cout << "资金不足!";
        cin.ignore();
        cin.get();
        return;
    }
  
    money -= pack.price;
    vector<Card> obtainedCards;
  
    for (int i = 0; i < pack.cardCount; ++i) {
        obtainedCards.push_back(drawCard(pack));
    }
  
    clearScreen();
    cout << "?? 抽卡结果 ??\n";
    cout << "获得 " << pack.cardCount << " 张卡牌:\n";
    for (auto& card : obtainedCards) {
        int value = (card.number * card.getRankValue())/2;
        cout << "  [" << card.rank << "-" << card.number 
             << "] 价值: ¥" << value << "\n";
        inventory.push_back(card);
    }
  
    cout << "\n剩余资金: ¥" << money;
    cin.ignore();
    cin.get();
}

void showInventory() {
    clearScreen();
    cout << "?? 我的库存 \n";
    cout << "当前持有卡牌: " << inventory.size() << "张\n";
    cout << "════════════════════\n";
  
    if (inventory.empty()) {
        cout << "库存为空!\n";
    } else {
        for (size_t i = 0; i < inventory.size(); ++i) {
            int value = (inventory[i].number * inventory[i].getRankValue())/2;
            cout << i+1 << ". " << inventory[i].rank 
                 << "-" << inventory[i].number
                 << "  价值: ¥" << value << "\n";
        }
    }
  
    cout << "════════════════════\n";
    cout << "输入0返回: ";
    int choice;
    cin >> choice;
}

void showExchange() {
    clearScreen();
    cout << "?? 交易所 \n";
    cout << "当前资金: ¥" << money << "\n";
    cout << "════════════════════\n";
  
    if (inventory.empty()) {
        cout << "?? 库存为空!\n";
    } else {
        cout << "可出售卡牌:\n";
        for (size_t i = 0; i < inventory.size(); ++i) {
            int value = (inventory[i].number * inventory[i].getRankValue())/2;
            cout << i+1 << ". [" << inventory[i].rank << "-" << inventory[i].number
                 << "] 回收价: ¥" << value << "\n";
        }
    }
  
    cout << "════════════════════\n";
    cout << "输入卡牌编号出售(0返回): ";
    int choice;
    cin >> choice;
  
    if (choice == 0) return;
  
    if (choice < 1 || choice > static_cast<int>(inventory.size())) {
        cout << "无效选择!";
        cin.ignore();
        cin.get();
        return;
    }
  
    Card sold = inventory[choice-1];
    int gain = (sold.number * sold.getRankValue()) / 2;
    money += gain;
    inventory.erase(inventory.begin() + choice - 1);
  
    cout << "\n成功售出 " << sold.rank << "-" << sold.number 
         << " 获得 ¥" << gain << "!";
    cin.ignore();
    cin.get();
}

int main() {
    srand(time(0));
  
    while (true) {
        showMainMenu();
        int mainChoice;
        cin >> mainChoice;
      
        if (cin.fail()) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            continue;
        }
      
        switch (mainChoice) {
            case 1: {
                while (true) {
                    showStore();
                    int storeChoice;
                    cin >> storeChoice;
                  
                    if (storeChoice == 0) break;
                  
                    if (storeChoice >= 1 && storeChoice <= static_cast<int>(PACKS.size())) {
                        purchasePack(storeChoice - 1);
                    }
                }
                break;
            }
            case 2:
                showInventory();
                break;
            case 3:
                showExchange();
                break;
            case 0:
                cout << "感谢游玩!\n";
                return 0;
            default:
                cout << "无效选择!";
                cin.ignore();
                cin.get();
        }
    }
}

相关文章:

  • JMH 基准测试实战:Java 性能对比的正确打开方式!
  • sqlite3基本语句
  • BUUCTF-web刷题篇(17)
  • Three.js 入门实战:安装、基础概念与第一个场景⭐
  • go语言应该如何学习
  • SQL:JOIN 完全指南:从基础到实战应用
  • EFA-YOLO:一种高效轻量的火焰检测模型解析
  • 【期中准备】电路基础(西电)
  • MySQL事务管理
  • 3 版本控制:GitLab、Jenkins 工作流及分支开发模式实践
  • Kubernetes 深入浅出系列 | 容器剖析之容器安全
  • 链路聚合+vrrp
  • 写给新人的深度学习扫盲贴:ReLu和梯度
  • DocLayout-YOLO:通过多样化合成数据与全局-局部感知实现文档布局分析突破
  • 【Java内存区域有什么?每个区域有什么作用?】
  • 跨站脚本攻击(XSS)与跨站请求伪造(CSRF)的介绍、区别和预防
  • 程序化广告行业(74/89):行业发展驱动因素与未来展望
  • 帆软fvs文件中某表格新增数据来声提醒
  • Kotlin日常使用函数记录
  • JavaScript逆向工程实战:如何精准定位加密参数生成位置
  • 有了网站怎么做app/百度指数排名
  • 王也天的个人资料/网络seo
  • 京东怎么做轮播图链接网站/排超最新积分榜
  • 北京网梯科技发展有限公司/seo优化软件大全
  • 深圳住房与建设部网站/搜索引擎优化的方式
  • 网站建设的公司服务/百度认证