彩票中奖游戏
2025-08-08 17:11:48
发布于:广东
求关注、点赞!!!
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
using namespace std;
struct GameData {
double cash = 100.0;
double bankBalance = 0.0;
double bankInterest = 0.02;
time_t lastInterestDate = 0;
time_t lastDepositDate = 0;
double todayDeposited = 0.0;
time_t lastSaveTime = 0;
vector<string> history;
string saveCode = "";
};
GameData gameData;
void color(int color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void clearScreen() {
system("cls");
}
void pause() {
system("pause");
}
string generateSaveCode() {
const string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string result;
srand(time(NULL));
for (int i = 0; i < 10; i++)
result += charset[rand() % charset.length()];
return result;
}
void saveGame() {
if (gameData.saveCode.empty())
gameData.saveCode = generateSaveCode();
gameData.lastSaveTime = time(NULL);
ofstream outfile("lottery_save.txt");
if (outfile.is_open()) {
outfile << gameData.saveCode << endl;
outfile << gameData.cash << endl;
outfile << gameData.bankBalance << endl;
outfile << gameData.lastInterestDate << endl;
outfile << gameData.lastDepositDate << endl;
outfile << gameData.todayDeposited << endl;
for (const auto &record : gameData.history)
outfile << record << endl;
outfile.close();
cout << "游戏已保存!您的存档码是: " << gameData.saveCode << endl;
} else {
cout << "无法保存游戏!" << endl;
}
pause();
}
bool loadGame() {
string inputCode;
clearScreen();
cout << "请输入您的存档码: ";
cin >> inputCode;
ifstream infile("lottery_save.txt");
if (infile.is_open()) {
string savedCode;
getline(infile, savedCode);
if (inputCode == savedCode) {
infile >> gameData.cash;
infile >> gameData.bankBalance;
infile >> gameData.lastInterestDate;
infile >> gameData.lastDepositDate;
infile >> gameData.todayDeposited;
infile.ignore();
gameData.history.clear();
string record;
while (getline(infile, record))
gameData.history.push_back(record);
gameData.saveCode = savedCode;
time_t currentTime = time(NULL);
struct tm *currentTM = localtime(¤tTime);
struct tm *lastDepositTM = localtime(&gameData.lastDepositDate);
if (currentTM->tm_yday != lastDepositTM->tm_yday ||
currentTM->tm_year != lastDepositTM->tm_year) {
gameData.todayDeposited = 0.0;
gameData.lastDepositDate = currentTime;
}
cout << "游戏加载成功!" << endl;
pause();
return true;
} else {
cout << "存档码不匹配!" << endl;
pause();
return false;
}
} else {
cout << "没有找到存档文件!" << endl;
pause();
return false;
}
}
void calculateBankInterest() {
time_t currentTime = time(NULL);
ifstream timefile("bank_time.dat", ios::binary);
if (timefile.is_open()) {
timefile.read((char *)&gameData.lastInterestDate, sizeof(time_t));
timefile.close();
}
if (gameData.lastSaveTime > gameData.lastInterestDate)
gameData.lastInterestDate = gameData.lastSaveTime;
if (gameData.lastInterestDate == 0) {
gameData.lastInterestDate = currentTime;
ofstream outfile("bank_time.dat", ios::binary);
if (outfile.is_open()) {
outfile.write((char *)¤tTime, sizeof(time_t));
outfile.close();
}
return;
}
double seconds = difftime(currentTime, gameData.lastInterestDate);
double days = seconds / (60 * 60 * 24);
if (days >= 1) {
for (int i = 0; i < (int)days; i++) {
double interest = gameData.bankBalance * gameData.bankInterest;
if (interest > 0.01) {
gameData.bankBalance += interest;
stringstream ss;
ss << fixed << setprecision(2) << interest;
string record = "银行日利息: +" + ss.str() + "美元";
gameData.history.push_back(record);
}
}
gameData.lastInterestDate = currentTime;
ofstream outfile("bank_time.dat", ios::binary);
if (outfile.is_open()) {
outfile.write((char *)¤tTime, sizeof(time_t));
outfile.close();
}
}
}
void bankDeposit() {
const double MIN_DEPOSIT = 10.0;
const double DAILY_DEPOSIT_LIMIT = 1000.0;
time_t currentTime = time(NULL);
struct tm *timeinfo = localtime(¤tTime);
if (gameData.lastDepositDate != timeinfo->tm_yday) {
gameData.todayDeposited = 0.0;
gameData.lastDepositDate = timeinfo->tm_yday;
}
clearScreen();
color(14);
cout << "════════════════ 银行存款 ════════════════\n";
color(7);
cout << "当前现金: " << gameData.cash << "美元\n";
cout << "银行存款: " << gameData.bankBalance << "美元\n";
cout << "最低存款额: " << MIN_DEPOSIT << "美元\n";
cout << "单日存款限额: " << DAILY_DEPOSIT_LIMIT << "美元\n";
cout << "今日已存款: " << gameData.todayDeposited << "/" << DAILY_DEPOSIT_LIMIT << "美元\n";
cout << "请输入存款金额: ";
double amount;
cin >> amount;
if (amount < MIN_DEPOSIT) {
cout << "存款金额不能少于 " << MIN_DEPOSIT << "美元!\n";
} else if (amount > gameData.cash) {
cout << "现金不足!\n";
} else if (gameData.todayDeposited + amount > DAILY_DEPOSIT_LIMIT) {
cout << "超过每日存款限额!今日还可存款: "
<< DAILY_DEPOSIT_LIMIT - gameData.todayDeposited << "美元\n";
} else {
gameData.cash -= amount;
gameData.bankBalance += amount;
gameData.todayDeposited += amount;
stringstream ss;
ss << fixed << setprecision(2) << amount;
string record = "银行存款: +" + ss.str() + "美元";
gameData.history.push_back(record);
cout << "存款成功!\n";
}
pause();
}
void bankWithdraw() {
const double MIN_WITHDRAW = 10.0;
const double FEE_RATE = 0.01;
clearScreen();
color(14);
cout << "════════════════ 银行取款 ════════════════\n";
color(7);
cout << "银行存款: " << gameData.bankBalance << "美元\n";
cout << "最低取款额: " << MIN_WITHDRAW << "美元\n";
cout << "手续费: " << FEE_RATE * 100 << "%\n";
cout << "请输入取款金额: ";
double amount;
cin >> amount;
if (amount < MIN_WITHDRAW)
cout << "取款金额不能少于 " << MIN_WITHDRAW << "美元!\n";
else if (amount > gameData.bankBalance)
cout << "银行存款不足!\n";
else {
double fee = amount * FEE_RATE;
double received = amount - fee;
gameData.bankBalance -= amount;
gameData.cash += received;
stringstream ss1, ss2;
ss1 << fixed << setprecision(2) << amount;
ss2 << fixed << setprecision(2) << fee;
string record = "银行取款: -" + ss1.str() + "美元 (手续费" + ss2.str() + "美元)";
gameData.history.push_back(record);
cout << "取款成功!实际到账: " << received << "美元 (手续费: " << fee << "美元)\n";
}
pause();
}
void showBankBalance() {
clearScreen();
color(14);
cout << "════════════════ 账户余额 ════════════════\n";
color(7);
cout << "当前现金: " << fixed << setprecision(2) << gameData.cash << "美元\n";
cout << "银行存款: " << gameData.bankBalance << "美元\n";
cout << "日利率: " << gameData.bankInterest * 100 << "%\n";
cout << "══════════════════════════════════════════\n";
pause();
}
void bankMenu() {
while (true) {
clearScreen();
color(11);
cout << "════════════════ 银行服务 ════════════════\n";
color(7);
cout << "1. 存款 (现金 → 银行)\n";
cout << "2. 取款 (银行 → 现金)\n";
cout << "3. 查看余额\n";
cout << "4. 返回主菜单\n";
cout << "══════════════════════════════════════════\n";
cout << "当前现金: " << fixed << setprecision(2) << gameData.cash << "美元\n";
cout << "银行存款: " << gameData.bankBalance << "美元\n";
cout << "日利率: " << gameData.bankInterest * 100 << "%\n";
cout << "══════════════════════════════════════════\n";
cout << "请选择: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
bankDeposit();
break;
case 2:
bankWithdraw();
break;
case 3:
showBankBalance();
break;
case 4:
return;
default:
cout << "无效选择!";
Sleep(1000);
}
}
}
void showHistory() {
clearScreen();
color(14);
cout << "════════════════ 账本 ════════════════\n";
color(7);
if (gameData.history.empty())
cout << "暂无历史记录\n";
else
for (const auto &record : gameData.history)
cout << record << endl;
cout << "══════════════════════════════════════════\n";
pause();
}
void welcome() {
system("color 0A");
for (int i = 0; i < 3; i++) {
clearScreen();
cout << "\n\n\n";
cout << " ★★☆★★☆★★☆★★☆★★\n";
cout << " ☆ ★\n";
cout << " ★ 彩票开奖系统 ☆\n";
cout << " ☆ ★\n";
cout << " ★★☆★★☆★★☆★★☆★★\n";
Sleep(500);
system("color 0B");
Sleep(500);
}
system("color 0F");
}
int menu() {
clearScreen();
color(11);
cout << "══════════════════════════════\n";
cout << " 彩票游戏 v3.0 \n";
cout << " 当前现金: " << fixed << setprecision(2) << gameData.cash << "美元\n";
cout << " 银行存款: " << gameData.bankBalance << "美元\n";
cout << "══════════════════════════════\n";
color(7);
cout << "1. 开始游戏\n";
cout << "2. 银行服务\n";
cout << "3. 游戏规则\n";
cout << "4. 历史记录\n";
cout << "5. 保存游戏\n";
cout << "6. 加载游戏\n";
cout << "7. 退出游戏\n";
cout << "══════════════════════════════\n";
cout << "请选择: ";
int a;
cin >> a;
return a;
}
void rules() {
clearScreen();
color(14);
cout << "════════════════ 游戏规则 ════════════════\n";
color(7);
cout << "1. 系统随机生成6个1-49的数字作为中奖号码\n";
cout << "2. 您需要选择6个不同的数字(1-49)\n";
cout << "3. 匹配的数字越多,奖金越高\n";
cout << "4. 匹配6个数字可获得头奖100万美元!\n";
cout << "5. 每注彩票花费2美元现金\n";
cout << "6. 银行存款有利息(年利率2%)\n";
cout << "7. 银行取款收取1%手续费\n";
color(14);
cout << "══════════════════════════════════════════\n";
color(7);
pause();
}
vector<int> generateNumbers(int count, int max) {
vector<int> v;
srand(time(NULL));
while (v.size() < count) {
int num = rand() % max + 1;
if (find(v.begin(), v.end(), num) == v.end())
v.push_back(num);
}
sort(v.begin(), v.end());
return v;
}
void drawAnimation(const vector<int> &v) {
clearScreen();
cout << "\n\n";
color(12);
cout << " 正在开奖...\n\n";
color(7);
vector<int> v2;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < 3; j++) {
cout << "\r ";
for (int k = 0; k <= i; k++) {
color(10);
cout << v[k] << " ";
color(7);
}
for (int k = 0; k < 3; k++)
cout << (rand() % 49 + 1) << " ";
Sleep(100);
}
v2.push_back(v[i]);
cout << "\r ";
for (int num : v2) {
color(10);
cout << num << " ";
color(7);
}
cout << " \n";
Sleep(800);
}
}
void playGame() {
calculateBankInterest();
const double TICKET_PRICE = 2.0;
if (gameData.cash < TICKET_PRICE) {
cout << "现金不足!每注彩票需要" << TICKET_PRICE << "美元。\n";
pause();
return;
}
clearScreen();
SetConsoleTitle(TEXT("彩票游戏 - 选择号码"));
color(11);
cout << "════════════════ 选择号码 ════════════════\n";
color(7);
cout << "每注彩票花费" << TICKET_PRICE << "美元现金\n";
cout << "当前现金: " << gameData.cash << "美元\n\n";
cout << "请输入6个不同的数字(1-49),用空格分隔:\n";
vector<int> userNumbers;
for (int i = 0; i < 6;) {
int num;
cin >> num;
if (num < 1 || num > 49) {
cout << "请输入1-49之间的数字!\n";
continue;
}
if (find(userNumbers.begin(), userNumbers.end(), num) != userNumbers.end()) {
cout << "不能重复选择相同数字!\n";
continue;
}
userNumbers.push_back(num);
i++;
}
sort(userNumbers.begin(), userNumbers.end());
clearScreen();
color(14);
cout << "您选择的号码是: ";
color(10);
for (int num : userNumbers)
cout << num << " ";
cout << "\n\n";
color(7);
pause();
gameData.cash -= TICKET_PRICE;
string record = "购买彩票: -" + to_string(TICKET_PRICE) + "美元 (现金) (号码: ";
for (int num : userNumbers)
record += to_string(num) + " ";
record += ")";
gameData.history.push_back(record);
vector<int> winningNumbers = generateNumbers(6, 49);
SetConsoleTitle(TEXT("彩票游戏 - 开奖中"));
drawAnimation(winningNumbers);
int matchCount = 0;
for (int num : userNumbers) {
if (find(winningNumbers.begin(), winningNumbers.end(), num) != winningNumbers.end()) {
matchCount++;
}
}
clearScreen();
color(14);
cout << "════════════════ 开奖结果 ════════════════\n";
color(7);
cout << "中奖号码: ";
color(12);
for (int num : winningNumbers)
cout << num << " ";
cout << "\n\n";
color(7);
cout << "您的号码: ";
color(11);
for (int num : userNumbers) {
if (find(winningNumbers.begin(), winningNumbers.end(), num) != winningNumbers.end())
color(10);
cout << num << " ";
color(11);
}
cout << "\n\n";
color(14);
if (matchCount)
cout << "您匹配了 " << matchCount << " 个号码!\n";
else
cout << "您没有匹配到号码。\n";
double prize = 0;
color(12);
switch (matchCount) {
case 6:
prize = 1000000;
cout << "恭喜!您中了头奖!奖金" << prize << "美元!\n";
break;
case 5:
prize = 10000;
cout << "二等奖!奖金" << prize << "美元!\n";
break;
case 4:
prize = 100;
cout << "三等奖!奖金" << prize << "美元!\n";
break;
case 3:
prize = 10;
cout << "四等奖!奖金" << prize << "美元!\n";
break;
case 2:
prize = 2;
cout << "五等奖!获得免费彩票一张!\n";
break;
default:
cout << "很遗憾,您没有中奖\n";
}
if (prize > 0) {
gameData.cash += prize;
stringstream ss;
ss << fixed << setprecision(2) << prize;
string prizeRecord = "中奖: +" + ss.str() + "美元 (现金) (匹配" + to_string(matchCount) + "个号码)";
gameData.history.push_back(prizeRecord);
}
color(7);
cout << "当前现金: " << gameData.cash << "美元\n";
cout << "银行存款: " << gameData.bankBalance << "美元\n";
pause();
}
int main() {
string password;
int cnt = 0;
printf("请输入游戏启动密码:");
cin >> password;
while (password != "1qaz2wsx") {
clearScreen();
cnt++;
if (cnt == 5) {
printf("请1分钟后再尝试。");
Sleep(1000 * 60);
}
cout << "密码错误,请重新输入:";
cin >> password;
}
ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
clearScreen();
SetConsoleTitle(TEXT("彩票游戏 v3.1"));
welcome();
while (true) {
int choice = menu();
switch (choice) {
case 1:
playGame();
break;
case 2:
bankMenu();
break;
case 3:
rules();
break;
case 4:
showHistory();
break;
case 5:
saveGame();
break;
case 6:
loadGame();
break;
case 7:
clearScreen();
color(11);
cout << "感谢游玩,再见!\n";
color(7);
return 0;
default:
cout << "无效选择,请重新输入!\n";
Sleep(1000);
}
}
return 0;
}
这里空空如也












有帮助,赞一个