C++#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <windows.h>
#include <algorithm>
using namespace std;
// 矿物结构体
struct Mineral {
string name;
string description;
int rarity; // 1-5,1最常见,5最稀有
int baseValue;
bool discovered;
string colorCode; // 控制台颜色代码
string type; // 矿石类型:普通、稀有、传说
int minPickaxeLevel; // 需要的最低镐子等级
string source; // 来源:mine-挖掘, chop-砍伐, both-两者均可
bool operator==(const Mineral& other) const {
return name == other.name;
}
};
// 镐子结构体
struct Pickaxe {
string name;
int level;
int durability;
int maxDurability;
int efficiency; // 采矿效率加成
int luck; // 幸运值,影响稀有矿物出现率
int speed; // 挖掘速度(毫秒)
int multiMine; // 一次挖掘获得矿物数量
vector<pair<string, int>> requiredMinerals; // 制作所需材料
// 构造函数,确保正确初始化
Pickaxe(string n = "", int lvl = 1, int dur = 30, int maxDur = 30,
int eff = 0, int lck = 0, int spd = 300, int multi = 1,
vector<pair<string, int>> req = {})
: name(n), level(lvl), durability(dur), maxDurability(maxDur),
efficiency(eff), luck(lck), speed(spd), multiMine(multi), requiredMinerals(req) {}
bool operator==(const Pickaxe& other) const {
return name == other.name;
}
};
// 玩家数据
struct PlayerData {
int money;
int level;
int experience;
int maxEnergy;
int currentEnergy;
Pickaxe currentPickaxe;
vector<Mineral> inventory;
vector<string> unlockedPickaxeNames;
int debt; // 负债
int totalDays; // 总天数
};
class MiningGame {
private:
vector<Mineral> minerals;
vector<Pickaxe> allPickaxes;
PlayerData player;
int dayCount;
// 颜色代码
const string RED = "\033[31m";
const string GREEN = "\033[32m";
const string YELLOW = "\033[33m";
const string BLUE = "\033[34m";
const string MAGENTA = "\033[35m";
const string CYAN = "\033[36m";
const string WHITE = "\033[37m";
const string GRAY = "\033[90m";
const string ORANGE = "\033[38;5;214m";
const string BROWN = "\033[38;5;130m";
const string RESET = "\033[0m";
public:
MiningGame() {
InitializeMinerals();
InitializePickaxes();
dayCount = 1;
LoadGame();
}
void InitializeMinerals() {
minerals = {
// 基础材料 - 可通过砍伐获得
{"木材", "基本的建筑材料", 1, 5, false, BROWN, "材料", 1, "chop"},
{"石头", "常见的建筑材料", 1, 3, false, GRAY, "材料", 1, "both"},
// 普通矿物 - 木镐(1级)可挖
{"煤炭", "常见的燃料矿物", 1, 10, false, WHITE, "普通", 1, "mine"},
{"铁矿石", "基础的金属矿石", 1, 20, false, WHITE, "普通", 1, "mine"},
{"铜矿石", "导电性良好的金属", 2, 30, false, YELLOW, "普通", 1, "mine"},
{"锡矿石", "柔软的白色金属", 1, 25, false, WHITE, "普通", 1, "mine"},
{"石灰石", "常见的沉积岩", 1, 15, false, WHITE, "普通", 1, "mine"},
{"花岗岩", "火成岩的一种", 1, 18, false, GRAY, "普通", 1, "mine"},
// 稀有矿物 - 石镐(2级)可挖
{"银矿石", "珍贵的白色金属", 3, 50, false, WHITE, "稀有", 2, "mine"},
{"铝矿石", "轻质金属", 2, 35, false, WHITE, "稀有", 2, "mine"},
{"铅矿石", "重金属矿石", 2, 40, false, GRAY, "稀有", 2, "mine"},
{"锌矿石", "蓝白色金属", 2, 38, false, BLUE, "稀有", 2, "mine"},
{"镍矿石", "银白色金属", 2, 45, false, WHITE, "稀有", 2, "mine"},
{"石英", "常见的硅酸盐矿物", 2, 25, false, CYAN, "稀有", 2, "mine"},
// 进阶矿物 - 铁镐(3级)可挖
{"金矿石", "贵重的黄色金属", 3, 80, false, YELLOW, "稀有", 3, "mine"},
{"水晶", "透明的石英晶体", 2, 40, false, CYAN, "稀有", 3, "mine"},
{"玛瑙", "条纹状的石英变种", 3, 60, false, RED, "稀有", 3, "mine"},
{"孔雀石", "绿色的铜矿石", 2, 45, false, GREEN, "稀有", 3, "mine"},
{"橄榄石", "黄绿色宝石", 3, 70, false, GREEN, "稀有", 3, "mine"},
{"石榴石", "深红色宝石", 3, 75, false, RED, "稀有", 3, "mine"},
// 高级矿物 - 银镐(4级)可挖
{"铂矿石", "贵重的银白色金属", 4, 120, false, WHITE, "传说", 4, "mine"},
{"钛矿石", "高强度轻质金属", 4, 110, false, GRAY, "传说", 4, "mine"},
{"锆石", "常见的宝石矿物", 3, 85, false, CYAN, "传说", 4, "mine"},
{"月光石", "具有月光效应的宝石", 4, 130, false, CYAN, "传说", 4, "mine"},
{"日光石", "含有赤铁矿包裹体", 4, 125, false, ORANGE, "传说", 4, "mine"},
{"青金石", "蓝色变质岩", 4, 140, false, BLUE, "传说", 4, "mine"},
// 珍贵矿物 - 金镐(5级)可挖
{"钻石", "最坚硬的天然矿物", 5, 200, false, CYAN, "传说", 5, "mine"},
{"红宝石", "红色的珍贵宝石", 4, 150, false, RED, "传说", 5, "mine"},
{"蓝宝石", "蓝色的珍贵宝石", 4, 150, false, BLUE, "传说", 5, "mine"},
{"翡翠", "绿色的珍贵玉石", 4, 120, false, GREEN, "传说", 5, "mine"},
{"紫水晶", "紫色的石英晶体", 3, 90, false, MAGENTA, "传说", 5, "mine"},
{"黄玉", "黄色的宝石", 3, 100, false, YELLOW, "传说", 5, "mine"},
// 稀有宝石 - 水晶镐(6级)可挖
{"祖母绿", "绿色的铍铝硅酸盐", 5, 250, false, GREEN, "传说", 6, "mine"},
{"海蓝宝石", "蓝色的绿柱石", 4, 180, false, CYAN, "传说", 6, "mine"},
{"碧玺", "多彩的硼硅酸盐", 4, 160, false, MAGENTA, "传说", 6, "mine"},
{"欧泊", "具有变彩效应的宝石", 5, 220, false, MAGENTA, "传说", 6, "mine"},
{"尖晶石", "镁铝氧化物矿物", 4, 170, false, RED, "传说", 6, "mine"},
// 顶级矿物 - 钻石镐(7级)可挖
{"亚历山大石", "变色的金绿宝石", 5, 300, false, GREEN, "传说", 7, "mine"},
{"坦桑石", "蓝色的黝帘石", 5, 280, false, BLUE, "传说", 7, "mine"},
{"红柱石", "铝硅酸盐矿物", 4, 190, false, RED, "传说", 7, "mine"},
{"堇青石", "具有多色性的宝石", 4, 200, false, BLUE, "传说", 7, "mine"},
// 传说矿物 - 铂金镐(8级)可挖
{"铀矿石", "放射性矿物,价值很高", 5, 350, false, GREEN, "传说", 8, "mine"},
{"钚矿石", "人造元素矿石", 5, 400, false, GREEN, "传说", 8, "mine"},
{"铱矿石", "极稀有的过渡金属", 5, 380, false, WHITE, "传说", 8, "mine"},
// 终极矿物 - 铱金镐(9级)可挖
{"铑矿石", "铂族金属中最贵的", 5, 450, false, WHITE, "传说", 9, "mine"},
{"钯矿石", "铂族金属之一", 5, 420, false, WHITE, "传说", 9, "mine"},
// 神话矿物 - 陨石镐(10级)可挖
{"陨石", "来自外太空的岩石", 5, 500, false, GRAY, "传说", 10, "mine"},
{"化石", "古代生物的遗迹", 4, 300, false, MAGENTA, "传说", 10, "mine"},
// 神级矿物 - 神之镐(11级)可挖
{"蓝钻", "稀有的蓝色钻石", 5, 600, false, BLUE, "传说", 11, "mine"},
{"红钻", "极其稀有的红色钻石", 5, 800, false, RED, "传说", 11, "mine"}
};
}
void InitializePickaxes() {
allPickaxes.clear();
allPickaxes = {
// 名称, 等级, 当前耐久, 最大耐久, 效率, 幸运, 速度, 多重挖掘, 所需材料
Pickaxe("木镐", 1, 30, 30, 0, 0, 300, 1, {{"木材", 10}}),
Pickaxe("石镐", 2, 50, 50, 10, 5, 250, 1, {{"石头", 15}, {"木材", 5}}),
Pickaxe("铁镐", 3, 80, 80, 20, 10, 200, 1, {{"铁矿石", 15}, {"石头", 10}}),
Pickaxe("银镐", 4, 100, 100, 30, 15, 180, 1, {{"银矿石", 20}, {"铁矿石", 10}}),
Pickaxe("金镐", 5, 120, 120, 40, 20, 150, 2, {{"金矿石", 25}, {"银矿石", 15}}),
Pickaxe("水晶镐", 6, 150, 150, 50, 25, 120, 2, {{"水晶", 30}, {"金矿石", 20}}),
Pickaxe("钻石镐", 7, 200, 200, 60, 30, 100, 2, {{"钻石", 10}, {"水晶", 20}}),
Pickaxe("铂金镐", 8, 250, 250, 70, 40, 80, 3, {{"铂矿石", 15}, {"钻石", 5}}),
Pickaxe("铱金镐", 9, 300, 300, 80, 50, 60, 3, {{"铱矿石", 8}, {"铂矿石", 10}}),
Pickaxe("陨石镐", 10, 400, 400, 100, 60, 40, 4, {{"陨石", 5}, {"铱矿石", 5}, {"钻石", 5}}),
Pickaxe("神之镐", 11, 500, 500, 120, 80, 20, 5, {{"蓝钻", 3}, {"红钻", 3}, {"陨石", 3}})
};
}
void ShowMainMenu() {
while(true) {
system("cls");
cout << CYAN << "===== 真实矿物挖掘 =====" << RESET << endl;
cout << "第 " << dayCount << " 天 (总计: " << player.totalDays << " 天)" << endl;
cout << "金钱: " << GREEN << player.money << " G" << RESET;
if(player.debt > 0) {
cout << " | 负债: " << RED << player.debt << " G" << RESET;
}
cout << endl;
cout << "等级: " << player.level << " | 经验: " << player.experience << "/" << (player.level * 100) << endl;
cout << "能量: " << player.currentEnergy << "/" << player.maxEnergy << endl;
cout << "当前镐: " << YELLOW << player.currentPickaxe.name << RESET;
cout << " | 耐久: " << player.currentPickaxe.durability << "/" << player.currentPickaxe.maxDurability;
if(player.currentPickaxe.multiMine > 1) {
cout << " | 多重挖掘: x" << player.currentPickaxe.multiMine;
}
cout << endl;
cout << "======================" << endl;
cout << "1. 去采矿" << endl;
cout << "2. 去砍树" << endl;
cout << "3. 查看矿物图鉴" << endl;
cout << "4. 查看背包" << endl;
cout << "5. 制作/修复镐子" << endl;
cout << "6. 休息一天" << endl;
cout << "7. 偿还债务" << endl;
cout << "8. 保存游戏" << endl;
cout << "9. 退出游戏" << endl;
cout << "选择: ";
int choice;
cin >> choice;
switch(choice) {
case 1: MineMinerals(); break;
case 2: ChopWood(); break;
case 3: ShowMineralCatalog(); break;
case 4: ShowInventory(); break;
case 5: CraftPickaxeMenu(); break;
case 6: Rest(); break;
case 7: PayDebt(); break;
case 8: SaveGame(); break;
case 9:
SaveGame();
cout << "游戏已保存,再见!" << endl;
return;
default:
cout << RED << "无效选择!" << RESET << endl;
Sleep(1000);
}
}
}
void MineMinerals() {
if(player.currentEnergy <= 0) {
cout << RED << "能量不足,需要休息!" << RESET << endl;
Sleep(1500);
return;
}
if(player.currentPickaxe.durability <= 0) {
cout << RED << "镐子已损坏,需要修复或制作新镐子!" << RESET << endl;
Sleep(1500);
return;
}
system("cls");
cout << YELLOW << "===== 采矿中... =====" << RESET << endl;
// 采矿动画 - 速度受镐子影响
int animationSpeed = player.currentPickaxe.speed;
for(int i = 0; i < 3; i++) {
cout << "挖掘";
for(int j = 0; j < 3; j++) {
cout << ".";
Sleep(animationSpeed);
}
cout << endl;
}
// 消耗能量和耐久
player.currentEnergy -= 10;
player.currentPickaxe.durability -= 1;
// 根据镐子等级决定找到矿物的概率
int successChance = 30 + player.currentPickaxe.efficiency;
if(rand() % 100 > successChance) {
cout << RED << "这次挖掘没有找到任何矿物..." << RESET << endl;
Sleep(1500);
return;
}
// 多重挖掘 - 一次可能挖到多个矿物
int mineralsFound = 0;
int totalValue = 0;
int totalExp = 0;
for(int i = 0; i < player.currentPickaxe.multiMine; i++) {
// 决定找到的矿物(受镐子幸运值影响,且只能挖到当前镐子等级允许的矿物)
int mineralIndex = SelectMineral("mine");
if(mineralIndex == -1) {
cout << YELLOW << "这个深度的矿物需要更高级的镐子才能挖掘!" << RESET << endl;
continue;
}
Mineral foundMineral = minerals[mineralIndex];
// 标记为已发现
minerals[mineralIndex].discovered = true;
// 添加到背包
player.inventory.push_back(foundMineral);
// 计算价值(受镐子等级影响)
int value = foundMineral.baseValue * (1 + player.currentPickaxe.level * 0.15);
totalValue += value;
// 获得经验
int expGained = foundMineral.rarity * 10;
totalExp += expGained;
mineralsFound++;
cout << foundMineral.colorCode << "你找到了: " << foundMineral.name << "!" << RESET << endl;
}
if(mineralsFound == 0) {
cout << YELLOW << "没有找到当前镐子可以挖掘的矿物..." << RESET << endl;
} else {
player.money += totalValue;
player.experience += totalExp;
cout << "总计价值: " << GREEN << totalValue << " G" << RESET << endl;
cout << "获得经验: " << totalExp << endl;
}
cout << "镐子耐久: " << player.currentPickaxe.durability << "/" << player.currentPickaxe.maxDurability << endl;
if(player.currentPickaxe.multiMine > 1 && mineralsFound > 0) {
cout << "多重挖掘: " << mineralsFound << " 个矿物" << endl;
}
// 检查升级
CheckLevelUp();
cout << "\n按任意键继续..." << endl;
cin.ignore();
cin.get();
}
void ChopWood() {
if(player.currentEnergy <= 0) {
cout << RED << "能量不足,需要休息!" << RESET << endl;
Sleep(1500);
return;
}
system("cls");
cout << GREEN << "===== 砍树中... =====" << RESET << endl;
// 砍树动画
for(int i = 0; i < 3; i++) {
cout << "砍树";
for(int j = 0; j < 3; j++) {
cout << ".";
Sleep(300);
}
cout << endl;
}
// 消耗能量
player.currentEnergy -= 5;
// 砍树成功率较高
if(rand() % 100 > 20) {
cout << RED << "这次砍树没有获得任何材料..." << RESET << endl;
Sleep(1500);
return;
}
// 获得木材和可能的石头
int materialsFound = 0;
// 总是获得木材
for(int i = 0; i < minerals.size(); i++) {
if(minerals[i].name == "木材") {
player.inventory.push_back(minerals[i]);
cout << BROWN << "你获得了: 木材!" << RESET << endl;
materialsFound++;
break;
}
}
// 有几率获得石头
if(rand() % 100 < 30) {
for(int i = 0; i < minerals.size(); i++) {
if(minerals[i].name == "石头") {
player.inventory.push_back(minerals[i]);
cout << GRAY << "你获得了: 石头!" << RESET << endl;
materialsFound++;
break;
}
}
}
// 获得少量经验和金钱
int expGained = 5;
int moneyGained = 3;
player.experience += expGained;
player.money += moneyGained;
cout << "获得经验: " << expGained << endl;
cout << "获得金钱: " << GREEN << moneyGained << " G" << RESET << endl;
// 检查升级
CheckLevelUp();
cout << "\n按任意键继续..." << endl;
cin.ignore();
cin.get();
}
int SelectMineral(string source) {
vector<int> availableMinerals;
vector<int> weights;
// 只选择当前镐子等级可以挖掘的矿物,并且符合来源要求
for(int i = 0; i < minerals.size(); i++) {
if(minerals[i].minPickaxeLevel <= player.currentPickaxe.level &&
(minerals[i].source == source || minerals[i].source == "both")) {
availableMinerals.push_back(i);
int weight = 100 / minerals[i].rarity;
// 镐子幸运值提高找到稀有矿物的概率
weight = max(1, weight - player.currentPickaxe.luck);
weights.push_back(weight);
}
}
if(availableMinerals.empty()) {
return -1; // 没有可获得的矿物/材料
}
int totalWeight = 0;
for(int weight : weights) {
totalWeight += weight;
}
int randomValue = rand() % totalWeight;
int currentWeight = 0;
for(int i = 0; i < weights.size(); i++) {
currentWeight += weights[i];
if(randomValue < currentWeight) {
return availableMinerals[i];
}
}
return availableMinerals[0];
}
void ShowMineralCatalog() {
system("cls");
cout << CYAN << "===== 矿物图鉴 =====" << RESET << endl;
cout << "当前镐子等级: " << player.currentPickaxe.level << endl;
cout << "可挖掘矿物: 等级 1-" << player.currentPickaxe.level << " 的矿物" << endl;
// 按类型分组显示
vector<string> types = {"材料", "普通", "稀有", "传说"};
for(const auto& type : types) {
cout << "\n" << MAGENTA << "=== " << type << (type == "材料" ? " ===" : "矿物 ===") << RESET << endl;
for(const auto& mineral : minerals) {
if(mineral.type == type) {
if(mineral.discovered) {
cout << mineral.colorCode << mineral.name << RESET;
cout << " - 稀有度: " << GetRarityStars(mineral.rarity);
cout << " - 价值: " << mineral.baseValue << " G";
if(mineral.type != "材料") {
cout << " - 需要镐子: " << mineral.minPickaxeLevel << "级";
}
cout << endl;
cout << " " << mineral.description;
if(mineral.source == "mine") {
cout << " [通过挖掘获得]";
} else if(mineral.source == "chop") {
cout << " [通过砍树获得]";
} else if(mineral.source == "both") {
cout << " [挖掘和砍树均可获得]";
}
cout << endl;
// 显示是否可以获取
if(mineral.type == "材料" || mineral.minPickaxeLevel <= player.currentPickaxe.level) {
cout << GREEN << " [可获取]" << RESET << endl;
} else {
cout << RED << " [需要 " << mineral.minPickaxeLevel << " 级镐子]" << RESET << endl;
}
} else {
cout << GRAY << "????" << RESET;
cout << " - 稀有度: " << GetRarityStars(mineral.rarity);
if(mineral.type != "材料") {
cout << " - 需要镐子: " << mineral.minPickaxeLevel << "级";
}
cout << endl;
cout << " 尚未发现的" << (mineral.type == "材料" ? "材料" : "矿物") << endl;
}
cout << endl;
}
}
}
int discoveredCount = 0;
for(const auto& mineral : minerals) {
if(mineral.discovered) discoveredCount++;
}
cout << "发现进度: " << discoveredCount << "/" << minerals.size() << endl;
cout << "\n按任意键继续..." << endl;
cin.ignore();
cin.get();
}
string GetRarityStars(int rarity) {
string stars;
for(int i = 0; i < rarity; i++) {
stars += "★";
}
for(int i = rarity; i < 5; i++) {
stars += "☆";
}
return stars;
}
void ShowInventory() {
system("cls");
cout << YELLOW << "===== 背包 =====" << RESET << endl;
if(player.inventory.empty()) {
cout << "背包是空的" << endl;
} else {
map<string, int> mineralCount;
int totalValue = 0;
for(const auto& mineral : player.inventory) {
mineralCount[mineral.name]++;
totalValue += mineral.baseValue;
}
for(const auto& pair : mineralCount) {
string color = WHITE;
for(const auto& mineral : minerals) {
if(mineral.name == pair.first) {
color = mineral.colorCode;
break;
}
}
cout << color << pair.first << RESET << " x " << pair.second << endl;
}
cout << "\n总计: " << player.inventory.size() << " 个物品" << endl;
cout << "总价值: " << GREEN << totalValue << " G" << RESET << endl;
}
cout << "\n1. 出售所有矿物" << endl;
cout << "2. 返回" << endl;
cout << "选择: ";
int choice;
cin >> choice;
if(choice == 1) {
SellAllMinerals();
}
}
void SellAllMinerals() {
if(player.inventory.empty()) {
cout << RED << "没有可出售的物品!" << RESET << endl;
Sleep(1000);
return;
}
int totalValue = 0;
for(const auto& mineral : player.inventory) {
totalValue += mineral.baseValue;
}
// 镐子等级提供出售加成
totalValue *= (1 + player.currentPickaxe.level * 0.1);
player.money += totalValue;
player.inventory.clear();
cout << GREEN << "出售所有物品获得: " << totalValue << " G!" << RESET << endl;
Sleep(1500);
}
void CraftPickaxeMenu() {
while(true) {
system("cls");
cout << CYAN << "===== 镐子工坊 =====" << RESET << endl;
cout << "当前镐: " << YELLOW << player.currentPickaxe.name << RESET;
cout << " | 耐久: " << player.currentPickaxe.durability << "/" << player.currentPickaxe.maxDurability << endl;
cout << "=====================" << endl;
// 显示可制作的镐子
for(int i = 0; i < allPickaxes.size(); i++) {
const Pickaxe& pickaxe = allPickaxes[i];
bool canCraft = CanCraftPickaxe(pickaxe);
bool isUnlocked = IsPickaxeUnlocked(pickaxe);
if(isUnlocked) {
cout << i + 1 << ". " << YELLOW << pickaxe.name << RESET;
cout << " (等级 " << pickaxe.level << ")" << endl;
cout << " 耐久: " << pickaxe.maxDurability << " | 效率: +" << pickaxe.efficiency << "%";
cout << " | 幸运: +" << pickaxe.luck << endl;
cout << " 速度: " << pickaxe.speed << "ms";
cout << " | 多重挖掘: x" << pickaxe.multiMine << endl;
cout << " 可挖掘: 等级 1-" << pickaxe.level << " 的矿物" << endl;
cout << " 需要: ";
for(const auto& req : pickaxe.requiredMinerals) {
cout << req.first << " x " << req.second << " ";
}
cout << endl;
if(canCraft) {
cout << GREEN << " [可制作]" << RESET << endl;
} else {
cout << RED << " [材料不足]" << RESET << endl;
}
} else {
cout << i + 1 << ". " << GRAY << "????" << RESET << " (需要等级 " << (i + 1) * 5 << ")" << endl;
}
cout << endl;
}
cout << allPickaxes.size() + 1 << ". 修复当前镐子 (消耗 50G)" << endl;
cout << "0. 返回" << endl;
cout << "选择: ";
int choice;
cin >> choice;
if(choice == 0) break;
if(choice == allPickaxes.size() + 1) {
RepairPickaxe();
continue;
}
int pickaxeIndex = choice - 1;
if(pickaxeIndex >= 0 && pickaxeIndex < allPickaxes.size()) {
CraftPickaxe(pickaxeIndex);
} else {
cout << RED << "无效选择!" << RESET << endl;
Sleep(1000);
}
}
}
bool CanCraftPickaxe(const Pickaxe& pickaxe) {
for(const auto& req : pickaxe.requiredMinerals) {
int count = 0;
for(const auto& mineral : player.inventory) {
if(mineral.name == req.first) {
count++;
}
}
if(count < req.second) {
return false;
}
}
return true;
}
bool IsPickaxeUnlocked(const Pickaxe& pickaxe) {
// 检查是否在已解锁列表中
for(const auto& name : player.unlockedPickaxeNames) {
if(name == pickaxe.name) {
return true;
}
}
// 根据玩家等级解锁镐子
return player.level >= pickaxe.level * 5;
}
void CraftPickaxe(int pickaxeIndex) {
Pickaxe pickaxe = allPickaxes[pickaxeIndex];
if(!IsPickaxeUnlocked(pickaxe)) {
cout << RED << "等级不足,无法制作此镐子!" << RESET << endl;
Sleep(1500);
return;
}
if(!CanCraftPickaxe(pickaxe)) {
cout << RED << "材料不足!" << RESET << endl;
Sleep(1500);
return;
}
// 消耗材料
for(const auto& req : pickaxe.requiredMinerals) {
int toRemove = req.second;
for(auto it = player.inventory.begin(); it != player.inventory.end() && toRemove > 0; ) {
if(it->name == req.first) {
it = player.inventory.erase(it);
toRemove--;
} else {
++it;
}
}
}
// 制作新镐子(使用完整的构造函数确保正确初始化)
player.currentPickaxe = Pickaxe(pickaxe.name, pickaxe.level, pickaxe.maxDurability, pickaxe.maxDurability,
pickaxe.efficiency, pickaxe.luck, pickaxe.speed, pickaxe.multiMine,
pickaxe.requiredMinerals);
// 添加到已解锁列表(如果还没有)
bool alreadyUnlocked = false;
for(const auto& name : player.unlockedPickaxeNames) {
if(name == pickaxe.name) {
alreadyUnlocked = true;
break;
}
}
if(!alreadyUnlocked) {
player.unlockedPickaxeNames.push_back(pickaxe.name);
}
cout << GREEN << "成功制作 " << pickaxe.name << "!" << RESET << endl;
cout << "新特性: ";
if(pickaxe.speed < 300) cout << "挖掘速度更快 ";
if(pickaxe.multiMine > 1) cout << "多重挖掘 x" << pickaxe.multiMine;
cout << " | 可挖掘等级 1-" << pickaxe.level << " 的矿物" << endl;
Sleep(1500);
}
void RepairPickaxe() {
if(player.money < 50) {
cout << RED << "金钱不足!修复需要 50G" << RESET << endl;
Sleep(1500);
return;
}
player.money -= 50;
player.currentPickaxe.durability = player.currentPickaxe.maxDurability;
cout << GREEN << "镐子已修复!消耗 50G" << RESET << endl;
Sleep(1500);
}
void Rest() {
// 每天增加基本生活费用
int livingCost = 20 + (player.level * 2);
player.debt += livingCost;
player.currentEnergy = player.maxEnergy;
dayCount++;
player.totalDays++;
cout << GREEN << "休息了一天,能量恢复了!" << RESET << endl;
cout << RED << "生活费用: " << livingCost << " G (已加入负债)" << RESET << endl;
cout << "总负债: " << player.debt << " G" << endl;
Sleep(1500);
}
void PayDebt() {
if(player.debt <= 0) {
cout << "你没有负债!" << endl;
Sleep(1000);
return;
}
system("cls");
cout << CYAN << "===== 偿还债务 =====" << RESET << endl;
cout << "当前负债: " << RED << player.debt << " G" << RESET << endl;
cout << "你的金钱: " << GREEN << player.money << " G" << RESET << endl;
cout << "1. 偿还全部债务" << endl;
cout << "2. 偿还部分债务" << endl;
cout << "3. 返回" << endl;
cout << "选择: ";
int choice;
cin >> choice;
if(choice == 1) {
if(player.money >= player.debt) {
player.money -= player.debt;
cout << GREEN << "成功偿还全部债务!" << RESET << endl;
player.debt = 0;
} else {
cout << RED << "金钱不足,无法偿还全部债务!" << RESET << endl;
}
} else if(choice == 2) {
cout << "输入偿还金额: ";
int amount;
cin >> amount;
if(amount > 0 && amount <= player.money && amount <= player.debt) {
player.money -= amount;
player.debt -= amount;
cout << GREEN << "成功偿还 " << amount << " G 债务!" << RESET << endl;
} else {
cout << RED << "无效金额!" << RESET << endl;
}
}
Sleep(1500);
}
void CheckLevelUp() {
int requiredExp = player.level * 100;
if(player.experience >= requiredExp) {
player.level++;
player.experience = 0;
player.maxEnergy += 20;
player.currentEnergy = player.maxEnergy;
cout << MAGENTA << "\n★ 升级了!现在等级 " << player.level << " ★" << RESET << endl;
cout << "最大能量增加至: " << player.maxEnergy << endl;
// 检查是否有新镐子解锁
for(const auto& pickaxe : allPickaxes) {
if(player.level == pickaxe.level * 5) {
cout << YELLOW << "新镐子解锁: " << pickaxe.name << RESET << endl;
// 自动添加到解锁列表
bool alreadyUnlocked = false;
for(const auto& name : player.unlockedPickaxeNames) {
if(name == pickaxe.name) {
alreadyUnlocked = true;
break;
}
}
if(!alreadyUnlocked) {
player.unlockedPickaxeNames.push_back(pickaxe.name);
}
}
}
Sleep(2000);
}
}
void SaveGame() {
ofstream saveFile("mining_game_save.txt");
if(saveFile.is_open()) {
// 保存玩家基本数据
saveFile << player.money << endl;
saveFile << player.level << endl;
saveFile << player.experience << endl;
saveFile << player.maxEnergy << endl;
saveFile << player.currentEnergy << endl;
saveFile << dayCount << endl;
saveFile << player.debt << endl;
saveFile << player.totalDays << endl;
// 保存当前镐子
saveFile << player.currentPickaxe.name << endl;
saveFile << player.currentPickaxe.durability << endl;
saveFile << player.currentPickaxe.maxDurability << endl;
// 保存发现的矿物
for(const auto& mineral : minerals) {
saveFile << mineral.discovered << " ";
}
saveFile << endl;
// 保存背包
saveFile << player.inventory.size() << endl;
for(const auto& mineral : player.inventory) {
saveFile << mineral.name << endl;
}
// 保存已解锁的镐子名字
saveFile << player.unlockedPickaxeNames.size() << endl;
for(const auto& name : player.unlockedPickaxeNames) {
saveFile << name << endl;
}
saveFile.close();
cout << GREEN << "游戏已保存!" << RESET << endl;
} else {
cout << RED << "保存失败!" << RESET << endl;
}
Sleep(1000);
}
void LoadGame() {
ifstream saveFile("mining_game_save.txt");
if(saveFile.is_open()) {
// 读取玩家基本数据
saveFile >> player.money;
saveFile >> player.level;
saveFile >> player.experience;
saveFile >> player.maxEnergy;
saveFile >> player.currentEnergy;
saveFile >> dayCount;
saveFile >> player.debt;
saveFile >> player.totalDays;
// 读取当前镐子
string pickaxeName;
int durability, maxDurability;
saveFile.ignore();
getline(saveFile, pickaxeName);
saveFile >> durability;
saveFile >> maxDurability;
// 找到对应的镐子数据并正确初始化
bool found = false;
for(const auto& pickaxe : allPickaxes) {
if(pickaxe.name == pickaxeName) {
player.currentPickaxe = Pickaxe(pickaxe.name, pickaxe.level,
durability, maxDurability,
pickaxe.efficiency, pickaxe.luck,
pickaxe.speed, pickaxe.multiMine,
pickaxe.requiredMinerals);
found = true;
break;
}
}
if(!found) {
// 如果没找到,使用默认木镐
player.currentPickaxe = allPickaxes[0];
}
// 读取发现的矿物
for(auto& mineral : minerals) {
bool discovered;
saveFile >> discovered;
mineral.discovered = discovered;
}
// 读取背包
player.inventory.clear();
int inventorySize;
saveFile >> inventorySize;
saveFile.ignore();
for(int i = 0; i < inventorySize; i++) {
string mineralName;
getline(saveFile, mineralName);
for(const auto& mineral : minerals) {
if(mineral.name == mineralName) {
player.inventory.push_back(mineral);
break;
}
}
}
// 读取已解锁的镐子名字
player.unlockedPickaxeNames.clear();
int unlockedSize;
saveFile >> unlockedSize;
saveFile.ignore();
for(int i = 0; i < unlockedSize; i++) {
string pickaxeName;
getline(saveFile, pickaxeName);
player.unlockedPickaxeNames.push_back(pickaxeName);
}
saveFile.close();
cout << GREEN << "游戏加载成功!" << RESET << endl;
} else {
// 新游戏初始化 - 使用构造函数确保正确初始化
cout << YELLOW << "开始新游戏..." << RESET << endl;
player.money = 100;
player.level = 1;
player.experience = 0;
player.maxEnergy = 100;
player.currentEnergy = 100;
player.currentPickaxe = allPickaxes[0]; // 使用构造函数初始化的木镐
player.debt = 0;
player.totalDays = 1;
dayCount = 1;
// 初始发现几种基础材料
minerals[0].discovered = true; // 木材
minerals[1].discovered = true; // 石头
minerals[2].discovered = true; // 煤炭
minerals[3].discovered = true; // 铁矿石
// 解锁初始镐子
player.unlockedPickaxeNames.push_back("木镐");
// 添加初始材料
for(int i = 0; i < 5; i++) {
player.inventory.push_back(minerals[0]); // 5个木材
}
for(int i = 0; i < 3; i++) {
player.inventory.push_back(minerals[1]); // 3个石头
}
}
Sleep(1000);
}
};
int main() {
srand(time(0)); // 初始化随机数种子
// 设置控制台支持ANSI颜色代码(Windows)
system("");
MiningGame game;
game.ShowMainMenu();
return 0;
}