卡牌收集者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();
}
}
}