C++#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
#include <climits> // 新增:包含INT_MAX的定义头文件
using namespace std;
// 全局常量定义(避免魔法数字)
const int MAP_SIZE = 25; // 地图尺寸 25x25
const int MAX_ROUNDS = 80; // 最大回合数
const int ZOMBIE_RESURRECT_ROUNDS = 3; // 僵尸复活冷却回合
const int RESURRECT_DISTANCE = 10; // 僵尸复活最小距离(人类)
const int GRENADE_RANGE = 5; // 手雷投掷距离
const int GRENADE_AOE = 2; // 手雷AOE范围(中心±2,共5x5)
const int HUNTER_ATTACK_RANGE = 1;// 猎手攻击范围(九宫格)
const int MAX_CHARACTERS = 10; // 1玩家+9人机
// 道具类型枚举(区分人类/僵尸道具)
enum ItemType {
NONE,
// 人类道具
AMMO_10, AMMO_15, AMMO_20, AMMO_30,
GRENADE_1, GRENADE_2,
SMG, SNIPER,
HAZMAT_SUIT,
// 僵尸道具
ZOMBIE_HP_1, ZOMBIE_HP_2, ZOMBIE_HP_3, ZOMBIE_ATK_1
};
// 武器类型枚举
enum WeaponType {
PISTOL, // 手枪:单发,无伤害加成
SMG_GUN, // 冲锋枪:两发
SNIPER_RIFLE // 狙击枪:单发+2伤害,无视障碍,装弹1回合
};
// 角色类型枚举
enum RoleType {
HUMAN, // 人类
ZOMBIE, // 僵尸
HUNTER // 猎手
};
// 坐标结构体
struct Point {
int x, y;
Point(int x=0, int y=0) : x(x), y(y) {}
// 计算两点曼哈顿距离
int distance(const Point& p) const {
return abs(x - p.x) + abs(y - p.y);
}
// 判断是否在地图内
bool isInMap() const {
return x >= 0 && x < MAP_SIZE && y >= 0 && y < MAP_SIZE;
}
};
// 角色结构体(封装所有属性)
struct Character {
RoleType type;
Point pos;
int hp; // 血量
int stamina; // 本回合剩余体力
bool alive; // 是否存活(僵尸死亡后进入复活冷却)
int resurrectTimer; // 复活倒计时(仅僵尸)
// 人类/猎手专属属性
WeaponType weapon;
int ammo; // 当前武器子弹
int grenade; // 手雷数量
bool hasHazmat; // 是否有防化服
bool isReloading; // 狙击枪装弹中
// 僵尸专属属性
int atk; // 攻击力(基础3)
bool buffs[4]; // 僵尸buff:+1血、+2血、+3血、+1攻(死亡后失效)
// 构造函数初始化
Character() {
type = HUMAN;
pos = Point(0, 0);
hp = 1;
stamina = 2;
alive = true;
resurrectTimer = 0;
weapon = PISTOL;
ammo = 20;
grenade = 0;
hasHazmat = false;
isReloading = false;
atk = 3;
memset(buffs, false, sizeof(buffs));
}
};
// 地图类
struct Map {
char grid[MAP_SIZE][MAP_SIZE]; // 地图格子:'1'-'3'、'#'(障碍)、'C'(补给箱)
bool visible[MAP_SIZE][MAP_SIZE]; // 玩家可见性(仅用于显示)
// 构造函数:初始化地图(符合题目数量限制)
Map() {
// 初始化地图格子
memset(grid, '1', sizeof(grid));
int cnt1 = 0, cnt2 = 0, cnt3 = 0;
// 随机生成数字格和障碍
for (int i=0; i<MAP_SIZE; i++) {
for (int j=0; j<MAP_SIZE; j++) {
int randVal = rand() % 10;
if (randVal < 4) { // 40% 概率是数字格
if (cnt1 < 200 && rand() % 3 == 0) {
grid[i][j] = '1'; cnt1++;
} else if (cnt2 < 150 && rand() % 2 == 0) {
grid[i][j] = '2'; cnt2++;
} else if (cnt3 < 100) {
grid[i][j] = '3'; cnt3++;
} else {
grid[i][j] = '1'; cnt1++; // 兜底
}
} else if (randVal < 6) { // 20% 概率是障碍
grid[i][j] = '#';
} else { // 40% 概率是普通格(体力1)
grid[i][j] = '1'; cnt1++;
}
}
}
// 随机生成10个补给箱(避免重叠在障碍上)
int supplyCnt = 0;
while (supplyCnt < 10) {
int x = rand() % MAP_SIZE;
int y = rand() % MAP_SIZE;
if (grid[x][y] != '#') {
grid[x][y] = 'C';
supplyCnt++;
}
}
// 初始化可见性为false
memset(visible, false, sizeof(visible));
}
// 计算移动到(x,y)所需体力(-1表示不可移动)
int getStaminaCost(int x, int y) const {
if (!Point(x,y).isInMap() || grid[x][y] == '#') return -1;
if (grid[x][y] == 'C') return 1; // 补给箱格子体力1
return grid[x][y] - '0';
}
// 更新玩家视野(障碍物遮挡:射线检测)
void updateVision(const Point& playerPos) {
memset(visible, false, sizeof(visible));
visible[playerPos.x][playerPos.y] = true;
// 四个方向射线检测(上下左右)
int dirs[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
for (int d=0; d<4; d++) {
int x = playerPos.x + dirs[d][0];
int y = playerPos.y + dirs[d][1];
while (Point(x,y).isInMap() && grid[x][y] != '#') {
visible[x][y] = true;
x += dirs[d][0];
y += dirs[d][1];
}
}
}
// 显示地图(仅显示可见区域,标注角色位置)
void display(const Character characters[], const Point& playerPos, int currentRound) {
updateVision(playerPos);
system("cls"); // Windows清屏(Dev-C++默认环境)
// 显示标题和回合数
printf("=== 25x25 生存游戏(回合:%d/%d)===\n", currentRound, MAX_ROUNDS);
// 显示地图网格
for (int i=0; i<MAP_SIZE; i++) {
for (int j=0; j<MAP_SIZE; j++) {
// 优先显示角色
bool hasChar = false;
for (int k=0; k<MAX_CHARACTERS; k++) {
if (characters[k].alive && characters[k].pos.x == i && characters[k].pos.y == j) {
if (characters[k].type == HUMAN) printf("H ");
else if (characters[k].type == ZOMBIE) printf("Z ");
else if (characters[k].type == HUNTER) printf("T ");
hasChar = true;
break;
}
}
if (hasChar) continue;
// 显示格子类型(仅可见区域)
if (!visible[i][j]) {
printf("? ");
} else {
if (grid[i][j] == '#') printf("# ");
else if (grid[i][j] == 'C') printf("C ");
else printf("%c ", grid[i][j]);
}
}
printf("\n");
}
// 显示玩家状态
const Character& player = characters[0];
printf("\n=== 玩家状态 ===\n");
printf("角色类型:%s\n", player.type == HUMAN ? "人类" : "猎手");
printf("血量:%d | 剩余体力:%d\n", player.hp, player.stamina);
printf("武器:");
if (player.weapon == PISTOL) printf("手枪");
else if (player.weapon == SMG_GUN) printf("冲锋枪");
else if (player.weapon == SNIPER_RIFLE) printf("狙击枪");
printf("(子弹:%d)\n", player.ammo);
printf("手雷:%d | 防化服:%s\n", player.grenade, player.hasHazmat ? "有" : "无");
if (player.isReloading) printf("状态:狙击枪装弹中(下一回合可用)\n");
}
};
// 全局变量(简化模块间访问,适合小型项目)
Map gameMap;
Character characters[MAX_CHARACTERS]; // 0号=玩家,1-9=人机
int currentRound = 1;
bool hasHunter = false; // 是否已有猎手出现
// 生成人类补给箱道具(按概率)
ItemType generateHumanItem() {
int randVal = rand() % 100;
if (randVal < 25) return AMMO_10;
else if (randVal < 45) return AMMO_15;
else if (randVal < 60) return AMMO_20;
else if (randVal < 65) return AMMO_30;
else if (randVal < 75) return GRENADE_1;
else if (randVal < 80) return GRENADE_2;
else if (randVal < 85) return SMG;
else if (randVal < 90) return SNIPER;
else return HAZMAT_SUIT;
}
// 生成僵尸补给箱道具(按概率)
ItemType generateZombieItem() {
int randVal = rand() % 100;
if (randVal < 40) return ZOMBIE_HP_1;
else if (randVal < 70) return ZOMBIE_HP_2;
else if (randVal < 90) return ZOMBIE_HP_3;
else return ZOMBIE_ATK_1;
}
// 打开补给箱(根据角色类型分配道具)
void openSupplyBox(Character& c) {
if (c.type == HUMAN || c.type == HUNTER) {
ItemType item = generateHumanItem();
printf("\n打开补给箱,获得:");
switch(item) {
case AMMO_10: c.ammo +=10; printf("子弹x10"); break;
case AMMO_15: c.ammo +=15; printf("子弹x15"); break;
case AMMO_20: c.ammo +=20; printf("子弹x20"); break;
case AMMO_30: c.ammo +=30; printf("子弹x30"); break;
case GRENADE_1: c.grenade +=1; printf("手雷x1"); break;
case GRENADE_2: c.grenade +=2; printf("手雷x2"); break;
case SMG: c.weapon = SMG_GUN; printf("冲锋枪"); break;
case SNIPER: c.weapon = SNIPER_RIFLE; printf("狙击枪"); break;
case HAZMAT_SUIT: c.hasHazmat = true; printf("防化服"); break;
default: break;
}
} else if (c.type == ZOMBIE && c.alive) {
ItemType item = generateZombieItem();
printf("\n僵尸打开补给箱,获得:");
switch(item) {
case ZOMBIE_HP_1: c.hp +=1; c.buffs[0] = true; printf("血量+1"); break;
case ZOMBIE_HP_2: c.hp +=2; c.buffs[1] = true; printf("血量+2"); break;
case ZOMBIE_HP_3: c.hp +=3; c.buffs[2] = true; printf("血量+3"); break;
case ZOMBIE_ATK_1: c.atk +=1; c.buffs[3] = true; printf("攻击力+1"); break;
default: break;
}
}
printf("!按任意键继续...\n");
fflush(stdin); // 清空输入缓冲
getchar();
}
// 检查两点是否在同一直线(水平/垂直)
bool isStraightLine(const Point& p1, const Point& p2) {
return p1.x == p2.x || p1.y == p2.y;
}
// 检查直线上是否有障碍物(狙击枪无视)
bool hasObstacle(const Point& p1, const Point& p2) {
int x1 = min(p1.x, p2.x), x2 = max(p1.x, p2.x);
int y1 = min(p1.y, p2.y), y2 = max(p1.y, p2.y);
if (p1.x == p2.x) { // 垂直直线
for (int y=y1+1; y<y2; y++) {
if (gameMap.grid[p1.x][y] == '#') return true;
}
} else { // 水平直线
for (int x=x1+1; x<x2; x++) {
if (gameMap.grid[x][p1.y] == '#') return true;
}
}
return false;
}
// 人类射击逻辑
void humanShoot(Character& shooter) {
if (shooter.isReloading) {
printf("狙击枪装弹中,本回合无法射击!\n");
fflush(stdin);
getchar();
return;
}
if (shooter.ammo <= 0) {
printf("子弹耗尽,无法射击!\n");
fflush(stdin);
getchar();
return;
}
int tx, ty;
printf("请输入射击目标坐标(x y,空格分隔):");
fflush(stdin);
scanf("%d %d", &tx, &ty);
Point target(tx, ty);
// 合法性检查
if (!target.isInMap()) {
printf("目标超出地图范围!\n");
fflush(stdin);
getchar();
return;
}
if (!isStraightLine(shooter.pos, target)) {
printf("只能水平或垂直射击!\n");
fflush(stdin);
getchar();
return;
}
if (shooter.weapon != SNIPER_RIFLE && hasObstacle(shooter.pos, target)) {
printf("视线被障碍物阻挡,无法射击!\n");
fflush(stdin);
getchar();
return;
}
// 计算伤害
int damage = 1;
if (shooter.weapon == SNIPER_RIFLE) damage += 2; // 狙击枪+2伤害
// 寻找目标僵尸
bool hit = false;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == ZOMBIE &&
characters[i].pos.x == tx && characters[i].pos.y == ty) {
characters[i].hp -= damage;
printf("击中僵尸!造成%d点伤害,僵尸剩余血量:%d\n", damage, characters[i].hp);
if (characters[i].hp <= 0) {
characters[i].alive = false;
printf("僵尸被消灭!\n");
if (hasHunter) {
printf("猎手在场,僵尸无法复活!\n");
} else {
characters[i].resurrectTimer = ZOMBIE_RESURRECT_ROUNDS;
printf("僵尸将在%d回合后复活!\n", ZOMBIE_RESURRECT_ROUNDS);
}
// 僵尸死亡,buff失效
memset(characters[i].buffs, false, sizeof(characters[i].buffs));
characters[i].atk = 3; // 重置攻击力
}
hit = true;
break;
}
}
if (!hit) {
printf("未击中任何僵尸!\n");
}
// 消耗子弹和处理狙击枪装弹
if (shooter.weapon == SMG_GUN) {
shooter.ammo -= 2;
if (shooter.ammo < 0) shooter.ammo = 0;
} else {
shooter.ammo -= 1;
if (shooter.weapon == SNIPER_RIFLE) {
shooter.isReloading = true;
printf("狙击枪射击后需要装弹,下一回合无法射击!\n");
}
}
printf("剩余子弹:%d 按任意键继续...\n", shooter.ammo);
fflush(stdin);
getchar();
}
// 投掷手雷逻辑
void throwGrenade(Character& thrower) {
if (thrower.grenade <= 0) {
printf("手雷耗尽,无法投掷!\n");
fflush(stdin);
getchar();
return;
}
int tx, ty;
printf("请输入手雷投掷目标坐标(x y,空格分隔,最大距离%d):", GRENADE_RANGE);
fflush(stdin);
scanf("%d %d", &tx, &ty);
Point target(tx, ty);
// 合法性检查
if (!target.isInMap()) {
printf("目标超出地图范围!\n");
fflush(stdin);
getchar();
return;
}
if (thrower.pos.distance(target) > GRENADE_RANGE) {
printf("投掷距离超出限制(最大%d格)!\n", GRENADE_RANGE);
fflush(stdin);
getchar();
return;
}
// 手雷爆炸:5x5范围伤害
printf("手雷落地爆炸!5x5范围内僵尸受到3点伤害!\n");
thrower.grenade--;
bool hitZombie = false;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == ZOMBIE) {
// 检查是否在AOE范围内(中心±2)
if (abs(characters[i].pos.x - target.x) <= GRENADE_AOE &&
abs(characters[i].pos.y - target.y) <= GRENADE_AOE) {
characters[i].hp -= 3;
printf("僵尸受到3点爆炸伤害,剩余血量:%d\n", characters[i].hp);
if (characters[i].hp <= 0) {
characters[i].alive = false;
printf("僵尸被手雷炸死!\n");
if (hasHunter) {
printf("猎手在场,僵尸无法复活!\n");
} else {
characters[i].resurrectTimer = ZOMBIE_RESURRECT_ROUNDS;
printf("僵尸将在%d回合后复活!\n", ZOMBIE_RESURRECT_ROUNDS);
}
memset(characters[i].buffs, false, sizeof(characters[i].buffs));
characters[i].atk = 3;
}
hitZombie = true;
}
}
}
if (!hitZombie) {
printf("爆炸范围内没有僵尸!\n");
}
printf("剩余手雷:%d 按任意键继续...\n", thrower.grenade);
fflush(stdin);
getchar();
}
// 猎手近战攻击(九宫格范围)
void hunterAttack(Character& hunter) {
printf("猎手使用刀攻击!九宫格范围内僵尸受到3点伤害!\n");
bool hitZombie = false;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == ZOMBIE) {
// 九宫格判断(x±1,y±1)
if (abs(characters[i].pos.x - hunter.pos.x) <= HUNTER_ATTACK_RANGE &&
abs(characters[i].pos.y - hunter.pos.y) <= HUNTER_ATTACK_RANGE) {
characters[i].hp -= 3;
printf("猎手击中僵尸!剩余血量:%d\n", characters[i].hp);
if (characters[i].hp <= 0) {
characters[i].alive = false;
printf("僵尸被猎手斩杀!\n");
memset(characters[i].buffs, false, sizeof(characters[i].buffs));
characters[i].atk = 3;
}
hitZombie = true;
}
}
}
if (!hitZombie) {
printf("九宫格范围内没有僵尸!\n");
}
printf("按任意键继续...\n");
fflush(stdin);
getchar();
}
// 僵尸攻击(上下左右范围)
void zombieAttack(Character& zombie) {
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && (characters[i].type == HUMAN || characters[i].type == HUNTER)) {
// 上下左右判断(曼哈顿距离=1)
if (zombie.pos.distance(characters[i].pos) == 1) {
if (characters[i].type == HUMAN && characters[i].hasHazmat) {
characters[i].hasHazmat = false;
printf("人类的防化服抵挡了僵尸攻击!防化服失效!\n");
} else {
characters[i].hp -= zombie.atk;
printf("僵尸攻击%s!造成%d点伤害!\n",
characters[i].type == HUMAN ? "人类" : "猎手", zombie.atk);
if (characters[i].hp <= 0) {
characters[i].alive = false;
if (characters[i].type == HUMAN) {
printf("人类被感染,变成僵尸!\n");
// 人类变僵尸:重置属性
characters[i].type = ZOMBIE;
characters[i].hp = 5;
characters[i].stamina = 3;
characters[i].atk = 3;
characters[i].alive = true;
memset(characters[i].buffs, false, sizeof(characters[i].buffs));
} else {
printf("猎手被消灭!\n");
}
}
}
}
}
}
}
// 随机打乱数组(替代random_shuffle,兼容Dev-C++)
void shuffleArray(vector<int>& arr) {
int n = arr.size();
for (int i = n-1; i > 0; i--) {
int j = rand() % (i+1);
swap(arr[i], arr[j]);
}
}
// 寻找僵尸复活位置(远离人类≥10格,尽可能远)
Point findResurrectPos() {
vector<Point> humanPositions;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && (characters[i].type == HUMAN || characters[i].type == HUNTER)) {
humanPositions.push_back(characters[i].pos);
}
}
// 无人类时随机复活
if (humanPositions.empty()) {
while (true) {
int x = rand() % MAP_SIZE;
int y = rand() % MAP_SIZE;
if (gameMap.grid[x][y] != '#') {
return Point(x, y);
}
}
}
// 寻找最大距离的可用格子
Point bestPos;
int maxMinDist = -1;
for (int x=0; x<MAP_SIZE; x++) {
for (int y=0; y<MAP_SIZE; y++) {
if (gameMap.grid[x][y] == '#') continue;
Point p(x, y);
// 计算到最近人类的距离
int minDist = INT_MAX; // 571行:INT_MAX需包含<climits>头文件
for (size_t k=0; k<humanPositions.size(); k++) { // 补充:用size_t避免unsigned/signed警告
int dist = p.distance(humanPositions[k]);
if (dist < minDist) minDist = dist;
}
// 更新最优位置
if (minDist > maxMinDist) {
maxMinDist = minDist;
bestPos = p;
if (maxMinDist >= RESURRECT_DISTANCE) break; // 满足最小距离,提前退出
}
}
if (maxMinDist >= RESURRECT_DISTANCE) break;
}
return bestPos;
}
// 检查是否生成猎手(场上只剩2名人类)
void checkHunter() {
if (hasHunter) return;
int humanCount = 0;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == HUMAN) {
humanCount++;
}
}
if (humanCount == 2) {
printf("\n=== 场上只剩2名人类,变身猎手! ===");
hasHunter = true;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == HUMAN) {
// 猎手属性重置
characters[i].type = HUNTER;
characters[i].hp = 10;
characters[i].stamina = 1;
characters[i].weapon = PISTOL; // 武器失效,强制用刀
characters[i].ammo = 0;
characters[i].grenade = 0;
characters[i].hasHazmat = false;
characters[i].isReloading = false;
}
}
printf("\n按任意键继续...\n");
fflush(stdin);
getchar();
}
}
// 检查胜利条件(1=人类胜,2=僵尸胜,0=未分胜负)
int checkWinCondition() {
// 1. 80回合后人类/猎手存活 → 人类胜
if (currentRound > MAX_ROUNDS) {
int humanHunterCount = 0;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && (characters[i].type == HUMAN || characters[i].type == HUNTER)) {
humanHunterCount++;
}
}
return humanHunterCount > 0 ? 1 : 2;
}
// 2. 80回合内全被感染 → 僵尸胜
int humanHunterCount = 0;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && (characters[i].type == HUMAN || characters[i].type == HUNTER)) {
humanHunterCount++;
}
}
if (humanHunterCount == 0) return 2;
// 3. 僵尸全被猎手消灭 → 人类胜
if (hasHunter) {
int zombieCount = 0;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == ZOMBIE) {
zombieCount++;
}
}
if (zombieCount == 0) return 1;
}
return 0;
}
// 玩家回合操作
void playerTurn() {
Character& player = characters[0];
while (player.stamina > 0) {
// 显示地图和状态
gameMap.display(characters, player.pos, currentRound);
// 显示操作菜单
printf("\n=== 玩家操作 ===");
printf("\n1. 移动 2. 射击 3. 投掷手雷 ");
if (player.type == HUNTER) printf("4. 近战攻击 ");
printf("0. 结束回合");
printf("\n请选择操作(0-%d):", player.type == HUNTER ? 4 : 3);
int opt;
fflush(stdin);
scanf("%d", &opt);
switch(opt) {
case 1: { // 移动
int dx, dy;
printf("请输入移动方向(dx dy,如1 0向右,-1 0向左):");
fflush(stdin);
scanf("%d %d", &dx, &dy);
Point newPos(player.pos.x + dx, player.pos.y + dy);
// 合法性检查
if (!newPos.isInMap()) {
printf("移动超出地图范围!\n");
fflush(stdin);
getchar();
continue;
}
int cost = gameMap.getStaminaCost(newPos.x, newPos.y);
if (cost == -1) {
printf("无法移动到障碍物或非法格子!\n");
fflush(stdin);
getchar();
continue;
}
if (cost > player.stamina) {
printf("体力不足(需要%d,剩余%d)!\n", cost, player.stamina);
fflush(stdin);
getchar();
continue;
}
// 移动成功
player.pos = newPos;
player.stamina -= cost;
// 检查是否踩补给箱
if (gameMap.grid[newPos.x][newPos.y] == 'C') {
openSupplyBox(player);
gameMap.grid[newPos.x][newPos.y] = '1'; // 补给箱打开后失效
}
break;
}
case 2: { // 射击(猎手不可用)
if (player.type == HUNTER) {
printf("猎手只能使用近战攻击!\n");
fflush(stdin);
getchar();
continue;
}
humanShoot(player);
break;
}
case 3: { // 投掷手雷(猎手不可用)
if (player.type == HUNTER) {
printf("猎手不能使用手雷!\n");
fflush(stdin);
getchar();
continue;
}
throwGrenade(player);
break;
}
case 4: { // 猎手近战攻击
if (player.type != HUNTER) {
printf("只有猎手能使用近战攻击!\n");
fflush(stdin);
getchar();
continue;
}
hunterAttack(player);
player.stamina = 0; // 猎手攻击后耗尽体力
break;
}
case 0: { // 结束回合
player.stamina = 0;
break;
}
default: {
printf("无效操作!\n");
fflush(stdin);
getchar();
break;
}
}
}
// 重置狙击枪装弹状态
if (player.isReloading) {
player.isReloading = false;
}
}
// 人机AI回合操作
void aiTurn() {
for (int i=1; i<MAX_CHARACTERS; i++) {
Character& ai = characters[i];
if (!ai.alive) continue;
// 重置本回合体力
if (ai.type == HUMAN) {
ai.stamina = (ai.weapon == PISTOL) ? 2 : 1; // 持枪1点,不持枪2点
} else if (ai.type == ZOMBIE) {
ai.stamina = 3;
} else if (ai.type == HUNTER) {
ai.stamina = 1;
}
while (ai.stamina > 0) {
if (ai.type == HUMAN) {
// 人类AI:优先找补给箱,其次随机移动
Point targetPos = ai.pos;
bool foundSupply = false;
int minDist = INT_MAX;
// 寻找最近的补给箱
for (int x=0; x<MAP_SIZE; x++) {
for (int y=0; y<MAP_SIZE; y++) {
if (gameMap.grid[x][y] == 'C') {
Point p(x, y);
int dist = ai.pos.distance(p);
if (dist < minDist) {
minDist = dist;
targetPos = p;
foundSupply = true;
}
}
}
}
// 移动逻辑(水平优先)
if (foundSupply) {
if (ai.pos.x < targetPos.x && gameMap.getStaminaCost(ai.pos.x+1, ai.pos.y) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x+1, ai.pos.y);
if (cost <= ai.stamina) {
ai.pos.x++;
ai.stamina -= cost;
} else break;
} else if (ai.pos.x > targetPos.x && gameMap.getStaminaCost(ai.pos.x-1, ai.pos.y) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x-1, ai.pos.y);
if (cost <= ai.stamina) {
ai.pos.x--;
ai.stamina -= cost;
} else break;
} else if (ai.pos.y < targetPos.y && gameMap.getStaminaCost(ai.pos.x, ai.pos.y+1) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x, ai.pos.y+1);
if (cost <= ai.stamina) {
ai.pos.y++;
ai.stamina -= cost;
} else break;
} else if (ai.pos.y > targetPos.y && gameMap.getStaminaCost(ai.pos.x, ai.pos.y-1) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x, ai.pos.y-1);
if (cost <= ai.stamina) {
ai.pos.y--;
ai.stamina -= cost;
} else break;
} else {
break; // 无法移动到补给箱
}
} else {
// 随机移动
int dirs[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
int randDir = rand() % 4;
Point newPos(ai.pos.x + dirs[randDir][0], ai.pos.y + dirs[randDir][1]);
int cost = gameMap.getStaminaCost(newPos.x, newPos.y);
if (cost != -1 && cost <= ai.stamina) {
ai.pos = newPos;
ai.stamina -= cost;
} else {
break;
}
}
// 检查补给箱
if (gameMap.grid[ai.pos.x][ai.pos.y] == 'C') {
openSupplyBox(ai);
gameMap.grid[ai.pos.x][ai.pos.y] = '1';
}
// 看到僵尸就射击
for (int j=0; j<MAX_CHARACTERS; j++) {
if (characters[j].alive && characters[j].type == ZOMBIE &&
isStraightLine(ai.pos, characters[j].pos) &&
(ai.weapon == SNIPER_RIFLE || !hasObstacle(ai.pos, characters[j].pos))) {
if (ai.ammo > 0 && !ai.isReloading) {
printf("人机人类射击僵尸!\n");
int damage = (ai.weapon == SNIPER_RIFLE) ? 3 : 1;
characters[j].hp -= damage;
if (characters[j].hp <= 0) {
characters[j].alive = false;
printf("人机人类消灭僵尸!\n");
if (hasHunter) {
printf("猎手在场,僵尸无法复活!\n");
} else {
characters[j].resurrectTimer = ZOMBIE_RESURRECT_ROUNDS;
}
memset(characters[j].buffs, false, sizeof(characters[j].buffs));
characters[j].atk = 3;
}
// 消耗子弹
if (ai.weapon == SMG_GUN) ai.ammo -= 2;
else {
ai.ammo -= 1;
if (ai.weapon == SNIPER_RIFLE) ai.isReloading = true;
}
break;
}
}
}
} else if (ai.type == ZOMBIE) {
// 僵尸AI:向最近的人类/猎手移动并攻击
Point targetPos = ai.pos;
int minDist = INT_MAX;
// 寻找最近的目标
for (int j=0; j<MAX_CHARACTERS; j++) {
if (characters[j].alive && (characters[j].type == HUMAN || characters[j].type == HUNTER)) {
int dist = ai.pos.distance(characters[j].pos);
if (dist < minDist) {
minDist = dist;
targetPos = characters[j].pos;
}
}
}
// 移动逻辑(水平优先)
if (ai.pos.x < targetPos.x && gameMap.getStaminaCost(ai.pos.x+1, ai.pos.y) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x+1, ai.pos.y);
if (cost <= ai.stamina) {
ai.pos.x++;
ai.stamina -= cost;
} else break;
} else if (ai.pos.x > targetPos.x && gameMap.getStaminaCost(ai.pos.x-1, ai.pos.y) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x-1, ai.pos.y);
if (cost <= ai.stamina) {
ai.pos.x--;
ai.stamina -= cost;
} else break;
} else if (ai.pos.y < targetPos.y && gameMap.getStaminaCost(ai.pos.x, ai.pos.y+1) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x, ai.pos.y+1);
if (cost <= ai.stamina) {
ai.pos.y++;
ai.stamina -= cost;
} else break;
} else if (ai.pos.y > targetPos.y && gameMap.getStaminaCost(ai.pos.x, ai.pos.y-1) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x, ai.pos.y-1);
if (cost <= ai.stamina) {
ai.pos.y--;
ai.stamina -= cost;
} else break;
} else {
break; // 无法移动
}
// 攻击范围内目标
zombieAttack(ai);
// 检查补给箱
if (gameMap.grid[ai.pos.x][ai.pos.y] == 'C') {
openSupplyBox(ai);
gameMap.grid[ai.pos.x][ai.pos.y] = '1';
}
} else if (ai.type == HUNTER) {
// 猎手AI:向最近的僵尸移动并攻击
Point targetPos = ai.pos;
int minDist = INT_MAX;
// 寻找最近的僵尸
for (int j=0; j<MAX_CHARACTERS; j++) {
if (characters[j].alive && characters[j].type == ZOMBIE) {
int dist = ai.pos.distance(characters[j].pos);
if (dist < minDist) {
minDist = dist;
targetPos = characters[j].pos;
}
}
}
// 移动逻辑
if (ai.pos.x < targetPos.x && gameMap.getStaminaCost(ai.pos.x+1, ai.pos.y) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x+1, ai.pos.y);
if (cost <= ai.stamina) {
ai.pos.x++;
ai.stamina -= cost;
}
} else if (ai.pos.x > targetPos.x && gameMap.getStaminaCost(ai.pos.x-1, ai.pos.y) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x-1, ai.pos.y);
if (cost <= ai.stamina) {
ai.pos.x--;
ai.stamina -= cost;
}
} else if (ai.pos.y < targetPos.y && gameMap.getStaminaCost(ai.pos.x, ai.pos.y+1) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x, ai.pos.y+1);
if (cost <= ai.stamina) {
ai.pos.y++;
ai.stamina -= cost;
}
} else if (ai.pos.y > targetPos.y && gameMap.getStaminaCost(ai.pos.x, ai.pos.y-1) != -1) {
int cost = gameMap.getStaminaCost(ai.pos.x, ai.pos.y-1);
if (cost <= ai.stamina) {
ai.pos.y--;
ai.stamina -= cost;
}
}
// 攻击
hunterAttack(ai);
ai.stamina = 0; // 攻击后耗尽体力
}
}
// 重置狙击枪装弹状态
if (ai.type == HUMAN && ai.isReloading) {
ai.isReloading = false;
}
}
}
// 僵尸复活处理(倒计时结束后复活)
void handleZombieResurrect() {
for (int i=0; i<MAX_CHARACTERS; i++) {
if (!characters[i].alive && characters[i].type == ZOMBIE && characters[i].resurrectTimer > 0) {
characters[i].resurrectTimer--;
if (characters[i].resurrectTimer == 0) {
characters[i].pos = findResurrectPos();
characters[i].hp = 5;
characters[i].alive = true;
characters[i].atk = 3;
memset(characters[i].buffs, false, sizeof(characters[i].buffs));
printf("僵尸在(%d,%d)复活!\n", characters[i].pos.x, characters[i].pos.y);
printf("按任意键继续...\n");
fflush(stdin);
getchar();
}
}
}
}
// 第25回合:随机2名人类变僵尸
void turn25ZombieTransform() {
if (currentRound != 25) return;
printf("\n=== 第25回合!随机2名人类变成僵尸! ===");
vector<int> humanIndices;
for (int i=0; i<MAX_CHARACTERS; i++) {
if (characters[i].alive && characters[i].type == HUMAN) {
humanIndices.push_back(i);
}
}
if (humanIndices.size() < 2) return;
// 随机选择2人(兼容Dev-C++的shuffle)
shuffleArray(humanIndices);
for (int i=0; i<2; i++) {
int idx = humanIndices[i];
// 人类变僵尸:重置属性
characters[idx].type = ZOMBIE;
characters[idx].hp = 5;
characters[idx].stamina = 3;
characters[idx].atk = 3;
characters[idx].alive = true;
memset(characters[idx].buffs, false, sizeof(characters[idx].buffs));
printf("\n%s变成僵尸!", idx == 0 ? "玩家" : "人机人类");
}
printf("\n按任意键继续...\n");
fflush(stdin);
getchar();
}
// 初始化游戏(角色位置、状态)
void initGame() {
// 初始化随机种子
srand((unsigned int)time(NULL));
// 初始化角色位置(避免障碍)
for (int i=0; i<MAX_CHARACTERS; i++) {
while (true) {
int x = rand() % MAP_SIZE;
int y = rand() % MAP_SIZE;
if (gameMap.grid[x][y] != '#') {
characters[i].pos = Point(x, y);
break;
}
}
// 初始状态:人类、手枪20发
characters[i].type = HUMAN;
characters[i].hp = 1;
characters[i].stamina = 2;
characters[i].weapon = PISTOL;
characters[i].ammo = 20;
characters[i].grenade = 0;
characters[i].hasHazmat = false;
characters[i].isReloading = false;
characters[i].alive = true;
characters[i].resurrectTimer = 0;
characters[i].atk = 3;
memset(characters[i].buffs, false, sizeof(characters[i].buffs));
}
currentRound = 1;
hasHunter = false;
}
int main() {
initGame();
// 游戏开始提示
printf("=== 生存游戏终极版 ===\n");
printf("规则说明:\n");
printf("1. 地图25x25,1玩家+9人机,第25回合随机2人变僵尸\n");
printf("2. 人类:持枪1点体力/回合,不持枪2点;血量1;初始手枪20发\n");
printf("3. 僵尸:3点体力/回合,5点血量,3点伤害;死亡3回合后复活(猎手在场则不能)\n");
printf("4. 猎手:场上剩2人类时变身,10点血量,1点体力/回合,近战9宫格攻击\n");
printf("5. 80回合后人类/猎手存活,或僵尸全被猎手消灭 → 人类胜\n");
printf("6. 80回合内全被感染 → 僵尸胜\n");
printf("按任意键开始游戏...\n");
fflush(stdin);
getchar();
// 主游戏循环
while (true) {
// 检查胜利条件
int win = checkWinCondition();
if (win != 0) {
system("cls");
if (win == 1) printf("=== 人类方获胜! ===\n");
else printf("=== 僵尸方获胜! ===\n");
printf("游戏结束!按任意键退出...\n");
fflush(stdin);
getchar();
break;
}
// 第25回合僵尸变身
turn25ZombieTransform();
// 显示当前回合
printf("\n=== 第%d回合 ===", currentRound);
printf("\n按任意键开始本回合...\n");
fflush(stdin);
getchar();
// 玩家回合
playerTurn();
// 人机/僵尸回合
aiTurn();
// 僵尸复活处理
handleZombieResurrect();
// 检查猎手生成
checkHunter();
// 进入下一回合
currentRound++;
}
return 0;
}