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;
}
}
}