问题标题: 矿工模拟器5.0

0
0
已解决
段辰曦
段辰曦
初级守护
初级守护

#include<bits/stdc++.h>
#include<Windows.h>
#include<conio.h>   
#include<limits>
using namespace std;

// 全局变量 - 统一初始化,增加注释
string username; 
double sum=0.0;      // 改为double避免精度丢失
int xl=100;          // 血量
int jin=0;           // 金币
int p=0;             // 挖矿状态标记
int sd=900;          // 挖掘速度(延迟)
int st[9]={900,800,700,580,430,310,170,90,5}; // 镐子速度数组(索引0-8对应1-9级)
double bs=1.0;       // 倍率
int bt=0,zxq=0,mxd=0,hj=0; // 绷带、注血器、满血丹、合金钻头
double bss[9]={1,1.5,2,3,5,6,9,15,22}; // 倍率数组(索引0-8对应1-9级)
int jg[9]={0,30,150,700,2500,10000,40000,100000,300000}; // 价格数组(索引0-8对应1-9级)
int pickaxe_level=1; // 镐子等级

// 排行榜用户结构体
struct RankUser {
    string name;    // 用户名
    int gold;       // 金币数
    // 排序规则:金币多的排前面
    bool operator<(const RankUser& other) const {
        return gold > other.gold;
    }
};

// 安全获取字符输入
char fb(){
    return _getch(); 
}

// 字符转数字
int czi(char a){
    return a-'0';
}

// 挖矿结果处理函数
void wk(int t){
    if(t == -1){ // 已挖过的位置
        cout<<"你曾经挖过这里"; 
    }else if(t<=10){
        cout<<"你挖到了岩浆,烫**了!";
        xl=0;
    }else if(t<=30){
        cout<<"恭喜你,挖到了钻石!";
        sum+=500*bs; 
    }else if(t<=50){
        cout<<"你遇到了怪物,因此而亡";
        xl=0; 
    }else if(t<=100){
        cout<<"你遇到了怪物,因此重伤";
        xl-=60; 
    }else if(t<=180){
        cout<<"你遇到了怪物,因此**";
        xl-=20; 
    }else if(t<=250){
        cout<<"你挖到了金矿石!";
        sum+=200*bs;
    }else if(t<=380){
        cout<<"你挖到了铁矿石!";
        sum+=100*bs; 
    }else if(t<=500){
        cout<<"你挖到了:泥土";
        sum+=bs; 
    }else if(t<=700){
        cout<<"你挖到了:岩石";
        sum+=bs; 
    }else if(t<=770){
        cout<<"你挖到了青金石!";
        sum+=80*bs; 
    }else if(t<=790){
        cout<<"你挖空了,摔了一跤!";
        xl-=5;
    }else if(t<=800){
        cout<<"你失足了,摔亡了!";
        xl=0;
    }else if(t<=840){
        cout<<"你挖到了水,需要花一些时间弄干\n";
        Sleep(2600);
        if(t==840){
            cout<<"你不小心呛**了!";
            xl=0; 
        }else{
            cout<<"衣服干了,继续前进"; 
        }
    }else if(t<=900){
        cout<<"你挖到了:铜";
        sum+=50*bs;
    }else if(t<910){
        cout<<"你有些累,回家了";
        p=-1;
    }else if(t<=999){
        cout<<"你挖到了:煤炭";
        sum+=30*bs;                     
    }
    
    if(xl<=0){ // 血量小于等于0时直接结束挖矿
        xl=0;
        p=-1;                        
    }

// **用户是否存在
bool isUserExist(string uname, string pwd, int &gold, int &p_level, int &bandage, int &inject, int &full_blood, int &drill) {
    ifstream infile("user_info.txt", ios::in);
    if (!infile.is_open()) {
        return false;
    }

    string u, p;
    int g, pl, b, i, fb, d;
    while (infile >> u >> p >> g >> pl >> b >> i >> fb >> d) {
        if (u == uname) {
            infile.close();
            if (p == pwd) {
                gold = g;
                p_level = pl;
                bandage = b;
                inject = i;
                full_blood = fb;
                drill = d;
                return true;
            } else {
                return false;
            }
        }
    }
    infile.close();
    return false;
}

// 注册用户
bool regUser(string uname, string pwd) {
    int tmp1, tmp2, tmp3, tmp4, tmp5, tmp6;
    if (isUserExist(uname, pwd, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6)) {
        return false;
    }

    ofstream outfile("user_info.txt", ios::app);
    if (!outfile.is_open()) {
        cout << "用户文件创建失败!" << endl;
        Sleep(1000);
        return false;
    }
    
    outfile << uname << " " << pwd << " 0 1 0 0 0 0" << endl;
    outfile.close();
    return true;
}

// 保存用户信息
void saveUserInfo() {
    if (username.empty()) { // 未登录时不保存
        cout << "请先登录!" << endl;
        Sleep(1000);
        return;
    }
    
    vector<vector<string> > allUsers;
    ifstream infile("user_info.txt", ios::in);
    if (!infile.is_open()) {
        cout << "用户文件不存在,无法保存!" << endl;
        return;
    }

    string u, p, g, pl, b, i, fb, d;
    while (infile >> u >> p >> g >> pl >> b >> i >> fb >> d) {
        if (u == username) {
            g = to_string(jin);
            pl = to_string(pickaxe_level);
            b = to_string(bt);
            i = to_string(zxq);
            fb = to_string(mxd);
            d = to_string(hj);
        }
        allUsers.push_back( {u, p, g, pl, b, i, fb, d} );
    }
    infile.close();

    ofstream outfile("user_info.txt", ios::out);
    if (!outfile.is_open()) {
        cout << "保存用户数据失败!" << endl;
        return;
    }
    for (auto &user : allUsers) {
        for (int i=0; i<user.size(); i++) {
            if (i > 0) outfile << " ";
            outfile << user[i];
        }
        outfile << endl;
    }
    outfile.close();
    cout << "用户数据保存成功!" << endl;
    Sleep(1000);
}

// 加载排行榜数据
vector<RankUser> loadRankData() {
    vector<RankUser> rankList;
    ifstream infile("user_info.txt", ios::in);
    if (!infile.is_open()) {
        cout << "暂无用户数据!" << endl;
        return rankList;
    }

    string u, p;
    int g, pl, b, i, fb, d;
    while (infile >> u >> p >> g >> pl >> b >> i >> fb >> d) {
        RankUser user;
        user.name = u;
        user.gold = g;
        rankList.push_back(user);
    }
    infile.close();
    
    // 按金币数降序排序
    sort(rankList.begin(), rankList.end());
    return rankList;
}

// 显示排行榜
void showRankList() {
    system("cls");
    cout << "==================== 矿工排行榜 ====================\n";
    cout << "排名\t用户名\t\t金币数\n";
    cout << "----------------------------------------------------\n";
    
    vector<RankUser> rankList = loadRankData();
    
    if (rankList.empty()) {
        cout << "\n\t\t暂无用户数据,请先注册用户!\n";
    } else {
        // 显示前20名(避免数据过多)
        int showCount = min(20, (int)rankList.size());
        int userRank = -1; // 当前登录用户的排名
        for (int i = 0; i < showCount; i++) {
            // 标记当前登录用户
            bool isCurrentUser = (rankList[i].name == username);
            if (isCurrentUser) {
                userRank = i + 1;
            }
            
            cout << i+1 << "\t" << rankList[i].name;
            // 对齐格式(用户名不足8位补空格)
            if (rankList[i].name.length() < 8) {
                cout << "\t\t";
            } else {
                cout << "\t";
            }
            cout << rankList[i].gold;
            
            // 高亮当前用户
            if (isCurrentUser) {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                cout << " <-- 你";
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
            }
            cout << endl;
        }
        
        // 如果当前用户不在前20名,显示其排名
        if (!username.empty() && userRank == -1) {
            for (int i = 20; i < rankList.size(); i++) {
                if (rankList[i].name == username) {
                    userRank = i + 1;
                    break;
                }
            }
            if (userRank != -1) {
                cout << "...\n";
                cout << userRank << "\t" << username;
                if (username.length() < 8) {
                    cout << "\t\t";
                } else {
                    cout << "\t";
                }
                // 查找当前用户的金币数
                int userGold = 0;
                for (auto& r : rankList) {
                    if (r.name == username) {
                        userGold = r.gold;
                        break;
                    }
                }
                cout << userGold;
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
                cout << " <-- 你";
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                cout << endl;
            }
        }
    }
    
    cout << "\n----------------------------------------------------\n";
    cout << "          按任意键返回主菜单...\n";
    fb(); // 等待按键
    system("cls");
}

// 游戏主函数
void gameMain(){
    // 地形数组 - 缩小尺寸避免卡顿,统一范围
    short int kw[50][20][50] = {0}; 
    cout<<"               欢迎来到矿工模拟器"
    <<"\n\n\n"
    <<"                ________________   \n"
    <<"                |  矿工模拟器  |   \n"
    <<"                ----------------   \n"
    <<"\n\n\n\n\n"
    <<"               按任意键进入\n\n\n       ";
    
    // 等待任意键
    fb();
    system("cls");
    
    while(1){
        system("cls");
        // 显示用户信息(未登录时提示)
        if (username.empty()) {
            cout <<"当前用户:未登录  金币:0  镐子等级:1" << endl;
        } else {
            cout <<"当前用户:"<<username<<"  金币:" << jin << "  镐子等级:" << pickaxe_level << endl;
        }
        cout<<"====================================="<<endl;
        // 新增7.排行榜选项
        cout<<"   1.商城\n   2.开始挖矿\n   3.教程\n   4.退出游戏\n   5.登录\n   6.注册\n   7.排行榜";
        cout<<endl<<"      ";
        
        char ch = fb();
        if(ch=='1'){             //商店页面
            if (username.empty()) {
                cout << "请先登录!" << endl;
                Sleep(1000);
                continue;
            }
            
            while(1){
                system("cls");
                cout<<"金币:"<<jin;
                cout<<"\n\n";
                cout<<"1.一级镐子:30金币     2.二级镐子:150金币     3.三级镐子:700金币     4.四级镐子:2500金币\n";
                cout<<"\n";
                cout<<"5.五级镐子:10000金币  6.六级镐子:40000金币   7.七级镐子:100000金币  8.八级镐子:300000金币\n" ;
                cout<<"\n";
                cout<<"9.绷带(a):600金币     10.注血器(b):1200金币  11.满血丹(c):2800金币  12.合金钻头(d):2000\n";
                cout<<"\n";
                cout<<"\n需要买什么?(按0返回)\n       ";
                
                char o = fb();
                if(o=='0'){
                    break;
                }else if(czi(o)>0&&czi(o)<=8){    
                    int level = czi(o);
                    if(jg[level]<=jin){
                        jin-=jg[level];
                        pickaxe_level = level;
                        sd=st[level-1];
                        bs=bss[level-1];
                        cout << "购买成功!当前镐子等级:" << level << endl;
                        Sleep(500);
                    }else{
                        cout<<"金币不够!\n";
                        Sleep(1000);
                        continue;
                    }
                }else if(o=='a'){
                    if(jin>=600){
                        bt++;
                        jin-=600;
                        cout << "购买绷带成功!当前数量:" << bt << endl;
                        Sleep(500);
                    }else{
                        cout<<"金币不够!\n";
                        Sleep(1000);
                        continue;
                    }
                }else if(o=='b'){
                    if(jin>=1200){
                        zxq++;
                        jin-=1200;
                        cout << "购买注血器成功!当前数量:" << zxq << endl;
                        Sleep(500);
                    }else{
                        cout<<"金币不够!\n";
                        Sleep(1000);
                        continue;
                    }
                }else if(o=='c'){
                    if(jin>=2800){
                        mxd++;
                        jin-=2800;
                        cout << "购买满血丹成功!当前数量:" << mxd << endl;
                        Sleep(500);
                    }else{
                        cout<<"金币不够!\n";
                        Sleep(1000);
                        continue;
                    }
                }else if(o=='d'){
                    if(jin>=2000){
                        hj++;
                        jin-=2000;
                        cout << "购买合金钻头成功!当前数量:" << hj << endl;
                        Sleep(500);
                    }else{
                        cout<<"金币不够!\n";
                        Sleep(1000);
                        continue;
                    }
                }else{
                    continue;
                }
            }
        }else if(ch=='2'){              //挖矿页面
            if (username.empty()) {
                cout << "请先登录!" << endl;
                Sleep(1000);
                continue;
            }
            
            xl=100;
            sum=0.0;
            p=0;
            srand(static_cast<unsigned int>(time(NULL)));
            
            system("cls");
            cout<<"\n  生成地形中···\n\n      加载中 "; 
            // 优化地形生成,减少循环次数
            for(int i=0;i<50;i++){
                if(i%10==0){
                    cout<<"█";
                }
                for(int j=0;j<20;j++){
                    for(int k=0;k<50;k++){
                        kw[i][j][k]=rand()%1000;
                    }
                }
            }
            
            // 初始化位置(避免越界)
            int x=rand()%50;
            int y=rand()%20;
            int z=rand()%50;
            
            while(xl>0){
                // 缓慢回血(每秒1点)
                if(xl<100){
                    xl+=1;
                }
                if(xl>100) xl=100;
                
                system("cls");
                if(p==1){ // 只有移动时才挖矿
                    wk(kw[x%50][y%20][z%50]);
                    kw[x%50][y%20][z%50]=-1; // 标记为已挖
                }
                
                // 挖矿结束条件
                if(p==-1 || xl<=0){
                    p=0;
                    cout<<"\n本次探险共获得"<<static_cast<int>(sum)<<"金币"; 
                    jin+=static_cast<int>(sum);  
                    Sleep(2000);
                    break;                            
                }
                
                // 显示状态
                cout<<"\n位置:   "
                <<"\n  前后:"<<x
                <<"\n  高度:"<<y
                <<"\n  左右:"<<z<<"\n";
                cout<<"  血量:  "<<xl;
                cout<<"\n      要往哪个方向挖?\n\n";
                cout<<"        打开背包按0\n\n      ";
                
                char move_ch = fb();
                if(move_ch=='a'){ // 左
                    z++;
                    p=1;
                }
                else if(move_ch=='d'){ // 右
                    z--;
                    p=1;
                }
                else if(move_ch=='w'){ // 前
                    x++;
                    p=1;
                }
                else if(move_ch=='s'){ // 后
                    x--;
                    p=1;
                }
                else if(move_ch==' '){ // 上
                    y++;
                    p=1;
                }
                else if(move_ch=='x'){ // 下
                    y--;
                    p=1;
                }else if(move_ch=='0'){ // 打开背包
                    system("cls");
                    cout<<"\n\n当前挖矿获得:"<<static_cast<int>(sum)<<"金币\n\n";
                    cout<<"当前物品:";
                    int l=1;
                    if(bt>0){
                        cout<<l<<".绷带(a):"<<bt<<"个 "; 
                        l++;
                    }
                    if(zxq>0){
                        cout<<l<<".注血器(b):"<<zxq<<"个 "; 
                        l++;
                    }
                    if(mxd>0){
                        cout<<l<<".满血丹(c):"<<mxd<<"个 "; 
                        l++;
                    }
                    if(hj>0){
                        cout<<l<<".合金钻头(d):"<<hj<<"个 "; 
                        l++;
                    }
                    if(l==1){
                        cout<<"无";
                    }
                    cout<<"\n\n按n退出背包,按对应键使用道具\n   ";
                    
                    char bag_op = fb();
                    if(bag_op=='n'){
                        p=0;
                        continue; 
                    }else if(bag_op=='a' && bt>0){
                        bt--;
                        xl+=15;
                        if(xl>100) xl=100;
                        cout << "\n使用绷带成功!血量+15,当前血量:" << xl << endl;
                        Sleep(1000);
                    }else if(bag_op=='b' && zxq>0){
                        zxq--;
                        xl+=40;
                        if(xl>100) xl=100;
                        cout << "\n使用注血器成功!血量+40,当前血量:" << xl << endl;
                        Sleep(1000);
                    }else if(bag_op=='c' && mxd>0){
                        mxd--;
                        xl=100;
                        cout << "\n使用满血丹成功!血量已回满!" << endl;
                        Sleep(1000);
                    }else if(bag_op=='d' && hj>0){
                        hj--;
                        cout<<"正在蓄力…\n";
                        Sleep(1000);
                        cout<<"轰!!!!!\n";
                        Sleep(1000);
                        // 向下挖10层
                        for(int i=1;i<=10;i++){
                            y--;
                            wk(kw[x%50][y%20][z%50]);
                            kw[x%50][y%20][z%50]=-1; 
                            cout<<endl;
                        }
                        cout<<"按任意键返回\n"; 
                        fb();
                    }else{
                        cout << "\n道具数量不足或输入错误!" << endl;
                        Sleep(1000);
                    }
                }else if(move_ch=='f'){ // 快速退出
                    cout<<"确定返回?(y/n)\n";
                    char confirm = fb();
                    if(confirm=='y' || confirm=='Y'){
                        jin+=static_cast<int>(sum);
                        break;
                    }
                }else{
                    p=0;
                    continue;
                }
                
                // 挖掘延迟(根据镐子等级)
                system("cls");
                cout<<"挖掘中...";
                Sleep(sd);
            }
        }else if(ch=='3'){ // 教程
            system("cls");
            cout<<"\n\n\n            挖矿操作:\n";
            cout<<"            上:空格   下:x   左:a   右:d   前:w   后:s\n\n";
            cout<<"            打开背包:0   快速退出挖矿:f\n";
            cout<<"            道具使用:背包中按a/b/c/d使用对应道具\n";
            cout<<"\n\n\n            ——按任意键退出——\n     ";
            fb();
        }else if(ch=='4'){ // 退出游戏
            saveUserInfo();
            cout << "感谢游玩矿工模拟器!" << endl;
            Sleep(1000);
            exit(0);
        }else if(ch=='/'){ // 作弊码
            if (username.empty()) {
                cout << "请先登录!" << endl;
                Sleep(1000);
                continue;
            }
            
            cout<<"\n输入作弊码:";
            string cheat;
            cin >> cheat;
            if(cheat=="kgzb") {
                jin=100000000;
                cout << "作弊成功!金币已增加!" << endl;
                Sleep(1000);
            }
            else if(cheat=="dxts"){
                cout << "地形显示功能暂未实现!" << endl;
                Sleep(1000);
            }
        }else if(ch=='5'){ // 登录
            system("cls");
            string uname, pwd;
            cout << "请输入用户名:";
            cin >> uname;
            cout << "请输入密码:";
            pwd.clear();
            
            // 密码输入优化(支持退格)
            char pass_ch;
            while ((pass_ch = _getch()) != '\r') { 
                if (pass_ch == '\b' && !pwd.empty()) {
                    pwd.pop_back();
                    cout << "\b \b";
                } else if (pass_ch != '\b') {
                    pwd.push_back(pass_ch);
                    cout << "*";
                }
            }
            cout << endl;
        
            // 登录验证
            int gold, p_level, bandage, inject, full_blood, drill;
            if (isUserExist(uname, pwd, gold, p_level, bandage, inject, full_blood, drill)) {
                username = uname;
                jin = gold;
                pickaxe_level = p_level;
                bt = bandage;
                zxq = inject;
                mxd = full_blood;
                hj = drill;
                // 更新镐子属**
                sd = st[pickaxe_level-1];
                bs = bss[pickaxe_level-1];
                
                cout << "登录成功!欢迎你," << uname << "!" << endl;
                Sleep(1000);
            }else{
                cout << "用户名不存在或密码错误!" << endl;
                Sleep(1000);
            }
        }else if(ch=='6'){ // 注册
            system("cls");
            string uname, pwd, pwd2;
            cout << "请输入用户名(不含空格):";
            cin >> uname;
            cout << "请输入密码(不含空格):";
            cin >> pwd;
            cout << "请再次输入密码:";
            cin >> pwd2;
        
            if (pwd != pwd2) {
                cout << "两次密码输入不一致!" << endl;
                Sleep(1000);
                continue; // 密码不一致直接返回
            }
        
            if (regUser(uname, pwd)) {
                cout << "注册成功!请返回登录界面登录。" << endl;
                Sleep(1000);
            } else {
                cout << "用户名已存在!" << endl;
                Sleep(1000);
            }    
        }else if(ch=='7'){ // 排行榜
            showRankList();
        }
    }
}

int main(){                               
    SetConsoleTitle(TEXT("____/|\\|/  矿工模拟器  /|\\|/____")); 
    gameMain();
    return 0;
}


0
已采纳
范聿舟
范聿舟
初级光能
初级光能

买不了绷带,建议买东西时确定一下

1
0
0
0
我要回答