跳到正文

Y_Z_Y788

2076514

在洛谷打开
资料、奖项与版本

资料

编号
2076514
抓取

神秘小游戏

仅C++11可用

C++
#include<bits/stdc++.h>
using namespace std;

// 全局常量和变量
const int OPEN_BOX_COST = 50000;  // 开箱费用
const int MAX_HP = 100;           // 血条上限
const int HP_LOSS = 20;           // 钱不够时掉血量
const int REWARD_MONEY = 80000;   // 掉血后赠送的钱
vector<string> candidateList = {
	"黄金杯子  60000¥",
	"黄帝内经  30000¥",
	"水果盘  10000¥",
	"皇家马车  2026888¥",
	"王权之心  1000000¥",
	"帝王翡翠800000¥",
	"非洲之心  9999999¥",
	"海洋之泪  99999999¥",
	"垃圾  1¥",
	"小狗  2¥",
	"小猫  3¥",
	"超级小小猫  6¥",
	"凤凰金雕  200000¥",
	"豪车钥匙  100000¥",
	"小飞机  56789¥",
	"小银鱼  15000¥",
	"82年葡萄汁  15000¥",
	"金表  12000¥",
	"水晶沙漏  15000¥",
	"24k金子  500000¥",
	"电脑  300000¥",
	"大金鱼  250000¥",
	"圣诞树  600000¥",
	"大金圣剑  400000¥",
	"阿萨拉金杯  300000",
	"无人机  600000",
	"金条  600000"
};

// 用户结构体:新增血条字段
struct User {
	string username;
	string password;
	long long balance;  // 玩家余额
	int hp;             // 血条(新增)
};

// ========== 文件操作(存档/读档):适配血条字段 ==========
// 检查用户是否存在
bool userExists(const string& username) {
	ifstream file("users.txt");
	string uname, pwd;
	long long bal;
	int hp;  // 读取血条
	while (file >> uname >> pwd >> bal >> hp) {
		if (uname == username) {
			file.close();
			return true;
		}
	}
	file.close();
	return false;
}

// 注册用户:新用户初始血条为满值
bool registerUser(const string& username, const string& password) {
	if (userExists(username)) {
		cout << "用户名已存在!" << endl;
		return false;
	}
	ofstream file("users.txt", ios::app);  // 追加模式
	file << username << " " << password << " 0 " << MAX_HP << endl;  // 新增血条字段
	file.close();
	cout << "注册成功!初始血条:" << MAX_HP << endl;
	return true;
}

// 登录用户:读取血条
User loginUser(const string& username, const string& password) {
	ifstream file("users.txt");
	string uname, pwd;
	long long bal;
	int hp;
	while (file >> uname >> pwd >> bal >> hp) {
		if (uname == username && pwd == password) {
			file.close();
			return {uname, pwd, bal, hp};
		}
	}
	file.close();
	cout << "用户名或密码错误!" << endl;
	return {"", "", 0, 0};  // 登录失败
}

// 存档:更新余额+血条
void saveUser(const User& user) {
	vector<User> allUsers;
	ifstream inFile("users.txt");
	string uname, pwd;
	long long bal;
	int hp;
	while (inFile >> uname >> pwd >> bal >> hp) {
		if (uname == user.username) {
			allUsers.push_back({uname, pwd, user.balance, user.hp});  // 保存血条
		} else {
			allUsers.push_back({uname, pwd, bal, hp});
		}
	}
	inFile.close();
	
	ofstream outFile("users.txt");
	for (const auto& u : allUsers) {
		outFile << u.username << " " << u.password << " " << u.balance << " " << u.hp << endl;
	}
	outFile.close();
	cout << "存档成功!" << endl;
}

// ========== 原有功能:随机获取奖品 ==========
string getRandomString() {
	if (candidateList.empty()) {
		cerr << "错误:候选字符串列表为空!" << endl;
		return "";
	}
	return candidateList[rand() % candidateList.size()];
}

// ========== 游戏主逻辑:新增血条规则 ==========
void gameMain(User& currentUser) {
	long long sum = currentUser.balance;
	int hp = currentUser.hp;  // 加载血条
	cout << "\n欢迎来到袁至熠摸金大赛!" << endl;
	cout << "当前血条:" << hp << "/" << MAX_HP << " | 当前余额:" << sum << "¥" << endl;
	
	// 新手奖励(仅首次登录/余额为0时触发)
	if (sum == 0) {
		cout << "恭喜获得新手奖励:" << getRandomString() << endl;
		cout << "宝物价值是什么?(必须回答)" << endl;
		long long n;
		cin >> n;
		sum += n;
		currentUser.balance = sum;
		saveUser(currentUser);      // 新手奖励后存档
		cout << "新手奖励后余额:" << sum << "¥" << endl;
	}
	
	// 游戏循环
	while (true) {
		// 先检查血条是否为0
		if (hp <= 0) {
			cout << "\n你的血条已清空!余额被清零!" << endl;
			sum = 0;
			currentUser.balance = sum;
			currentUser.hp = MAX_HP;  // 重置血条
			saveUser(currentUser);
			cout << "血条已重置为:" << MAX_HP << " | 余额:0¥" << endl;
			break;  // 结束游戏
		}
		
		cout << "\n—————— 操作面板 ——————" << endl;
		cout << "当前状态:血条 " << hp << "/" << MAX_HP << " | 余额 " << sum << "¥" << endl;
		cout << "1.开箱(" << OPEN_BOX_COST << "¥)  2.退出  3.查看钱包" << endl;
		int k;
		cin >> k;
		
		if (k == 1) {  // 开箱
			if (sum >= OPEN_BOX_COST) {
				// 钱足够:正常开箱
				sum -= OPEN_BOX_COST;
				cout << "\n开箱成功!恭喜获得:" << getRandomString() << endl;
				cout << "宝物价值是什么?(必须回答)" << endl;
				long long f;
				cin >> f;
				sum += f - 100000;  // 保留你原有的扣减逻辑
				currentUser.balance = sum;
				saveUser(currentUser);
				cout << "开箱后余额:" << sum << "¥" << endl;
			} else {
				// 钱不够:掉血+送钱
				cout << "\n余额不足!掉" << HP_LOSS << "滴血,赠送" << REWARD_MONEY << "¥!" << endl;
				hp -= HP_LOSS;
				sum += REWARD_MONEY;
				
				// 更新用户数据
				currentUser.hp = hp;
				currentUser.balance = sum;
				saveUser(currentUser);
				
				cout << "当前血条:" << hp << "/" << MAX_HP << " | 余额:" << sum << "¥" << endl;
			}
		} else if (k == 2) {  // 退出游戏
			cout << "\n感谢游玩!最终状态:" << endl;
			cout << "血条:" << hp << "/" << MAX_HP << " | 余额:" << sum << "¥" << endl;
			currentUser.balance = sum;
			currentUser.hp = hp;
			saveUser(currentUser);
			break;
		} else if (k == 3) {  // 看钱包
			cout << "\n 当前余额:" << sum << "¥ | 血条:" << hp << "/" << MAX_HP << endl;
		} else {  // 输入错误
			cout << "\n 输入错误!请选择1/2/3" << endl;
		}
	}
}

// ========== 程序入口 ==========
int main() {
	srand(static_cast<unsigned int>(time(0)));
	
	cout << "===== 袁至熠摸金大赛 =====" << endl;
	cout << "1. 登录" << endl;
	cout << "2. 注册" << endl;
	int choice;
	cin >> choice;
	
	User currentUser;
	if (choice == 2) {  // 注册
		string uname, pwd;
		cout << "输入用户名:";
		cin >> uname;
		cout << "输入密码:";
		cin >> pwd;
		if (registerUser(uname, pwd)) {
			currentUser = {uname, pwd, 0, MAX_HP};
		} else {
			return 0;
		}
	} else if (choice == 1) {  // 登录
		string uname, pwd;
		cout << "输入用户名:";
		cin >> uname;
		cout << "输入密码:";
		cin >> pwd;
		currentUser = loginUser(uname, pwd);
		if (currentUser.username.empty()) {
			return 0;
		}
	} else {
		cout << "输入错误!" << endl;
		return 0;
	}
	
	// 登录/注册成功,进入游戏
	gameMain(currentUser);
	
	return 0;
}

还没有收录这位用户的文章。