• 个人简介

    #include #include #include using namespace std;

    int main() { srand(time(0));

    // 玩家属性
    int life = 50, maxLife = 50, attack = 5, defense = 5;
    int gold = 0, potions = 3, sword = 0, armor = 0, room = 0;
    bool gameOver = false;
    
    // 游戏开始
    system("cls");
    cout << "=== 金币迷宫探险 ===\n"
         << "生命:" << life << "/" << maxLife << " 攻击:" << attack << " 防御:" << defense << "\n"
         << "金币:" << gold << " 药水:" << potions << "\n";
    cout << "\n====================\n按回车开始...";
    cin.get();
    
    while(!gameOver) {
        room++;
        system("cls");
        cout << "=== 房间 #" << room << " ===\n"
             << "生命:" << life << "/" << maxLife 
             << " 攻击:" << attack+sword 
             << " 防御:" << defense+armor << "\n"
             << "金币:" << gold << " 药水:" << potions << "\n"
             << "---------------------\n";
    
        switch(rand() % 6) {
            case 0: // 空房间
                cout << "空房间,无事发生";
                break;
                
            case 1: case 2: // 宝箱
                gold += 5 + rand() % 6;
                cout << "发现宝箱!获得" << gold << "金币";
                break;
                
            case 3: // 陷阱
                life-=max(1, 3 - defense - armor);
                cout << "踩到陷阱!生命-" <<max(1, 3 - defense - armor);
                break;
                
            case 4:{ // 战斗
                int mLife = 8 + room/2, mAtk = 1 + room/4;
                cout << "遭遇怪物 (生命:" << mLife << " 攻击:" << mAtk << ")\n";
                //进入回合制直到一方血量为0 
                while(mLife > 0 && life > 0) {
                    cout << "\n你的生命:" << life << " 怪物生命:" << mLife
                         << "\n(A)攻击 (H)药水(" << potions << "瓶): ";
                    
                    char c; cin >> c;
                    if(toupper(c) == 'H') {
                        if(potions > 0) {
                            potions--;
                            life = min(maxLife, life + 5);
                            cout << "使用药水,恢复5生命";
                            continue;
                        } else {
                            cout << "没有药水了!";
                            continue;
                        }
                    }
                    
                    int dmg = max(1, attack + sword - rand() % 2);
                    mLife -= dmg;
                    cout << "你造成" << dmg << "伤害!";
                    
                    if(mLife <= 0) {
                        gold += 10 + mAtk;
                        cout << "\n击败怪物!获得" << (10 + mAtk) << "金币";
                        break;
                    }
                    //25%的概率会暴击 
                    bool crit = rand() % 4 == 0;
                    dmg = max(1, (crit ? mAtk*2 : mAtk) - defense - armor);
                    life -= dmg;
                    cout << (crit ? "\n怪物暴击!" : "\n怪物攻击!") << "受到" << dmg << "伤害";
                }
                //玩家血量为0时游戏结束 
                if(life <= 0) gameOver = true;
                break;
            }
                
            case 5: { // 商店
                while(true) {
                    system("cls");
                    cout << "=== 商店 ===\n金币:" << gold 
                         << "\n1.宝剑升级(15金) 当前:" << sword
                         << "\n2.护甲升级(15金) 当前:" << armor
                         << "\n3.购买药水(5金) 当前:" << potions
                         << "\n0.离开商店\n选择:";
                    
                    int choice; 
                    cin >> choice;
                    
                    if(choice == 0) break;
                    //计算我的选择需要多少钱 
                    int cost = (choice == 3) ? 5 : 15;
                    
                    if(choice >= 1 && choice <= 3) {
                        if(gold >= cost) {
                            gold -= cost;
                            switch(choice) {
                                case 1: sword++; cout << "宝剑升级至+" << sword; break;
                                case 2: armor++; maxLife+=2; life+=2; 
                                       cout << "护甲升级至+" << armor << " 生命+2"; break;
                                case 3: potions++; cout << "药水+1"; break;
                            }
                        } else {
                            cout << "金币不足!需要" << cost << "金(现有" << gold << "金)";
                        }
                    } else {
                        cout << "无效选择!";
                    }
                    
                    cout << "\n按回车继续...";
                    cin.ignore(); cin.get();
                }
                //别忘了结束循环后也要break结束switch 
                break;
            }
        }
        
        if(gameOver) break;
        cout << "\n\n按回车继续...";
        cin.get();
    }
    
    system("cls");
    cout << "=== 游戏结束 ===\n"
         << "探索房间:" << room << "\n"
         << "获得金币:" << gold << "\n"
         << "装备: 宝剑+" << sword << " 护甲+" << armor << "\n"
         << "==================";
    return 0;
    

    }

  • 通过的题目

  • 最近活动

  • 最近编写的题解

    This person is lazy and didn't write any solutions.

题目标签

模拟
10
计算几何
5
数学
4
顺序结构
3
语言入门
2
字符串
2
素数判断
1
质数
1
筛法
1
图论
1
1
深度优先搜索 DFS
1
组合数学
1
高精度
1