C++#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <string.h>
// 核心常量定义(20×20地图,半区10列)
#define MAP_SIZE_X 20 // X轴(纵向)
#define MAP_SIZE_Y 20 // Y轴(横向)
#define PLAYER_AREA_Y 10 // 玩家海域:y < 10,AI海域:y >= 10
#define MAX_OIL 10
#define MAX_SHIPS 12 // 1+2+4+5=12艘战舰
// 分栏布局常量
#define MAP_LEFT 2
#define STATUS_LEFT 50
// 战舰类型
#define CARRIER 0
#define CRUISER 1
#define DESTROYER 2
#define SUBMARINE 3
// 方向类型(仅保留攻击相关,移除移动/转向方向)
#define HORIZONTAL 4 // 横向:y轴延伸
#define VERTICAL 5 // 纵向:x轴延伸
// 地图单元格状态
#define EMPTY 0
#define SHIP_PLAYER_C 1
#define SHIP_PLAYER_R 2
#define SHIP_PLAYER_D 3
#define SHIP_PLAYER_S 4
#define SHIP_AI_C 5
#define SHIP_AI_R 6
#define SHIP_AI_D 7
#define SHIP_AI_S 8
#define HIT 9 // 命中(无论是否击沉)
#define MISS 10 // 未命中(无战舰)
#define ATTACK_RANGE 11
#define ATTACK_CURSOR 12 // 攻击光标标记
// 自定义坐标结构体
typedef struct {
int x;
int y;
} Point;
// 战舰结构体(移除移动/转向相关字段)
typedef struct {
int type; // 战舰类型
char name[20]; // 战舰名称
char icon; // 地图显示图标
int id; // 战舰ID
int x, y; // 坐标(左上角锚点)
int dir; // 朝向(仅部署时使用)
int health; // 血量
int oilCost; // 油费消耗
int dmgWidth; // 攻击宽度(横向范围)
int dmgHeight; // 攻击高度(纵向范围)
int length; // 主长度
int width; // 宽度
int isAlive; // 是否存活(1=是,0=否)
int isPlayer; // 是否玩家战舰(1=是,0=否)
} Ship;
// 攻击选择类型
#define ATTACK_SINGLE 0 // 潜艇:单点
#define ATTACK_CENTER 1 // 航母:中心范围
#define ATTACK_LINE 2 // 巡洋/驱逐:直线首尾
// 游戏全局数据
Ship playerShips[MAX_SHIPS];
Ship aiShips[MAX_SHIPS];
int playerShipCount = 0;
int aiShipCount = 0;
int map[MAP_SIZE_X][MAP_SIZE_Y];
int playerOil;
int aiOil;
int shipIdCounter = 1;
// 控制台光标定位(兼容老编译器)
void gotoXY(int x, int y) {
COORD pos;
pos.X = (SHORT)x; // 显式转换为SHORT
pos.Y = (SHORT)y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
// 隐藏控制台光标
void hideCursor() {
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
// 显示控制台光标
void showCursor() {
CONSOLE_CURSOR_INFO cci;
cci.bVisible = TRUE;
cci.dwSize = 1;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
// 检查坐标是否在地图内
int isInMap(int x, int y) {
return (x >= 0 && x < MAP_SIZE_X && y >= 0 && y < MAP_SIZE_Y) ? 1 : 0;
}
// 检查坐标是否在己方海域
int isInOwnArea(int y, int isPlayer) {
if (isPlayer) {
return (y < PLAYER_AREA_Y) ? 1 : 0; // 玩家:y < 10
} else {
return (y >= PLAYER_AREA_Y) ? 1 : 0; // AI:y >= 10
}
}
// 获取战舰占用的所有坐标
void getShipCells(Ship* ship, Point cells[], int* cellCount) {
int k, dx, dy; // 老编译器要求变量在开头定义
*cellCount = 0;
int x = ship->x, y = ship->y;
int len = ship->length;
int wid = ship->width;
if (ship->dir == HORIZONTAL) {
// 横向:y轴延伸(左右)
for (dy = 0; dy < wid; dy++) {
for (dx = 0; dx < len; dx++) {
cells[*cellCount].x = x + dx;
cells[*cellCount].y = y + dy;
(*cellCount)++;
}
}
} else {
// 纵向:x轴延伸(上下)
for (dy = 0; dy < len; dy++) {
for (dx = 0; dx < wid; dx++) {
cells[*cellCount].x = x + dx;
cells[*cellCount].y = y + dy;
(*cellCount)++;
}
}
}
}
// 获取战舰对应的单元格状态
int getShipCellState(Ship* ship) {
if (ship->isPlayer) {
switch (ship->type) {
case CARRIER: return SHIP_PLAYER_C;
case CRUISER: return SHIP_PLAYER_R;
case DESTROYER: return SHIP_PLAYER_D;
case SUBMARINE: return SHIP_PLAYER_S;
default: return EMPTY;
}
} else {
switch (ship->type) {
case CARRIER: return SHIP_AI_C;
case CRUISER: return SHIP_AI_R;
case DESTROYER: return SHIP_AI_D;
case SUBMARINE: return SHIP_AI_S;
default: return EMPTY;
}
}
}
// 检查战舰放置是否合法(含半区限制)
int isShipPlacementValid(Ship* ship) {
Point cells[20];
int cellCount = 0;
int k; // 老编译器要求变量在开头定义
getShipCells(ship, cells, &cellCount);
for (k = 0; k < cellCount; k++) {
int x = cells[k].x;
int y = cells[k].y;
// 1. 越界检查
if (!isInMap(x, y)) return 0;
// 2. 半区限制:不能跨到对方海域
if (!isInOwnArea(y, ship->isPlayer)) return 0;
// 3. 己方重叠检查
if (ship->isPlayer) {
if (map[x][y] == SHIP_PLAYER_C || map[x][y] == SHIP_PLAYER_R ||
map[x][y] == SHIP_PLAYER_D || map[x][y] == SHIP_PLAYER_S) {
return 0;
}
} else {
if (map[x][y] == SHIP_AI_C || map[x][y] == SHIP_AI_R ||
map[x][y] == SHIP_AI_D || map[x][y] == SHIP_AI_S) {
return 0;
}
}
// 4. 跨阵营不重叠
if (ship->isPlayer) {
if (map[x][y] == SHIP_AI_C || map[x][y] == SHIP_AI_R ||
map[x][y] == SHIP_AI_D || map[x][y] == SHIP_AI_S) {
return 0;
}
} else {
if (map[x][y] == SHIP_PLAYER_C || map[x][y] == SHIP_PLAYER_R ||
map[x][y] == SHIP_PLAYER_D || map[x][y] == SHIP_PLAYER_S) {
return 0;
}
}
}
return 1;
}
// 标记战舰到地图
void markShipOnMap(Ship* ship) {
Point cells[20];
int cellCount = 0;
int k; // 老编译器要求变量在开头定义
getShipCells(ship, cells, &cellCount);
int state = getShipCellState(ship);
for (k = 0; k < cellCount; k++) {
int x = cells[k].x;
int y = cells[k].y;
// 仅在空位置标记战舰(避免覆盖HIT/MISS)
if (map[x][y] == EMPTY) {
map[x][y] = state;
}
}
}
// 清除战舰地图标记(保留HIT/MISS)
void clearShipFromMap(Ship* ship) {
Point cells[20];
int cellCount = 0;
int k; // 老编译器要求变量在开头定义
getShipCells(ship, cells, &cellCount);
for (k = 0; k < cellCount; k++) {
int x = cells[k].x;
int y = cells[k].y;
// 仅清除战舰标记,保留HIT/MISS
if (map[x][y] == getShipCellState(ship)) {
map[x][y] = EMPTY;
}
}
}
// 初始化战舰属性(移除移动相关字段)
void initShip(Ship* ship, int type, int id, int isPlayer) {
ship->type = type;
ship->id = id;
ship->isPlayer = isPlayer;
ship->isAlive = 1;
switch (type) {
case CARRIER:
strcpy(ship->name, "航母");
ship->icon = isPlayer ? 'C' : 'c';
ship->health = 20;
ship->oilCost = 3;
ship->dmgWidth = 3; // 3×3攻击范围
ship->dmgHeight = 3;
ship->length = 4;
ship->width = 2;
break;
case CRUISER:
strcpy(ship->name, "巡洋战舰");
ship->icon = isPlayer ? 'R' : 'r';
ship->health = 10;
ship->oilCost = 2;
ship->dmgWidth = 1; // 直线攻击(首尾坐标)
ship->dmgHeight = 3;
ship->length = 3; // 3×2尺寸
ship->width = 2;
break;
case DESTROYER:
strcpy(ship->name, "驱逐舰");
ship->icon = isPlayer ? 'D' : 'd';
ship->health = 5;
ship->oilCost = 1;
ship->dmgWidth = 1; // 直线攻击(首尾坐标)
ship->dmgHeight = 2;
ship->length = 3;
ship->width = 1;
break;
case SUBMARINE:
strcpy(ship->name, "潜艇");
ship->icon = isPlayer ? 'S' : 's';
ship->health = 3;
ship->oilCost = 1;
ship->dmgWidth = 1; // 单点攻击
ship->dmgHeight = 1;
ship->length = 2;
ship->width = 1;
break;
}
// 初始朝向(AI随机,玩家手动选)
ship->dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
// 初始坐标(AI随机,玩家手动选)
if (isPlayer) {
ship->x = 0;
ship->y = 0;
} else {
int maxX, maxY; // 老编译器要求变量在开头定义
maxX = MAP_SIZE_X - (ship->dir == VERTICAL ? ship->length : ship->width);
maxY = MAP_SIZE_Y - (ship->dir == HORIZONTAL ? ship->length : ship->width);
ship->x = rand() % (maxX > 0 ? maxX : 1);
ship->y = PLAYER_AREA_Y + (rand() % (maxY - PLAYER_AREA_Y)); // AI半区
}
}
// 自动部署AI战舰
void deployAIShips() {
int i, retryCount, maxX, maxY; // 老编译器要求变量在开头定义
Ship ship;
// 航母1艘
for (i = 0; i < 1; i++) {
initShip(&ship, CARRIER, shipIdCounter++, 0);
retryCount = 0;
while (!isShipPlacementValid(&ship) && retryCount < 200) {
maxX = MAP_SIZE_X - (ship.dir == VERTICAL ? ship.length : ship.width);
maxY = MAP_SIZE_Y - (ship.dir == HORIZONTAL ? ship.length : ship.width);
ship.x = rand() % (maxX > 0 ? maxX : 1);
ship.y = PLAYER_AREA_Y + (rand() % (maxY - PLAYER_AREA_Y));
ship.dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
retryCount++;
}
markShipOnMap(&ship);
aiShips[aiShipCount++] = ship;
}
// 巡洋舰2艘
for (i = 0; i < 2; i++) {
initShip(&ship, CRUISER, shipIdCounter++, 0);
retryCount = 0;
while (!isShipPlacementValid(&ship) && retryCount < 200) {
maxX = MAP_SIZE_X - (ship.dir == VERTICAL ? ship.length : ship.width);
maxY = MAP_SIZE_Y - (ship.dir == HORIZONTAL ? ship.length : ship.width);
ship.x = rand() % (maxX > 0 ? maxX : 1);
ship.y = PLAYER_AREA_Y + (rand() % (maxY - PLAYER_AREA_Y));
ship.dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
retryCount++;
}
markShipOnMap(&ship);
aiShips[aiShipCount++] = ship;
}
// 驱逐舰4艘
for (i = 0; i < 4; i++) {
initShip(&ship, DESTROYER, shipIdCounter++, 0);
retryCount = 0;
while (!isShipPlacementValid(&ship) && retryCount < 200) {
maxX = MAP_SIZE_X - (ship.dir == VERTICAL ? ship.length : ship.width);
maxY = MAP_SIZE_Y - (ship.dir == HORIZONTAL ? ship.length : ship.width);
ship.x = rand() % (maxX > 0 ? maxX : 1);
ship.y = PLAYER_AREA_Y + (rand() % (maxY - PLAYER_AREA_Y));
ship.dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
retryCount++;
}
markShipOnMap(&ship);
aiShips[aiShipCount++] = ship;
}
// 潜艇5艘
for (i = 0; i < 5; i++) {
initShip(&ship, SUBMARINE, shipIdCounter++, 0);
retryCount = 0;
while (!isShipPlacementValid(&ship) && retryCount < 200) {
maxX = MAP_SIZE_X - (ship.dir == VERTICAL ? ship.length : ship.width);
maxY = MAP_SIZE_Y - (ship.dir == HORIZONTAL ? ship.length : ship.width);
ship.x = rand() % (maxX > 0 ? maxX : 1);
ship.y = PLAYER_AREA_Y + (rand() % (maxY - PLAYER_AREA_Y));
ship.dir = (rand() % 2 == 0) ? HORIZONTAL : VERTICAL;
retryCount++;
}
markShipOnMap(&ship);
aiShips[aiShipCount++] = ship;
}
}
// 绘制部署预览地图
void drawDeployMap(int cursorX, int cursorY, Ship* ship, int isPreview) {
int i, j; // 老编译器要求变量在开头定义
char cellChar;
Point previewCells[20];
int previewCount = 0;
int k;
system("cls");
gotoXY(MAP_LEFT, 0);
printf("===== 手动部署战舰(你的海域:y < 10)=====\n");
printf("操作说明:方向键移动光标 | 空格键切换朝向 | 回车键确认部署\n");
printf("当前部署:%s | 尺寸:%d×%d | 朝向:%s\n",
ship->name,
ship->dir == HORIZONTAL ? ship->length : ship->width,
ship->dir == HORIZONTAL ? ship->width : ship->length,
ship->dir == HORIZONTAL ? "横向" : "纵向");
// 绘制列号
gotoXY(MAP_LEFT, 5);
printf(" ");
for (j = 0; j < MAP_SIZE_Y; j++) {
printf("%2d", j % 10);
}
// 绘制地图
for (i = 0; i < MAP_SIZE_X; i++) {
gotoXY(MAP_LEFT, 6 + i);
printf("%2d ", i % 10);
for (j = 0; j < MAP_SIZE_Y; j++) {
cellChar = ' ';
// 基础地图
if (map[i][j] == EMPTY) {
cellChar = '.';
} else if (map[i][j] == SHIP_PLAYER_C) {
cellChar = 'C';
} else if (map[i][j] == SHIP_PLAYER_R) {
cellChar = 'R';
} else if (map[i][j] == SHIP_PLAYER_D) {
cellChar = 'D';
} else if (map[i][j] == SHIP_PLAYER_S) {
cellChar = 'S';
}
// 半区分割线
if (j == PLAYER_AREA_Y - 1) {
printf(" %c|", cellChar);
} else {
printf(" %c", cellChar);
}
}
}
// 绘制预览(临时战舰位置)
if (isPreview) {
ship->x = cursorX;
ship->y = cursorY;
getShipCells(ship, previewCells, &previewCount);
for (k = 0; k < previewCount; k++) {
int x = previewCells[k].x;
int y = previewCells[k].y;
if (isInMap(x, y) && isInOwnArea(y, 1)) {
gotoXY(MAP_LEFT + 3 + y*2 + (y >= PLAYER_AREA_Y ? 1 : 0), 6 + x);
printf("%c", ship->icon);
}
}
}
// 绘制光标
gotoXY(MAP_LEFT + 3 + cursorY*2 + (cursorY >= PLAYER_AREA_Y ? 1 : 0), 6 + cursorX);
printf("%c", ship->icon);
}
// 可视化部署单艘战舰
void deploySingleShip(Ship* ship) {
int cursorX, cursorY;
int key;
int valid;
// 初始光标位置
cursorX = 0;
cursorY = 0;
hideCursor();
while (1) {
// 绘制预览地图
drawDeployMap(cursorX, cursorY, ship, 1);
// 检测按键
key = _getch();
if (key == 224) { // 方向键前缀
key = _getch();
switch (key) {
case 72: // 上
if (cursorX > 0) cursorX--;
break;
case 80: // 下
if (cursorX < MAP_SIZE_X - 1) cursorX++;
break;
case 75: // 左
if (cursorY > 0) cursorY--;
break;
case 77: // 右
if (cursorY < PLAYER_AREA_Y - 1) cursorY++; // 限制在玩家半区
break;
}
} else if (key == 32) { // 空格键切换朝向
ship->dir = (ship->dir == HORIZONTAL) ? VERTICAL : HORIZONTAL;
} else if (key == 13) { // 回车键确认
ship->x = cursorX;
ship->y = cursorY;
valid = isShipPlacementValid(ship);
if (valid) {
markShipOnMap(ship);
break;
} else {
gotoXY(MAP_LEFT, 30);
printf("部署失败!越界/重叠,请重新选择位置!");
_getch();
}
}
}
showCursor();
system("cls");
gotoXY(MAP_LEFT, 0);
printf("===== %s 部署成功!=====\n", ship->name);
printf("按任意键继续...");
getch();
}
// 可视化手动部署玩家战舰
void manualDeployPlayerShips() {
int i; // 老编译器要求变量在开头定义
Ship carrier, cruiser, destroyer, submarine;
// 部署航母1艘
initShip(&carrier, CARRIER, shipIdCounter++, 1);
deploySingleShip(&carrier);
playerShips[playerShipCount++] = carrier;
shipIdCounter++;
// 部署巡洋舰2艘
for (i = 0; i < 2; i++) {
initShip(&cruiser, CRUISER, shipIdCounter++, 1);
sprintf(cruiser.name, "巡洋战舰%d", i+1);
deploySingleShip(&cruiser);
playerShips[playerShipCount++] = cruiser;
shipIdCounter++;
}
// 部署驱逐舰4艘
for (i = 0; i < 4; i++) {
initShip(&destroyer, DESTROYER, shipIdCounter++, 1);
sprintf(destroyer.name, "驱逐舰%d", i+1);
deploySingleShip(&destroyer);
playerShips[playerShipCount++] = destroyer;
shipIdCounter++;
}
// 部署潜艇5艘
for (i = 0; i < 5; i++) {
initShip(&submarine, SUBMARINE, shipIdCounter++, 1);
sprintf(submarine.name, "潜艇%d", i+1);
deploySingleShip(&submarine);
playerShips[playerShipCount++] = submarine;
shipIdCounter++;
}
system("cls");
gotoXY(MAP_LEFT, 0);
printf("===== 所有战舰部署完成!=====\n");
printf("按任意键进入游戏...");
getch();
}
// 绘制攻击光标和范围(攻击选择专用)
void drawAttackCursor(int cursorX, int cursorY, int type, Point lineEnd, int showRange) {
int i, j;
char cellChar;
// 重新绘制地图(保留原有状态)
gotoXY(MAP_LEFT, 0);
printf("========== 攻击坐标选择 ==========");
gotoXY(MAP_LEFT, 1);
printf("操作:方向键移动 | 回车确认 | ESC取消 | 范围预览:%s", showRange ? "开启" : "关闭");
gotoXY(MAP_LEFT, 2);
printf("当前光标位置:(%d, %d)", cursorX, cursorY);
// 绘制列号
gotoXY(MAP_LEFT, 4);
printf(" ");
for (j = 0; j < MAP_SIZE_Y; j++) {
printf("%2d", j % 10);
}
// 绘制地图
for (i = 0; i < MAP_SIZE_X; i++) {
gotoXY(MAP_LEFT, 5 + i);
printf("%2d ", i % 10);
for (j = 0; j < MAP_SIZE_Y; j++) {
cellChar = ' ';
switch (map[i][j]) {
case EMPTY: cellChar = '.'; break;
case SHIP_PLAYER_C: cellChar = 'C'; break;
case SHIP_PLAYER_R: cellChar = 'R'; break;
case SHIP_PLAYER_D: cellChar = 'D'; break;
case SHIP_PLAYER_S: cellChar = 'S'; break;
case SHIP_AI_C: cellChar = '.'; break; // 隐藏敌方战舰
case SHIP_AI_R: cellChar = '.'; break;
case SHIP_AI_D: cellChar = '.'; break;
case SHIP_AI_S: cellChar = '.'; break;
case HIT: cellChar = 'X'; break;
case MISS: cellChar = 'O'; break;
default: cellChar = '.'; break;
}
// 绘制攻击光标
if (i == cursorX && j == cursorY) {
cellChar = '#'; // 攻击光标标记
}
// 绘制直线攻击的终点
else if (type == ATTACK_LINE && i == lineEnd.x && j == lineEnd.y) {
cellChar = '@'; // 直线终点标记
}
// 绘制攻击范围预览
else if (showRange) {
// 航母3×3范围
if (type == ATTACK_CENTER) {
int dx = abs(i - cursorX);
int dy = abs(j - cursorY);
if (dx <= 1 && dy <= 1) {
cellChar = '*';
}
}
// 直线攻击范围
else if (type == ATTACK_LINE) {
int minX = (cursorX < lineEnd.x) ? cursorX : lineEnd.x;
int maxX = (cursorX > lineEnd.x) ? cursorX : lineEnd.x;
int minY = (cursorY < lineEnd.y) ? cursorY : lineEnd.y;
int maxY = (cursorY > lineEnd.y) ? cursorY : lineEnd.y;
if (i >= minX && i <= maxX && j >= minY && j <= maxY) {
cellChar = '*';
}
}
}
// 绘制半区分割线
if (j == PLAYER_AREA_Y - 1) {
printf(" %c|", cellChar);
} else {
printf(" %c", cellChar);
}
}
}
}
// 可视化选择攻击坐标(核心新增函数)
int selectAttackPosition(int attackType, Point* pos1, Point* pos2) {
int cursorX = 0, cursorY = 0;
int key;
int step = 0; // 0=选第一个点,1=选第二个点(仅直线攻击)
int showRange = 0;
Point lineEnd = {0, 0};
hideCursor();
pos1->x = pos1->y = pos2->x = pos2->y = -1;
while (1) {
// 绘制攻击选择界面
drawAttackCursor(cursorX, cursorY, attackType, lineEnd, showRange);
// 检测按键
key = _getch();
if (key == 27) { // ESC取消
showCursor();
return 0;
}
else if (key == 224) { // 方向键前缀
key = _getch();
switch (key) {
case 72: // 上
if (cursorX > 0) cursorX--;
break;
case 80: // 下
if (cursorX < MAP_SIZE_X - 1) cursorX++;
break;
case 75: // 左
if (cursorY > 0) cursorY--;
break;
case 77: // 右
if (cursorY < MAP_SIZE_Y - 1) cursorY++;
break;
}
}
else if (key == 32) { // 空格键切换范围预览
showRange = !showRange;
}
else if (key == 13) { // 回车键确认
if (attackType == ATTACK_SINGLE || attackType == ATTACK_CENTER) {
// 单点/航母中心:直接确认
pos1->x = cursorX;
pos1->y = cursorY;
showCursor();
return 1;
}
else if (attackType == ATTACK_LINE) {
if (step == 0) {
// 第一步:选直线起点
pos1->x = cursorX;
pos1->y = cursorY;
lineEnd.x = cursorX;
lineEnd.y = cursorY;
step = 1;
gotoXY(MAP_LEFT, 3);
printf("已选起点(%d,%d),请选择直线终点...", cursorX, cursorY);
}
else {
// 第二步:选直线终点
pos2->x = cursorX;
pos2->y = cursorY;
showCursor();
return 1;
}
}
}
}
}
// 绘制20×20地图(隐藏敌方战舰)
void drawMap(int isPlayerView) {
int i, j; // 老编译器要求变量在开头定义
char cellChar;
gotoXY(MAP_LEFT, 0);
printf("========== 20×20 海战地图(半区分割线:y=10)==========");
gotoXY(MAP_LEFT, 1);
printf("油费:%d/%d | 你的海域:y<10 | 敌方海域:y>=10", isPlayerView ? playerOil : aiOil, MAX_OIL);
gotoXY(MAP_LEFT, 2);
printf("标识:C=航母 R=巡洋 D=驱逐 S=潜艇 | X=命中(扣血) O=未命中 | *=攻击范围");
// 绘制列号
gotoXY(MAP_LEFT, 4);
printf(" ");
for (j = 0; j < MAP_SIZE_Y; j++) {
printf("%2d", j % 10);
}
// 绘制地图
for (i = 0; i < MAP_SIZE_X; i++) {
gotoXY(MAP_LEFT, 5 + i);
printf("%2d ", i % 10);
for (j = 0; j < MAP_SIZE_Y; j++) {
cellChar = ' ';
switch (map[i][j]) {
case EMPTY: cellChar = '.'; break;
case SHIP_PLAYER_C: cellChar = 'C'; break;
case SHIP_PLAYER_R: cellChar = 'R'; break;
case SHIP_PLAYER_D: cellChar = 'D'; break;
case SHIP_PLAYER_S: cellChar = 'S'; break;
case SHIP_AI_C: cellChar = '.'; break; // 隐藏敌方战舰
case SHIP_AI_R: cellChar = '.'; break;
case SHIP_AI_D: cellChar = '.'; break;
case SHIP_AI_S: cellChar = '.'; break;
case HIT: cellChar = 'X'; break; // 命中(无论是否击沉)
case MISS: cellChar = 'O'; break; // 未命中(无战舰)
case ATTACK_RANGE: cellChar = '*'; break;
}
// 绘制半区分割线
if (j == PLAYER_AREA_Y - 1) {
printf(" %c|", cellChar);
} else {
printf(" %c", cellChar);
}
}
}
}
// 绘制战舰状态面板
void drawShipStatus(int isPlayerTurn) {
int k, row; // 老编译器要求变量在开头定义
Ship* ships;
int shipCount;
gotoXY(STATUS_LEFT, 0);
printf("========== 战舰状态面板 ==========");
gotoXY(STATUS_LEFT, 1);
printf(isPlayerTurn ? "【你的战舰】" : "【人机战舰】");
gotoXY(STATUS_LEFT, 3);
printf("ID | 类型 | 血量 | 位置 | 油费");
gotoXY(STATUS_LEFT, 4);
printf("--------------------------------");
ships = isPlayerTurn ? playerShips : aiShips;
shipCount = isPlayerTurn ? playerShipCount : aiShipCount;
row = 5;
for (k = 0; k < shipCount; k++) {
if (ships[k].isAlive) {
gotoXY(STATUS_LEFT, row++);
printf("%2d | %5s | %3d | (%d,%d) | %3d",
ships[k].id, ships[k].name, ships[k].health,
ships[k].x, ships[k].y, ships[k].oilCost);
}
if (row >= 25) break;
}
gotoXY(STATUS_LEFT, 27);
printf("操作说明:");
gotoXY(STATUS_LEFT, 28);
printf("3=攻击 4=结束"); // 移除转向/移动操作说明
}
// 清屏并绘制完整界面
void drawFullScreen(int isPlayerTurn) {
system("cls");
drawMap(isPlayerTurn);
drawShipStatus(isPlayerTurn);
gotoXY(MAP_LEFT, 30);
printf("========================================");
gotoXY(MAP_LEFT, 31);
}
// 显示攻击范围预览(兼容新的坐标选择)
void showAttackRange(Ship* ship, int targetX, int targetY, int startX, int startY, int endX, int endY) {
int i, j, minX, maxX, minY, maxY; // 老编译器要求变量在开头定义
int originalMap[MAP_SIZE_X][MAP_SIZE_Y];
// 备份原地图
for (i = 0; i < MAP_SIZE_X; i++) {
for (j = 0; j < MAP_SIZE_Y; j++) {
originalMap[i][j] = map[i][j];
}
}
if (ship->type == CARRIER) {
// 航母:3×3范围(中心坐标)
int dmgStartX = targetX - (ship->dmgHeight / 2);
int dmgStartY = targetY - (ship->dmgWidth / 2);
for (i = 0; i < ship->dmgHeight; i++) {
for (j = 0; j < ship->dmgWidth; j++) {
int cx = dmgStartX + i;
int cy = dmgStartY + j;
if (isInMap(cx, cy) && map[cx][cy] == EMPTY) {
map[cx][cy] = ATTACK_RANGE;
}
}
}
} else if (ship->type == CRUISER || ship->type == DESTROYER) {
// 巡洋舰/驱逐舰:首尾坐标直线范围
minX = (startX < endX) ? startX : endX;
maxX = (startX > endX) ? startX : endX;
minY = (startY < endY) ? startY : endY;
maxY = (startY > endY) ? startY : endY;
for (i = minX; i <= maxX; i++) {
for (j = minY; j <= maxY; j++) {
if (isInMap(i, j) && map[i][j] == EMPTY) {
map[i][j] = ATTACK_RANGE;
}
}
}
} else {
// 潜艇:单点
if (isInMap(targetX, targetY) && map[targetX][targetY] == EMPTY) {
map[targetX][targetY] = ATTACK_RANGE;
}
}
drawMap(1);
gotoXY(MAP_LEFT, 31);
printf("攻击范围预览(*),按任意键确认攻击...");
getch();
// 恢复地图
for (i = 0; i < MAP_SIZE_X; i++) {
for (j = 0; j < MAP_SIZE_Y; j++) {
map[i][j] = originalMap[i][j];
}
}
drawMap(1);
}
// 查找战舰(通过ID)
Ship* findShip(int shipId, int isPlayer) {
int k; // 老编译器要求变量在开头定义
Ship* ships;
int shipCount;
ships = isPlayer ? playerShips : aiShips;
shipCount = isPlayer ? playerShipCount : aiShipCount;
for (k = 0; k < shipCount; k++) {
if (ships[k].id == shipId && ships[k].isAlive) {
return &ships[k];
}
}
return NULL;
}
// 攻击逻辑(核心修复:命中=扣血,未命中=无战舰)
int attackPosition(Ship* attacker) {
int targetX, targetY, startX, startY, endX, endY; // 老编译器要求变量在开头定义
int i, j, k, m, hasHit, hitShip;
int dmgStartX, dmgStartY, minX, maxX, minY, maxY;
Point cells[20];
int cellCount;
Point pos1, pos2;
if (playerOil < attacker->oilCost) {
gotoXY(MAP_LEFT, 31);
printf("油费不足!需要%d点,当前剩余%d点", attacker->oilCost, playerOil);
getch();
return 0;
}
// 可视化选择攻击坐标
int attackType = 0;
if (attacker->type == CARRIER) {
attackType = ATTACK_CENTER;
gotoXY(MAP_LEFT, 31);
printf("航母攻击(3×3范围)- 请用方向键选择中心坐标(ESC取消)...");
getch();
} else if (attacker->type == CRUISER || attacker->type == DESTROYER) {
attackType = ATTACK_LINE;
gotoXY(MAP_LEFT, 31);
printf("巡洋舰/驱逐舰攻击(直线)- 请用方向键选择首尾坐标(ESC取消)...");
getch();
} else {
attackType = ATTACK_SINGLE;
gotoXY(MAP_LEFT, 31);
printf("潜艇攻击(单点)- 请用方向键选择目标坐标(ESC取消)...");
getch();
}
// 调用可视化坐标选择
if (!selectAttackPosition(attackType, &pos1, &pos2)) {
gotoXY(MAP_LEFT, 31);
printf("攻击取消!按任意键继续...");
getch();
return 0;
}
// 解析选择的坐标
if (attackType == ATTACK_SINGLE || attackType == ATTACK_CENTER) {
targetX = pos1.x;
targetY = pos1.y;
startX = startY = endX = endY = 0;
} else {
startX = pos1.x;
startY = pos1.y;
endX = pos2.x;
endY = pos2.y;
targetX = targetY = 0;
}
// 显示攻击范围预览
showAttackRange(attacker, targetX, targetY, startX, startY, endX, endY);
hasHit = 0;
// 处理航母攻击
if (attacker->type == CARRIER) {
dmgStartX = targetX - (attacker->dmgHeight / 2);
dmgStartY = targetY - (attacker->dmgWidth / 2);
for (i = 0; i < attacker->dmgHeight; i++) {
for (j = 0; j < attacker->dmgWidth; j++) {
int cx = dmgStartX + i;
int cy = dmgStartY + j;
if (!isInMap(cx, cy)) continue;
hitShip = 0;
// 检查是否命中敌方战舰(无论是否存活)
for (k = 0; k < aiShipCount; k++) {
getShipCells(&aiShips[k], cells, &cellCount);
for (m = 0; m < cellCount; m++) {
if (cells[m].x == cx && cells[m].y == cy) {
// 命中:扣血,标记HIT(即使战舰已沉没)
if (aiShips[k].isAlive) {
aiShips[k].health -= 1;
if (aiShips[k].health <= 0) {
aiShips[k].isAlive = 0;
clearShipFromMap(&aiShips[k]);
gotoXY(MAP_LEFT, 32);
printf("击沉人机%s!", aiShips[k].name);
}
}
// 仅在未标记HIT/MISS时标记
if (map[cx][cy] != HIT && map[cx][cy] != MISS) {
map[cx][cy] = HIT;
}
hitShip = 1;
hasHit = 1;
break;
}
}
if (hitShip) break;
}
// 未命中:无战舰,标记MISS(仅空位置)
if (!hitShip && map[cx][cy] == EMPTY) {
map[cx][cy] = MISS;
}
}
}
}
// 处理巡洋舰/驱逐舰攻击
else if (attacker->type == CRUISER || attacker->type == DESTROYER) {
minX = (startX < endX) ? startX : endX;
maxX = (startX > endX) ? startX : endX;
minY = (startY < endY) ? startY : endY;
maxY = (startY > endY) ? startY : endY;
for (i = minX; i <= maxX; i++) {
for (j = minY; j <= maxY; j++) {
if (!isInMap(i, j)) continue;
hitShip = 0;
// 检查是否命中敌方战舰
for (k = 0; k < aiShipCount; k++) {
getShipCells(&aiShips[k], cells, &cellCount);
for (m = 0; m < cellCount; m++) {
if (cells[m].x == i && cells[m].y == j) {
if (aiShips[k].isAlive) {
aiShips[k].health -= 1;
if (aiShips[k].health <= 0) {
aiShips[k].isAlive = 0;
clearShipFromMap(&aiShips[k]);
gotoXY(MAP_LEFT, 32);
printf("击沉人机%s!", aiShips[k].name);
}
}
if (map[i][j] != HIT && map[i][j] != MISS) {
map[i][j] = HIT;
}
hitShip = 1;
hasHit = 1;
break;
}
}
if (hitShip) break;
}
if (!hitShip && map[i][j] == EMPTY) {
map[i][j] = MISS;
}
}
}
}
// 处理潜艇攻击(核心修复:单点命中逻辑)
else if (attacker->type == SUBMARINE) {
int cx = targetX;
int cy = targetY;
if (isInMap(cx, cy)) {
hitShip = 0;
// 检查是否命中敌方战舰
for (k = 0; k < aiShipCount; k++) {
getShipCells(&aiShips[k], cells, &cellCount);
for (m = 0; m < cellCount; m++) {
if (cells[m].x == cx && cells[m].y == cy) {
// 命中:扣血(即使战舰已沉没)
if (aiShips[k].isAlive) {
aiShips[k].health -= 1;
if (aiShips[k].health <= 0) {
aiShips[k].isAlive = 0;
clearShipFromMap(&aiShips[k]);
gotoXY(MAP_LEFT, 32);
printf("击沉人机%s!", aiShips[k].name);
}
}
// 标记HIT(仅未标记时)
if (map[cx][cy] != HIT && map[cx][cy] != MISS) {
map[cx][cy] = HIT;
}
hitShip = 1;
hasHit = 1;
break;
}
}
if (hitShip) break;
}
// 未命中:无战舰,标记MISS
if (!hitShip && map[cx][cy] == EMPTY) {
map[cx][cy] = MISS;
}
}
}
playerOil -= attacker->oilCost;
gotoXY(MAP_LEFT, 33);
printf("%s 剩余油费:%d", hasHit ? "攻击命中(扣血)!" : "攻击未命中(无战舰)!", playerOil);
getch();
return 1;
}
// 检查碰撞
void checkCollision() {
int p, a, pc, ac, collision; // 老编译器要求变量在开头定义
Point pCells[20], aiCells[20];
int pCellCount, aiCellCount;
for (p = 0; p < playerShipCount; p++) {
if (!playerShips[p].isAlive) continue;
getShipCells(&playerShips[p], pCells, &pCellCount);
for (a = 0; a < aiShipCount; a++) {
if (!aiShips[a].isAlive) continue;
getShipCells(&aiShips[a], aiCells, &aiCellCount);
collision = 0;
for (pc = 0; pc < pCellCount; pc++) {
for (ac = 0; ac < aiCellCount; ac++) {
if (pCells[pc].x == aiCells[ac].x && pCells[pc].y == aiCells[ac].y) {
collision = 1;
break;
}
}
if (collision) break;
}
if (collision) {
gotoXY(MAP_LEFT, 31);
printf("碰撞!你的%s与人机%s同归于尽!按任意键继续...", playerShips[p].name, aiShips[a].name);
playerShips[p].isAlive = 0;
aiShips[a].isAlive = 0;
clearShipFromMap(&playerShips[p]);
clearShipFromMap(&aiShips[a]);
// 碰撞位置标记为HIT
for (pc = 0; pc < pCellCount; pc++) {
int x = pCells[pc].x;
int y = pCells[pc].y;
if (isInMap(x, y) && map[x][y] != HIT && map[x][y] != MISS) {
map[x][y] = HIT;
}
}
for (ac = 0; ac < aiCellCount; ac++) {
int x = aiCells[ac].x;
int y = aiCells[ac].y;
if (isInMap(x, y) && map[x][y] != HIT && map[x][y] != MISS) {
map[x][y] = HIT;
}
}
getch();
drawFullScreen(1);
}
}
}
}
// 检查游戏结束
int isGameOver() {
int k, playerAlive, aiAlive; // 老编译器要求变量在开头定义
playerAlive = 0;
for (k = 0; k < playerShipCount; k++) {
if (playerShips[k].isAlive) {
playerAlive = 1;
break;
}
}
aiAlive = 0;
for (k = 0; k < aiShipCount; k++) {
if (aiShips[k].isAlive) {
aiAlive = 1;
break;
}
}
if (!playerAlive) {
drawFullScreen(1);
gotoXY(MAP_LEFT, 31);
printf("\n===== 游戏结束 =====\n");
printf("你的所有战舰被击沉,人机获胜!\n");
return 1;
}
if (!aiAlive) {
drawFullScreen(1);
gotoXY(MAP_LEFT, 31);
printf("\n===== 游戏结束 =====\n");
printf("恭喜!你击沉了人机所有战舰,获得胜利!\n");
return 1;
}
return 0;
}
// AI攻击逻辑(核心修复:命中=扣血,未命中=无战舰)
void aiAttack(Ship* aiShip) {
int rx, ry, i, j, k, m, hasHit, hitShip;
int dmgStartX, dmgStartY, minX, maxX, minY, maxY;
Point cells[20];
int cellCount;
if (aiOil < aiShip->oilCost) {
gotoXY(MAP_LEFT, 31);
printf("人机油费不足,无法攻击!");
return;
}
gotoXY(MAP_LEFT, 31);
printf("人机使用%s攻击...", aiShip->name);
Sleep(500); // 模拟思考延迟
hasHit = 0;
// 航母:3×3范围攻击
if (aiShip->type == CARRIER) {
rx = rand() % MAP_SIZE_X;
ry = rand() % PLAYER_AREA_Y;
dmgStartX = rx - (aiShip->dmgHeight / 2);
dmgStartY = ry - (aiShip->dmgWidth / 2);
gotoXY(MAP_LEFT, 32);
printf("航母攻击中心坐标(%d,%d),3×3范围...", rx, ry);
Sleep(500);
for (i = 0; i < aiShip->dmgHeight; i++) {
for (j = 0; j < aiShip->dmgWidth; j++) {
int cx = dmgStartX + i;
int cy = dmgStartY + j;
if (!isInMap(cx, cy)) continue;
hitShip = 0;
// 检查是否命中玩家战舰
for (k = 0; k < playerShipCount; k++) {
getShipCells(&playerShips[k], cells, &cellCount);
for (m = 0; m < cellCount; m++) {
if (cells[m].x == cx && cells[m].y == cy) {
// 命中:扣血(即使战舰已沉没)
if (playerShips[k].isAlive) {
playerShips[k].health -= 1;
if (playerShips[k].health <= 0) {
playerShips[k].isAlive = 0;
clearShipFromMap(&playerShips[k]);
gotoXY(MAP_LEFT, 33);
printf("你的%s被击沉!", playerShips[k].name);
}
}
// 标记HIT(仅未标记时)
if (map[cx][cy] != HIT && map[cx][cy] != MISS) {
map[cx][cy] = HIT;
}
hitShip = 1;
hasHit = 1;
break;
}
}
if (hitShip) break;
}
// 未命中:无战舰,标记MISS
if (!hitShip && map[cx][cy] == EMPTY) {
map[cx][cy] = MISS;
}
}
}
}
// 巡洋舰/驱逐舰:直线攻击
else if (aiShip->type == CRUISER || aiShip->type == DESTROYER) {
int startX = rand() % MAP_SIZE_X;
int startY = rand() % PLAYER_AREA_Y;
int endX = startX + (rand() % 3 - 1); // 随机±1
int endY = startY + (rand() % 3 - 1);
// 确保在地图内
endX = (endX < 0) ? 0 : (endX >= MAP_SIZE_X ? MAP_SIZE_X-1 : endX);
endY = (endY < 0) ? 0 : (endY >= PLAYER_AREA_Y ? PLAYER_AREA_Y-1 : endY);
minX = (startX < endX) ? startX : endX;
maxX = (startX > endX) ? startX : endX;
minY = (startY < endY) ? startY : endY;
maxY = (startY > endY) ? startY : endY;
gotoXY(MAP_LEFT, 32);
printf("直线攻击(%d,%d)到(%d,%d)...", startX, startY, endX, endY);
Sleep(500);
for (i = minX; i <= maxX; i++) {
for (j = minY; j <= maxY; j++) {
if (!isInMap(i, j)) continue;
hitShip = 0;
// 检查是否命中玩家战舰
for (k = 0; k < playerShipCount; k++) {
getShipCells(&playerShips[k], cells, &cellCount);
for (m = 0; m < cellCount; m++) {
if (cells[m].x == i && cells[m].y == j) {
if (playerShips[k].isAlive) {
playerShips[k].health -= 1;
if (playerShips[k].health <= 0) {
playerShips[k].isAlive = 0;
clearShipFromMap(&playerShips[k]);
gotoXY(MAP_LEFT, 33);
printf("你的%s被击沉!", playerShips[k].name);
}
}
if (map[i][j] != HIT && map[i][j] != MISS) {
map[i][j] = HIT;
}
hitShip = 1;
hasHit = 1;
break;
}
}
if (hitShip) break;
}
if (!hitShip && map[i][j] == EMPTY) {
map[i][j] = MISS;
}
}
}
}
// 潜艇:单点攻击(核心修复)
else if (aiShip->type == SUBMARINE) {
rx = rand() % MAP_SIZE_X;
ry = rand() % PLAYER_AREA_Y;
gotoXY(MAP_LEFT, 32);
printf("潜艇单点攻击坐标(%d,%d)...", rx, ry);
Sleep(500);
if (isInMap(rx, ry)) {
hitShip = 0;
// 检查是否命中玩家战舰
for (k = 0; k < playerShipCount; k++) {
getShipCells(&playerShips[k], cells, &cellCount);
for (m = 0; m < cellCount; m++) {
if (cells[m].x == rx && cells[m].y == ry) {
// 命中:扣血(即使战舰已沉没)
if (playerShips[k].isAlive) {
playerShips[k].health -= 1;
if (playerShips[k].health <= 0) {
playerShips[k].isAlive = 0;
clearShipFromMap(&playerShips[k]);
gotoXY(MAP_LEFT, 33);
printf("你的%s被击沉!", playerShips[k].name);
}
}
// 标记HIT(仅未标记时)
if (map[rx][ry] != HIT && map[rx][ry] != MISS) {
map[rx][ry] = HIT;
}
hitShip = 1;
hasHit = 1;
break;
}
}
if (hitShip) break;
}
// 未命中:无战舰,标记MISS
if (!hitShip && map[rx][ry] == EMPTY) {
map[rx][ry] = MISS;
}
}
}
aiOil -= aiShip->oilCost;
gotoXY(MAP_LEFT, 34);
printf("%s 剩余油费:%d", hasHit ? "攻击命中(扣血)!" : "攻击未命中(无战舰)!", aiOil);
Sleep(1000);
}
// 人机回合(修复油费耗尽+潜艇攻击+命中逻辑)
void aiTurn() {
int k, aliveCount; // 老编译器要求变量在开头定义
Ship* aiShip;
Ship* aliveShips[MAX_SHIPS];
drawFullScreen(0);
gotoXY(MAP_LEFT, 31);
printf("===== 人机回合 =====");
getch();
// AI回合内循环攻击,直到油费耗尽或无存活战舰
while (aiOil > 0) {
aliveCount = 0;
for (k = 0; k < aiShipCount; k++) {
if (aiShips[k].isAlive && aiShips[k].oilCost <= aiOil) {
aliveShips[aliveCount++] = &aiShips[k];
}
}
if (aliveCount == 0) break;
// 随机选一艘可攻击的战舰
aiShip = aliveShips[rand() % aliveCount];
aiAttack(aiShip);
checkCollision();
if (isGameOver()) return;
// 油费不足时退出循环
if (aiOil <= 0) {
gotoXY(MAP_LEFT, 35);
printf("人机油费耗尽!");
Sleep(1000);
break;
}
}
gotoXY(MAP_LEFT, 35);
printf("人机回合结束,按任意键进入你的回合...");
getch();
}
// 玩家回合(仅保留攻击和结束选项)
void playerTurn() {
int choice, shipId; // 老编译器要求变量在开头定义
Ship* ship;
playerOil = MAX_OIL;
while (1) {
drawFullScreen(1);
// 油费耗尽提示
if (playerOil <= 0) {
gotoXY(MAP_LEFT, 31);
printf("油费已耗尽!按任意键结束回合...");
getch();
break;
}
gotoXY(MAP_LEFT, 31);
printf("请选择操作(3=攻击 4=结束):"); // 移除转向/移动选项
scanf("%d", &choice);
if (choice == 4) break;
// 仅处理攻击操作
if (choice != 3) {
gotoXY(MAP_LEFT, 33);
printf("无效选择!按任意键继续...");
getch();
continue;
}
gotoXY(MAP_LEFT, 32);
printf("输入要操作的战舰ID:");
scanf("%d", &shipId);
ship = findShip(shipId, 1);
if (ship == NULL) {
gotoXY(MAP_LEFT, 33);
printf("无效的战舰ID或战舰已沉没!按任意键继续...");
getch();
continue;
}
// 执行攻击
attackPosition(ship);
drawFullScreen(1);
checkCollision();
if (isGameOver()) exit(0);
}
gotoXY(MAP_LEFT, 31);
printf("你的回合结束!按任意键进入人机回合...");
getch();
}
// 初始化游戏
void initGame() {
int i, j; // 老编译器要求变量在开头定义
srand((unsigned int)time(NULL)); // 显式转换为unsigned int
// 初始化空地图
for (i = 0; i < MAP_SIZE_X; i++) {
for (j = 0; j < MAP_SIZE_Y; j++) {
map[i][j] = EMPTY;
}
}
// 初始化油费(仅一次)
playerOil = MAX_OIL;
aiOil = MAX_OIL;
// 手动部署玩家战舰(可视化)
manualDeployPlayerShips();
// 自动部署AI战舰
deployAIShips();
drawFullScreen(1);
gotoXY(MAP_LEFT, 31);
printf("===== 战舰部署完成(半区隔离)=====\n");
printf("游戏规则:\n");
printf("1. 每回合油费10点,攻击消耗对应战舰油费\n");
printf("2. 命中(X):攻击到敌方战舰(扣血,无论是否击沉)\n");
printf("3. 未命中(O):攻击位置无敌方战舰\n");
printf("4. 击沉战舰后,该位置仍保留X标记\n");
printf("5. 攻击时用方向键选择坐标,空格预览范围,回车确认,ESC取消\n");
printf("按任意键开始游戏...");
getch();
}
// 主函数
int main() {
// 兼容老编译器的控制台设置
system("chcp 936 > nul");
system("mode con cols=120 lines=40"); // 扩大控制台尺寸适配20×20地图
initGame();
while (1) {
playerTurn();
if (isGameOver()) break;
aiTurn();
if (isGameOver()) break;
}
gotoXY(0, 38);
printf("\n按任意键退出...\n");
getch();
return 0;
}