问题标题: 新的钓鱼游戏

0
0
闵子航
闵子航
修练者
修练者

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

// 鱼的结构
struct Fish {
    string name;
    int baseGold;
    int exp;
    double minWeight;
    double maxWeight;
    int rarity;
    // 构造函数,兼容 C++98
    Fish(string n, int g, int e, double minW, double maxW, int r)
        : name(n), baseGold(g), exp(e), minWeight(minW), maxWeight(maxW), rarity(r) {}
};

vector<Fish> fishList;

struct Player {
    int gold;
    int level;
    int exp;
    int rodLevel;
    int normalBait;
    int superBait;
    string biggestFish;
    double biggestWeight;
} player;

void initFish() {
    fishList.push_back(Fish("鲫鱼", 5, 10, 0.1, 0.5, 1));
    fishList.push_back(Fish("鲤鱼", 8, 15, 0.2, 0.8, 1));
    fishList.push_back(Fish("草鱼", 10, 20, 0.3, 1.0, 1));
    fishList.push_back(Fish("鲢鱼", 6, 12, 0.2, 0.7, 1));
    fishList.push_back(Fish("黑鱼", 30, 50, 0.5, 1.5, 2));
    fishList.push_back(Fish("鲈鱼", 40, 60, 0.6, 1.8, 2));
    fishList.push_back(Fish("鳜鱼", 50, 70, 0.7, 2.0, 2));
    fishList.push_back(Fish("鳊鱼", 35, 55, 0.5, 1.6, 2));
    fishList.push_back(Fish("金龙鱼", 200, 200, 1.0, 3.0, 3));
    fishList.push_back(Fish("巨骨舌鱼", 300, 300, 2.0, 5.0, 3));
    fishList.push_back(Fish("美人鱼", 500, 500, 3.0, 6.0, 3));
}

int expToNextLevel() {
    return player.level * 100;
}

void checkLevelUp() {
    int need = expToNextLevel();
    while (player.exp >= need) {
        player.exp -= need;
        player.level++;
        need = expToNextLevel();
        cout << "恭喜!你升级了!当前等级: " << player.level << endl;
    }
}

void clearInputBuffer() {
    while (kbhit()) getch();
}

double randomWeight(double minW, double maxW) {
    double r = (double)rand() / RAND_MAX;
    return minW + r * (maxW - minW);
}

Fish getRandomFish(int rodLevel, int baitType) {
    double commonProb = 70.0;
    double rareProb = 25.0;
    double legendProb = 5.0;

    commonProb -= rodLevel * 3;
    rareProb += rodLevel * 2;
    legendProb += rodLevel * 1;

    if (baitType == 1) {
        legendProb *= 1.5;
        double diff = legendProb - 5.0;
        commonProb -= diff * 0.7;
        rareProb -= diff * 0.3;
    } else if (baitType == 2) {
        legendProb *= 2.5;
        double diff = legendProb - 5.0;
        commonProb -= diff * 0.6;
        rareProb -= diff * 0.4;
    }

    if (commonProb < 0) commonProb = 0;
    if (rareProb < 0) rareProb = 0;
    if (legendProb < 0) legendProb = 0;
    double total = commonProb + rareProb + legendProb;
    double r = (double)rand() / RAND_MAX * total;

    int targetRarity;
    if (r < commonProb) targetRarity = 1;
    else if (r < commonProb + rareProb) targetRarity = 2;
    else targetRarity = 3;

    vector<Fish> candidates;
    for (size_t i = 0; i < fishList.size(); i++) {
        if (fishList[i].rarity == targetRarity) {
            candidates.push_back(fishList[i]);
        }
    }
    if (candidates.empty()) {
        return fishList[0];
    }
    int idx = rand() % candidates.size();
    return candidates[idx];
}

void goFishing() {
    system("cls");
    cout << "===== 开始钓鱼 =====" << endl;

    int baitType = 0;
    if (player.normalBait > 0 || player.superBait > 0) {
        cout << "你拥有:" << endl;
        cout << "1. 普通鱼饵: " << player.normalBait << " 个 (增加稀有鱼概率)" << endl;
        cout << "2. 高级鱼饵: " << player.superBait << " 个 (大幅增加稀有鱼概率)" << endl;
        cout << "3. 不使用鱼饵" << endl;
        cout << "请选择使用的鱼饵 (1/2/3): ";
        char c = getch();
        cout << c << endl;
        if (c == '1' && player.normalBait > 0) {
            baitType = 1;
            player.normalBait--;
            cout << "使用了普通鱼饵!" << endl;
        } else if (c == '2' && player.superBait > 0) {
            baitType = 2;
            player.superBait--;
            cout << "使用了高级鱼饵!" << endl;
        } else {
            cout << "不使用鱼饵。" << endl;
        }
    } else {
        cout << "你没有鱼饵,可在商城购买。" << endl;
    }

    cout << "正在钓鱼,请稍候..." << endl;
    int waitTime = 2000 + rand() % 3000;
    int elapsed = 0;
    while (elapsed < waitTime) {
        Sleep(500);
        cout << "." << flush;
        elapsed += 500;
    }
    cout << endl;

    clearInputBuffer();
    system("cls");
    cout << "!!! 有鱼上钩了 !!!" << endl;
    cout << "快按 【空格键】 拉竿!(你只有1.5秒)" << endl;

    clock_t start = clock();
    bool success = false;
    while (true) {
        if (kbhit()) {
            char key = getch();
            if (key == 32) {
                success = true;
                break;
            }
        }
        if ( (double)(clock() - start) / CLOCKS_PER_SEC > 1.5 ) {
            break;
        }
        Sleep(10);
    }

    if (!success) {
        cout << "太慢了!鱼跑掉了..." << endl;
        cout << "按任意键返回菜单..." << endl;
        getch();
        return;
    }

    int baseSuccess = 60;
    int rodBonus = player.rodLevel * 10;
    int levelBonus = (player.level - 1) * 2;
    int baitBonus = 0;
    if (baitType == 1) baitBonus = 10;
    else if (baitType == 2) baitBonus = 20;

    int successRate = baseSuccess + rodBonus + levelBonus + baitBonus;
    if (successRate > 95) successRate = 95;

    int roll = rand() % 100;
    if (roll >= successRate) {
        cout << "可惜,鱼脱钩了!成功率" << successRate << "%,你roll了" << roll << endl;
        cout << "按任意键返回菜单..." << endl;
        getch();
        return;
    }

    Fish caught = getRandomFish(player.rodLevel, baitType);
    double weight = randomWeight(caught.minWeight, caught.maxWeight);
    weight = ((int)(weight * 10 + 0.5)) / 10.0;

    int goldEarned = caught.baseGold + (int)(weight * 2);
    int expEarned = caught.exp + (int)(weight * 5);

    cout << "钓到了! " << caught.name << " 重量: " << weight << " kg" << endl;
    cout << "获得金币: " << goldEarned << "  经验: " << expEarned << endl;

    player.gold += goldEarned;
    player.exp += expEarned;
    checkLevelUp();

    if (caught.rarity >= 2 && weight > player.biggestWeight) {
        player.biggestWeight = weight;
        // 使用 stringstream 代替 to_string,兼容 C++98
        stringstream ss;
        ss << caught.name << " " << weight << "kg";
        player.biggestFish = ss.str();
        cout << "新纪录!最大鱼更新!" << endl;
    }

    cout << "按任意键返回菜单..." << endl;
    getch();
}

void shop() {
    while (true) {
        system("cls");
        cout << "========== 渔具商城 ==========" << endl;
        cout << "当前金币: " << player.gold << endl;
        cout << "1. 升级鱼竿 (当前等级: " << player.rodLevel << "/5)" << endl;
        int rodCost = 0;
        if (player.rodLevel < 5) {
            rodCost = 100;
            for (int i = 0; i < player.rodLevel; i++) rodCost *= 2;
            cout << "   升级费用: " << rodCost << " 金币 (提升钓鱼成功率与稀有鱼概率)" << endl;
        } else {
            cout << "   鱼竿已满级!" << endl;
        }
        cout << "2. 购买普通鱼饵 (10金币/个)  当前: " << player.normalBait << " 个" << endl;
        cout << "3. 购买高级鱼饵 (50金币/个)  当前: " << player.superBait << " 个" << endl;
        cout << "4. 离开商城" << endl;
        cout << "请选择: ";
        char c = getch();
        cout << c << endl;

        if (c == '1') {
            if (player.rodLevel >= 5) {
                cout << "鱼竿已达最高等级!" << endl;
            } else if (player.gold >= rodCost) {
                player.gold -= rodCost;
                player.rodLevel++;
                cout << "鱼竿升级成功!当前等级: " << player.rodLevel << endl;
            } else {
                cout << "金币不足!" << endl;
            }
        } else if (c == '2') {
            cout << "购买数量: ";
            int num;
            cin >> num;
            if (cin.fail() || num <= 0) {
                cin.clear();
                cin.ignore(10000, '\\n');
                cout << "输入无效!" << endl;
            } else if (player.gold >= num * 10) {
                player.gold -= num * 10;
                player.normalBait += num;
                cout << "成功购买 " << num << " 个普通鱼饵!" << endl;
            } else {
                cout << "金币不足!" << endl;
            }
            clearInputBuffer();
        } else if (c == '3') {
            cout << "购买数量: ";
            int num;
            cin >> num;
            if (cin.fail() || num <= 0) {
                cin.clear();
                cin.ignore(10000, '\\n');
                cout << "输入无效!" << endl;
            } else if (player.gold >= num * 50) {
                player.gold -= num * 50;
                player.superBait += num;
                cout << "成功购买 " << num << " 个高级鱼饵!" << endl;
            } else {
                cout << "金币不足!" << endl;
            }
            clearInputBuffer();
        } else if (c == '4') {
            break;
        } else {
            cout << "无效选项!" << endl;
        }
        cout << "按任意键继续..." << endl;
        getch();
    }
}

void showStatus() {
    system("cls");
    cout << "========== 玩家状态 ==========" << endl;
    cout << "金币: " << player.gold << endl;
    cout << "等级: " << player.level << endl;
    cout << "经验: " << player.exp << " / " << expToNextLevel() << endl;
    cout << "鱼竿等级: " << player.rodLevel << "/5" << endl;
    cout << "普通鱼饵: " << player.normalBait << " 个" << endl;
    cout << "高级鱼饵: " << player.superBait << " 个" << endl;
    cout << "最大鱼记录: ";
    if (player.biggestFish == "无") cout << "暂无" << endl;
    else cout << player.biggestFish << endl;
    cout << "按任意键返回菜单..." << endl;
    getch();
}

void showHelp() {
    system("cls");
    cout << "========== 游戏说明 ==========" << endl;
    cout << "1. 钓鱼:等待鱼上钩后按空格键拉竿,反应越快越好。" << endl;
    cout << "   成功率受鱼竿等级、人物等级和鱼饵影响。" << endl;
    cout << "   鱼饵可提高稀有鱼出现概率,钓鱼前可选择使用。" << endl;
    cout << "2. 商城:使用金币升级鱼竿或购买鱼饵。" << endl;
    cout << "3. 升级需要经验值,钓鱼可获得经验,提升等级增加成功率。" << endl;
    cout << "4. 钓到稀有或传说大鱼可刷新最大鱼记录。" << endl;
    cout << "5. 祝你好运,钓上美人鱼!" << endl;
    cout << "按任意键返回..." << endl;
    getch();
}

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

    player.gold = 50;
    player.level = 1;
    player.exp = 0;
    player.rodLevel = 0;
    player.normalBait = 0;
    player.superBait = 0;
    player.biggestFish = "无";
    player.biggestWeight = 0.0;

    while (true) {
        system("cls");
        cout << "========== 文字钓鱼** ==========" << endl;
        cout << "金币: " << player.gold << "  等级: " << player.level << "  鱼竿: Lv" << player.rodLevel << endl;
        cout << "1. 去钓鱼" << endl;
        cout << "2. 逛商城" << endl;
        cout << "3. 查看状态" << endl;
        cout << "4. 游戏说明" << endl;
        cout << "5. 退出游戏" << endl;
        cout << "请选择: ";
        char cmd = getch();
        cout << cmd << endl;

        switch (cmd) {
            case '1': goFishing(); break;
            case '2': shop(); break;
            case '3': showStatus(); break;
            case '4': showHelp(); break;
            case '5':
                cout << "再见,钓鱼佬!" << endl;
                return 0;
            default:
                cout << "无效选项,请重新选择!" << endl;
                Sleep(500);
        }
    }
    return 0;
}

闵子航在2026-06-12 19:59:51追加了内容

ai  

aiaa

 

ai

deepseek


0
马弘毅
马弘毅
新手守护
新手守护

6666666666666666666666666666666666666666666

0
0
0
0
0
0
我要回答