冰征之旅AI版
2025-08-06 16:59:59
发布于:上海
灵感来源于@哇!我传伞太准了
原帖
工具-编译选项-编译时加入以下命令-“-stdc++11"
求赞
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <algorithm>
#include <map>
#include <iomanip>
using namespace std;
// 游戏常量
const int MAX_DAYS = 30;
const int BASE_TEMP = -25;
const int FIRE_WARMTH = 15;
// 居民类
class Settler {
public:
int id;
string name;
int health;
int hunger;
int warmth;
bool alive;
bool assigned;
Settler(int _id, string _name)
: id(_id), name(_name), health(100),
hunger(50), warmth(70), alive(true),
assigned(false) {}
void updateStatus(int temp, bool hasFire) {
if(!alive) return;
hunger += 10;
warmth -= 8 - (hasFire ? FIRE_WARMTH : 0) + (temp < -30 ? 5 : 0);
if(hunger > 100) health -= 15;
if(warmth < 40) health -= 20;
if(warmth > 90) health += 5;
health = max(0, min(100, health));
warmth = max(0, min(100, warmth));
if(health <= 0) {
alive = false;
cout << name << " 因恶劣环境去世了...\n";
Sleep(1500);
}
}
};
// 建筑类
class Building {
public:
string name;
int level;
int capacity;
int workers;
int techCost;
int resourceType; // 0:food 1:wood 2:coal
Building(string n, int cap, int cost, int type)
: name(n), level(1), capacity(cap),
workers(0), techCost(cost),
resourceType(type) {}
void upgrade() {
level++;
capacity += 2;
techCost += 5;
}
int produce() {
if(workers == 0) return 0;
return workers * level * (resourceType == 0 ? 3 : 2);
}
};
// 游戏主类
class IceSettlementGame {
private:
int day;
int temperature;
int techPoints;
bool hasFire;
vector<Settler> settlers;
vector<Building> buildings;
map<string, int> resources;
void initGame() {
day = 1;
temperature = BASE_TEMP;
techPoints = 0;
hasFire = false;
// 初始资源
resources["food"] = 50;
resources["wood"] = 30;
resources["coal"] = 20;
// 初始建筑
buildings = {
Building("冰屋", 8, 5, -1),
Building("厨房", 3, 8, 0),
Building("采冰站", 4, 10, 0),
Building("伐木场", 3, 10, 1),
Building("煤矿", 3, 12, 2)
};
// 初始居民
vector<string> names = {"张","李","王","赵","钱","孙"};
for(int i=0; i<names.size(); i++) {
settlers.push_back(Settler(i+1, names[i]));
}
}
void displayStatus() {
system("cls");
cout << "============== 冰征之旅 ==============\n";
cout << "第 " << day << " 天 | 温度: " << temperature << "°C";
cout << " | 科技点: " << techPoints << "\n\n";
cout << "【资源】食物: " << resources["food"]
<< " | 木材: " << resources["wood"]
<< " | 煤炭: " << resources["coal"] << "\n\n";
cout << "【居民状态】\n";
cout << left << setw(4) << "ID" << setw(6) << "姓名"
<< setw(6) << "健康" << setw(6) << "饥饿"
<< setw(6) << "体温" << "工作地点\n";
for(auto& s : settlers) {
if(s.alive) {
cout << setw(4) << s.id << setw(6) << s.name
<< setw(6) << s.health << setw(6) << s.hunger
<< setw(6) << s.warmth;
bool working = false;
for(auto& b : buildings) {
if(b.workers > 0 && find_if(settlers.begin(), settlers.end(),
[&](Settler& s){ return s.id == b.workers; }) != settlers.end()) {
cout << b.name;
working = true;
break;
}
}
if(!working) cout << "无";
cout << "\n";
} else {
cout << setw(4) << s.id << setw(6) << s.name << " (已死亡)\n";
}
}
cout << "\n【建筑设施】\n";
for(int i=0; i<buildings.size(); i++) {
cout << i+1 << ". " << buildings[i].name << " Lv." << buildings[i].level
<< " (" << buildings[i].workers << "/" << buildings[i].capacity << ")";
if(buildings[i].workers > 0)
cout << " - 工人ID: " << buildings[i].workers;
cout << "\n";
}
}
void assignWorker() {
cout << "\n选择建筑(1-" << buildings.size() << ", 0取消): ";
int bChoice;
cin >> bChoice;
if(bChoice < 1 || bChoice > buildings.size()) return;
Building& b = buildings[bChoice-1];
if(b.workers >= b.capacity) {
cout << "该建筑已满员!\n";
Sleep(1000);
return;
}
cout << "可用居民:\n";
vector<int> available;
for(auto& s : settlers) {
if(s.alive && !s.assigned) {
cout << s.id << ". " << s.name << "\n";
available.push_back(s.id);
}
}
if(available.empty()) {
cout << "没有可用居民!\n";
Sleep(1000);
return;
}
cout << "选择居民ID(0取消): ";
int sChoice;
cin >> sChoice;
if(find(available.begin(), available.end(), sChoice) != available.end()) {
b.workers++;
settlers[sChoice-1].assigned = true;
cout << "分配成功!\n";
Sleep(1000);
}
}
void manageFire() {
cout << "\n当前火源状态: " << (hasFire ? "燃烧中" : "熄灭") << "\n";
if(!hasFire) {
if(resources["wood"] >= 5) {
resources["wood"] -= 5;
hasFire = true;
cout << "成功点燃火源!\n";
} else {
cout << "木材不足!需要5木材\n";
}
} else {
if(resources["coal"] >= 3) {
resources["coal"] -= 3;
cout << "添加煤炭维持火源!\n";
} else {
hasFire = false;
cout << "煤炭不足,火源熄灭!\n";
}
}
Sleep(1500);
}
void upgradeBuilding() {
cout << "\n选择要升级的建筑(1-" << buildings.size() << ", 0取消): ";
int choice;
cin >> choice;
if(choice < 1 || choice > buildings.size()) return;
Building& b = buildings[choice-1];
if(techPoints < b.techCost) {
cout << "科技点不足!需要" << b.techCost << "点\n";
} else {
techPoints -= b.techCost;
b.upgrade();
cout << b.name << " 升级到 Lv." << b.level << "!\n";
}
Sleep(1500);
}
void distributeFood() {
cout << "\n当前食物储备: " << resources["food"] << "\n";
cout << "每人消耗5食物,分配吗?(1:是 0:否): ";
int choice;
cin >> choice;
if(choice == 1) {
int needed = count_if(settlers.begin(), settlers.end(),
[](Settler& s){ return s.alive; }) * 5;
if(resources["food"] >= needed) {
resources["food"] -= needed;
for(auto& s : settlers) {
if(s.alive) s.hunger = max(0, s.hunger-25);
}
cout << "食物分配完成!\n";
} else {
cout << "食物不足!\n";
}
}
Sleep(1500);
}
void processDay() {
// 温度变化
temperature = BASE_TEMP + (rand()%15 - 7);
// 资源生产
for(auto& b : buildings) {
if(b.workers > 0) {
switch(b.resourceType) {
case 0: resources["food"] += b.produce(); break;
case 1: resources["wood"] += b.produce(); break;
case 2: resources["coal"] += b.produce(); break;
}
techPoints += b.level;
}
}
// 居民状态更新
for(auto& s : settlers) {
s.updateStatus(temperature, hasFire);
s.assigned = false;
}
// 建筑工人重置
for(auto& b : buildings) {
b.workers = 0;
}
day++;
randomEvent();
}
void randomEvent() {
int event = rand()%100;
if(event < 15) { // 暴风雪
cout << "\n★ 遭遇暴风雪!温度骤降!\n";
temperature -= 10;
for(auto& s : settlers) {
if(s.alive) s.warmth -= 15;
}
Sleep(1500);
}
else if(event < 30) { // 发现物资
string type;
int amount = rand()%15 + 5;
switch(rand()%3) {
case 0: type = "food"; break;
case 1: type = "wood"; break;
case 2: type = "coal"; break;
}
resources[type] += amount;
cout << "\n★ 发现物资!获得 " << amount << " "
<< (type=="food"?"食物":type=="wood"?"木材":"煤炭") << "\n";
Sleep(1500);
}
}
void showMenu() {
cout << "\n【每日指令】\n";
cout << "1. 分配工人\n";
cout << "2. 管理火源\n";
cout << "3. 分配食物\n";
cout << "4. 升级建筑\n";
cout << "5. 结束本日\n";
cout << "0. 退出游戏\n";
cout << "选择: ";
}
public:
void run() {
srand(time(0));
initGame();
while(true) {
displayStatus();
showMenu();
int choice;
cin >> choice;
switch(choice) {
case 1: assignWorker(); break;
case 2: manageFire(); break;
case 3: distributeFood(); break;
case 4: upgradeBuilding(); break;
case 5: processDay(); break;
case 0: return;
default: cout << "无效输入!\n"; Sleep(1000);
}
// 检查游戏结束
int alive = count_if(settlers.begin(), settlers.end(),
[](Settler& s){ return s.alive; });
if(alive == 0) {
displayStatus();
cout << "\n=== 游戏结束 ===\n";
cout << "聚集区在第 " << day << " 天覆灭\n";
system("pause");
return;
}
if(day > MAX_DAYS) {
displayStatus();
cout << "\n=== 生存成功 ===\n";
cout << "恭喜度过极地寒冬!\n";
system("pause");
return;
}
}
}
};
int main() {
IceSettlementGame game;
game.run();
return 0;
}
全部评论 1
ddd
20小时前 来自 辽宁
0
有帮助,赞一个