跳到正文

休闲·娱乐

卡片游戏

Lzc20100803约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年6月2日
篇幅
约 9 分钟
获赞
1

版本

  1. v1当前

#底部有详细游玩步骤

全部代码:

C++
#include <bits/stdc++.h>

using namespace std;

struct Card {
	string name;
	string rarity;
	int value;
};

class CardPack {
public:
	string name;
	int price;
	vector<Card> cards;
	vector<int> weights;
	int totalWeight;

	CardPack(string name, int price) : name(name), price(price), totalWeight(0) {}

	void addCard(Card card, int weight) {
		cards.push_back(card);
		weights.push_back(weight);
		totalWeight += weight;
	}

	Card drawCard() {
		if(totalWeight == 0) return {"", "", 0};
		int random = rand() % totalWeight;
		int cumulative = 0;
		for (int i = 0; i < cards.size(); ++i) {
			cumulative += weights[i];
			if (random < cumulative) {
				return cards[i];
			}
		}
		return cards.back();
	}
};

class Game {
private:
	int currency;
	vector<Card> inventory;
	vector<CardPack> packs;

public:
	Game(int initialCurrency) : currency(initialCurrency) {
		initializePacks();
	}

	void initializePacks() {
		// N卡包(极常见)
		CardPack nPack("N Card Pack", 10);
		nPack.addCard({"N1",  "N",  5},  40);
		nPack.addCard({"N2",  "N",  15}, 30);
		nPack.addCard({"N3",  "N",  30}, 30);
		packs.push_back(nPack);

		// R卡包(常见)
		CardPack rPack("R Card Pack", 100);
		rPack.addCard({"R1",  "R",  50},  60);
		rPack.addCard({"R2",  "R",  150}, 25);
		rPack.addCard({"R3",  "R",  200}, 15);
		packs.push_back(rPack);

		// SR卡包(稀有)
		CardPack srPack("SR Card Pack", 300);
		srPack.addCard({"SR1", "SR", 200}, 70);
		srPack.addCard({"SR2", "SR", 400}, 20);
		srPack.addCard({"SR3", "SR", 600}, 10);
		packs.push_back(srPack);

		// SSR卡包(超稀有)
		CardPack ssrPack("SSR Card Pack", 500);
		ssrPack.addCard({"SSR1", "SSR", 350}, 80);
		ssrPack.addCard({"SSR2", "SSR", 800}, 15);
		ssrPack.addCard({"SSR3", "SSR", 1500}, 5);
		packs.push_back(ssrPack);

		// 陶子涵卡包(?)
		CardPack zhPack("陶子涵 Card Pack", 1000);
		zhPack.addCard({"路人子涵", "的卡片", 700},  70);
		zhPack.addCard({"小螃蟹2",  "的卡片", 1500}, 29);
		zhPack.addCard({"炮芯大怪", "的卡片", 64800}, 1);
		packs.push_back(zhPack);
	}

	void buyPack(int packIndex) {
		if (packIndex < 0 || packIndex >= packs.size()) {
			cout << "无效的卡包选择!" << endl;
			return;
		}
		CardPack& pack = packs[packIndex];
		if (currency < pack.price) {
			cout << "货币不足!" << endl;
			return;
		}
		currency -= pack.price;
		Card card = pack.drawCard();
		inventory.push_back(card);
		cout << "抽到卡片:" << card.name << " (" << card.rarity << "),价值"
		<< card.value << "货币" << endl;
	}

	void displayInventory() {
		cout << "\n当前库存:" << endl;
		if (inventory.empty()) {
			cout << "库存为空" << endl;
			return;
		}
		for (int i = 0; i < inventory.size(); ++i) {
			cout << i+1 << ". " << inventory[i].name << " (" << inventory[i].rarity
			<< ") - 价值:" << inventory[i].value << endl;
		}
	}

	void exchangeCard(int index) {
		if (index < 0 || index >= inventory.size()) {
			cout << "无效的卡片索引!" << endl;
			return;
		}
		Card card = inventory[index];
		currency += card.value;
		inventory.erase(inventory.begin() + index);
		cout << "已兑换 " << card.name << " 获得 " << card.value << " 货币" << endl;
	}

	void showCurrency() {
		cout << "\n当前货币: " << currency << endl;
	}

	vector<CardPack>& getPacks() {
		return packs;
	}
};

int main() {
	srand(time(0));
	Game game(1000);

	while (true) {
		cout << "\n===== 抽卡游戏菜单 =====" << endl;
		cout << "1. 购买卡包" << endl;
		cout << "2. 查看库存" << endl;
		cout << "3. 兑换卡片" << endl;
		cout << "4. 退出游戏" << endl;
		cout << "请输入选项:";

		int choice;
		cin >> choice;

		if (choice == 1) {
			game.showCurrency();
			cout << "\n可购买卡包:" << endl;
			vector<CardPack>& packs = game.getPacks();
			for (int i = 0; i < packs.size(); ++i) {
				cout << i+1 << ". " << packs[i].name
				<< " - 价格: " << packs[i].price << endl;
			}
			cout << "请选择卡包:";
			int packChoice;
			cin >> packChoice;
			game.buyPack(packChoice - 1);
			game.showCurrency();
		} else if (choice == 2) {
			game.displayInventory();
		} else if (choice == 3) {
			game.displayInventory();
			if (!game.getPacks().empty()) {
				cout << "输入要兑换的卡片编号:";
				int index;
				cin >> index;
				game.exchangeCard(index - 1);
				game.showCurrency();
			}
		} else if (choice == 4) {
			break;
		} else {
			cout << "无效的选项!" << endl;
		}
		cin.get();cin.get();
		system("cls");
	}

	return 0;
}

游玩教程

首先,点击下载c++

下好之后打开

将代码复制到c++上然后点这里

等待弹窗后就可以玩了 \^~^/

原文