战争雷霆
2025-08-04 11:00:54
发布于:江苏
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <random>
#include <memory>
#include <algorithm>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
// 颜色定义
enum Color {
BLACK = 0,
BLUE = 1,
GREEN = 2,
CYAN = 3,
RED = 4,
MAGENTA = 5,
BROWN = 6,
LIGHTGRAY = 7,
DARKGRAY = 8,
LIGHTBLUE = 9,
LIGHTGREEN = 10,
LIGHTCYAN = 11,
LIGHTRED = 12,
LIGHTMAGENTA = 13,
YELLOW = 14,
WHITE = 15
};
// 设置控制台文本颜色
void setColor(int textColor, int bgColor) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (bgColor << 4) | textColor);
}
// 移动光标到指定位置
void gotoxy(int x, int y) {
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
// 隐藏光标
void hideCursor() {
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
// 前置声明
class Map;
// 战斗单位基类
class Unit {
protected:
string name;
int health;
int maxHealth;
int attack;
int defense;
int speed;
int x, y; // 位置坐标
char symbol; // 控制台显示符号
Color color; // 显示颜色
bool isPlayerUnit; // 是否为玩家单位
public:
Unit(string n, int h, int a, int d, int s, char sym, Color c, bool player)
: name(n), health(h), maxHealth(h), attack(a), defense(d), speed(s),
symbol(sym), color(c), isPlayerUnit(player), x(0), y(0) {
}
virtual ~Unit() = default;
// 获取名称
string getName() const { return name; }
// 获取生命值
int getHealth() const { return health; }
int getMaxHealth() const { return maxHealth; }
// 受伤
void takeDamage(int damage) {
int actualDamage = max(1, damage - defense / 3); // 防御减轻伤害
health = max(0, health - actualDamage);
}
// 攻击目标
virtual int attackTarget(Unit& target) {
// 计算命中率 (70-90%)
int hitChance = 70 + rand() % 21;
if (rand() % 100 < hitChance) {
target.takeDamage(attack);
return attack; // 命中
}
return 0; // 未命中
}
// 移动
virtual bool move(int newX, int newY, const Map& map);
// 获取位置
int getX() const { return x; }
int getY() const { return y; }
// 设置位置
void setPosition(int x, int y) {
this->x = x;
this->y = y;
}
// 检查是否存活
bool isAlive() const { return health > 0; }
// 显示信息
virtual void displayInfo() const {
setColor(color, BLACK);
cout << name << " (" << symbol << "):" << endl;
setColor(WHITE, BLACK);
cout << " 生命值: " << health << "/" << maxHealth << endl;
cout << " 攻击力: " << attack << endl;
cout << " 防御力: " << defense << endl;
cout << " 速度: " << speed << endl;
cout << " 位置: (" << x << ", " << y << ")" << endl;
}
// 显示在地图上
void displayOnMap() const {
setColor(color, BLACK);
cout << symbol;
setColor(WHITE, BLACK);
}
// 是否为玩家单位
bool isPlayer() const { return isPlayerUnit; }
// 升级
virtual void upgrade() {
maxHealth += 10;
health = maxHealth;
attack += 3;
defense += 2;
speed += 1;
}
// 获取符号
char getSymbol() const { return symbol; }
// 获取速度
int getSpeed() const { return speed; }
};
// 地图类
class Map {
private:
int width, height;
vector<vector<char>> terrain; // 地形数据
vector<vector<bool>> passable; // 地形是否可通过
public:
Map(int w, int h) : width(w), height(h) {
// 初始化地图
terrain.resize(height, vector<char>(width, '.'));
passable.resize(height, vector<bool>(width, true));
// 随机生成一些地形
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = rand() % 100;
if (r < 10) { // 山脉 (10%)
terrain[y][x] = '^';
passable[y][x] = false;
}
else if (r < 20) { // 森林 (10%)
terrain[y][x] = '*';
passable[y][x] = true;
}
else if (r < 25) { // 水域 (5%)
terrain[y][x] = '~';
passable[y][x] = false; // 大多数单位不能通过水域
}
}
}
}
// 检查位置是否在地图范围内
bool isWithinBounds(int x, int y) const {
return x >= 0 && x < width && y >= 0 && y < height;
}
// 检查位置是否可通过
bool isPassable(int x, int y) const {
return isWithinBounds(x, y) && passable[y][x];
}
// 特殊检查:是否为水域(供船只使用)
bool isWater(int x, int y) const {
return isWithinBounds(x, y) && terrain[y][x] == '~';
}
// 显示地图
void display(const vector<shared_ptr<Unit>>& units) const {
// 绘制顶部边框
setColor(CYAN, BLACK);
cout << "+";
for (int x = 0; x < width; x++) cout << "-";
cout << "+" << endl;
// 绘制地图内容
for (int y = 0; y < height; y++) {
cout << "|"; // 左侧边框
for (int x = 0; x < width; x++) {
// 检查是否有单位在这个位置
bool unitFound = false;
for (const auto& unit : units) {
if (unit->getX() == x && unit->getY() == y && unit->isAlive()) {
unit->displayOnMap();
unitFound = true;
break;
}
}
// 如果没有单位,显示地形
if (!unitFound) {
setColor(CYAN, BLACK);
cout << terrain[y][x];
}
}
cout << "|" << endl; // 右侧边框
}
// 绘制底部边框
cout << "+";
for (int x = 0; x < width; x++) cout << "-";
cout << "+" << endl;
setColor(WHITE, BLACK);
}
int getWidth() const { return width; }
int getHeight() const { return height; }
// 获取地形
char getTerrain(int x, int y) const {
if (isWithinBounds(x, y)) {
return terrain[y][x];
}
return '\0'; // 无效位置
}
};
// 实现Unit类的move方法(需要Map类定义)
bool Unit::move(int newX, int newY, const Map& map) {
// 检查是否在地图范围内且可移动
if (map.isPassable(newX, newY)) {
x = newX;
y = newY;
return true;
}
return false;
}
// 坦克类
class Tank : public Unit {
private:
int armorPiercing; // 穿甲能力
public:
Tank(string n, bool player = false)
: Unit(n, 150, 40, 60, 2, 'T', player ? GREEN : RED, player),
armorPiercing(30) {
}
int attackTarget(Unit& target) override {
// 坦克对车辆有加成,对空中单位有惩罚
int effectiveAttack = attack;
// 判断目标类型(通过符号)
char targetSymbol = target.getSymbol();
if (targetSymbol == 'T' || targetSymbol == 'S') { // 其他坦克或船只
effectiveAttack += armorPiercing / 2;
}
else if (targetSymbol == 'P' || targetSymbol == 'H') { // 飞机或直升机
effectiveAttack /= 2;
}
// 计算命中率
int hitChance = 80 + rand() % 15;
if (rand() % 100 < hitChance) {
target.takeDamage(effectiveAttack);
return effectiveAttack;
}
return 0;
}
void displayInfo() const override {
Unit::displayInfo();
cout << " 穿甲能力: " << armorPiercing << endl;
}
void upgrade() override {
Unit::upgrade();
armorPiercing += 5;
}
};
// 飞机类
class Plane : public Unit {
private:
int altitude; // 高度
int bombingPower; // 轰炸能力
public:
Plane(string n, bool player = false)
: Unit(n, 80, 30, 20, 6, 'P', player ? LIGHTBLUE : LIGHTRED, player),
altitude(1000), bombingPower(50) {
}
bool move(int newX, int newY, const Map& map) override {
// 飞机可以飞越任何地形
if (map.isWithinBounds(newX, newY)) {
setPosition(newX, newY);
return true;
}
return false;
}
int attackTarget(Unit& target) override {
// 飞机对地面单位有加成,对空中单位正常
int effectiveAttack = attack;
char targetSymbol = target.getSymbol();
if (targetSymbol == 'T' || targetSymbol == 'S') { // 坦克或船只
effectiveAttack += bombingPower / 2;
}
// 计算命中率
int hitChance = 75 + rand() % 20;
if (rand() % 100 < hitChance) {
target.takeDamage(effectiveAttack);
return effectiveAttack;
}
return 0;
}
void displayInfo() const override {
Unit::displayInfo();
cout << " 高度: " << altitude << "m" << endl;
cout << " 轰炸能力: " << bombingPower << endl;
}
void upgrade() override {
Unit::upgrade();
bombingPower += 8;
altitude += 500;
}
};
// 直升机类
class Helicopter : public Unit {
private:
int rotorSpeed; // 旋翼速度
int missileCount; // 导弹数量
public:
Helicopter(string n, bool player = false)
: Unit(n, 100, 35, 30, 4, 'H', player ? YELLOW : LIGHTMAGENTA, player),
rotorSpeed(300), missileCount(4) {
}
bool move(int newX, int newY, const Map& map) override {
// 直升机可以飞越大部分地形,但不能飞越山脉
if (map.isWithinBounds(newX, newY) && map.getTerrain(newX, newY) != '^') {
setPosition(newX, newY);
return true;
}
return false;
}
int attackTarget(Unit& target) override {
// 直升机对坦克有加成
int effectiveAttack = attack;
char targetSymbol = target.getSymbol();
if (targetSymbol == 'T') { // 坦克
effectiveAttack += 15;
if (missileCount > 0) {
effectiveAttack += 25;
missileCount--;
}
}
// 计算命中率
int hitChance = 85 + rand() % 10;
if (rand() % 100 < hitChance) {
target.takeDamage(effectiveAttack);
return effectiveAttack;
}
return 0;
}
void displayInfo() const override {
Unit::displayInfo();
cout << " 旋翼速度: " << rotorSpeed << "rpm" << endl;
cout << " 剩余导弹: " << missileCount << endl;
}
void upgrade() override {
Unit::upgrade();
rotorSpeed += 50;
missileCount = 6; // 升级后补充并增加导弹数量
}
};
// 船只类
class Ship : public Unit {
private:
int displacement; // 排水量
int cannonRange; // 火炮射程
public:
Ship(string n, bool player = false)
: Unit(n, 200, 50, 70, 1, 'S', player ? LIGHTCYAN : BROWN, player),
displacement(5000), cannonRange(5) {
}
bool move(int newX, int newY, const Map& map) override {
// 船只只能在水上移动
if (map.isWater(newX, newY)) {
setPosition(newX, newY);
return true;
}
return false;
}
int attackTarget(Unit& target) override {
// 计算距离
int dx = abs(getX() - target.getX());
int dy = abs(getY() - target.getY());
int distance = dx + dy;
// 船只对其他船只和靠近的单位有加成
int effectiveAttack = attack;
char targetSymbol = target.getSymbol();
if (targetSymbol == 'S') { // 其他船只
effectiveAttack += 20;
}
// 距离越近命中率越高
int hitChance = max(30, 90 - distance * 10);
if (rand() % 100 < hitChance && distance <= cannonRange) {
target.takeDamage(effectiveAttack);
return effectiveAttack;
}
return 0;
}
void displayInfo() const override {
Unit::displayInfo();
cout << " 排水量: " << displacement << "吨" << endl;
cout << " 火炮射程: " << cannonRange << endl;
}
void upgrade() override {
Unit::upgrade();
displacement += 1000;
cannonRange += 1;
}
};
// 科技树节点类
class TechNode {
private:
string name;
string description;
bool researched;
vector<int> prerequisites; // 前置科技ID
string unitType; // 解锁的单位类型
public:
TechNode(string n, string desc, vector<int> pre, string unit)
: name(n), description(desc), prerequisites(pre), unitType(unit), researched(false) {
}
// 研究科技
bool research(const vector<TechNode>& techTree) {
// 检查是否已研究
if (researched) return false;
// 检查前置科技
for (int pre : prerequisites) {
if (pre >= 0 && pre < techTree.size() && !techTree[pre].isResearched()) {
return false;
}
}
researched = true;
return true;
}
// 获取信息
string getName() const { return name; }
string getDescription() const { return description; }
bool isResearched() const { return researched; }
string getUnitType() const { return unitType; }
const vector<int>& getPrerequisites() const { return prerequisites; }
};
// 科技树类
class TechTree {
private:
vector<TechNode> nodes;
public:
TechTree() {
// 初始化科技树节点
nodes.emplace_back(
"基础装甲技术",
"提高所有装甲单位的防御力",
vector<int>(),
"tank"
);
nodes.emplace_back(
"初级航空技术",
"解锁基础战斗机",
vector<int>(),
"plane"
);
nodes.emplace_back(
"舰船设计",
"解锁基础船只",
vector<int>(),
"ship"
);
nodes.emplace_back(
"高级装甲",
"大幅提高装甲单位防御力,解锁重型坦克",
vector<int>{0},
"tank"
);
nodes.emplace_back(
"喷气推进",
"提高飞机速度和攻击力,解锁喷气式战斗机",
vector<int>{1},
"plane"
);
nodes.emplace_back(
"直升机技术",
"解锁直升机单位",
vector<int>{1},
"helicopter"
);
nodes.emplace_back(
"先进舰炮",
"提高船只攻击力和射程,解锁巡洋舰",
vector<int>{2},
"ship"
);
}
// 显示科技树
void display() const {
cout << "\n===== 科技树 =====" << endl;
for (size_t i = 0; i < nodes.size(); i++) {
setColor(nodes[i].isResearched() ? GREEN : WHITE, BLACK);
cout << i + 1 << ". " << nodes[i].getName() << endl;
setColor(WHITE, BLACK);
cout << " " << nodes[i].getDescription() << endl;
if (!nodes[i].getPrerequisites().empty()) {
cout << " 前置科技: ";
for (int pre : nodes[i].getPrerequisites()) {
cout << nodes[pre].getName() << " ";
}
cout << endl;
}
cout << endl;
}
}
// 研究指定科技
bool researchTech(int index) {
if (index >= 0 && index < nodes.size()) {
return nodes[index].research(nodes);
}
return false;
}
// 检查是否已研究指定科技
bool isTechResearched(int index) const {
if (index >= 0 && index < nodes.size()) {
return nodes[index].isResearched();
}
return false;
}
// 检查是否可以建造某种单位
bool canBuildUnit(string unitType) const {
for (const auto& node : nodes) {
if (node.isResearched() && node.getUnitType() == unitType) {
return true;
}
}
return false;
}
int getTechCount() const { return nodes.size(); }
const TechNode& getTechNode(int index) const { return nodes[index]; }
};
// 游戏类
class Game {
private:
Map map;
TechTree techTree;
vector<shared_ptr<Unit>> units;
int playerMoney;
int turn;
bool gameOver;
public:
Game(int mapWidth, int mapHeight) : map(mapWidth, mapHeight),
playerMoney(500), turn(1), gameOver(false) {
// 初始化玩家单位
units.push_back(make_shared<Tank>("M4 谢尔曼", true));
units.back()->setPosition(5, 5);
// 初始化敌人单位
units.push_back(make_shared<Tank>("虎式坦克", false));
units.back()->setPosition(15, 15);
// 初始解锁基础科技
techTree.researchTech(0); // 基础装甲技术
techTree.researchTech(1); // 初级航空技术
}
// 运行游戏主循环
void run() {
hideCursor();
while (!gameOver) {
system("cls");
displayGameStatus();
// 玩家回合
playerTurn();
// 检查游戏是否结束
if (checkGameOver()) {
break;
}
// 敌人回合
enemyTurn();
// 检查游戏是否结束
if (checkGameOver()) {
break;
}
turn++;
playerMoney += 100; // 每回合增加资金
}
// 显示游戏结束信息
system("cls");
if (isPlayerVictorious()) {
setColor(GREEN, BLACK);
cout << "恭喜!你赢得了这场战斗!" << endl;
}
else {
setColor(RED, BLACK);
cout << "游戏结束,你被击败了!" << endl;
}
setColor(WHITE, BLACK);
cout << "按任意键退出..." << endl;
_getch();
}
// 显示游戏状态
void displayGameStatus() const {
setColor(YELLOW, BLACK);
cout << "===== 战争雷霆 - 回合 " << turn << " =====" << endl;
setColor(WHITE, BLACK);
cout << "资金: " << playerMoney << " 金币" << endl << endl;
// 显示地图
map.display(units);
cout << endl;
// 显示图例
setColor(GREEN, BLACK);
cout << "T: 你的坦克 ";
setColor(YELLOW, BLACK);
cout << "H: 你的直升机 ";
setColor(LIGHTBLUE, BLACK);
cout << "P: 你的飞机 ";
setColor(LIGHTCYAN, BLACK);
cout << "S: 你的船只 ";
setColor(RED, BLACK);
cout << "T: 敌人坦克 ";
setColor(LIGHTMAGENTA, BLACK);
cout << "H: 敌人直升机 ";
setColor(LIGHTRED, BLACK);
cout << "P: 敌人飞机 ";
setColor(BROWN, BLACK);
cout << "S: 敌人船只 ";
setColor(CYAN, BLACK);
cout << "^: 山脉 *: 森林 ~: 水域 .: 平原" << endl;
setColor(WHITE, BLACK);
}
// 玩家回合
void playerTurn() {
cout << "\n===== 你的回合 =====" << endl;
while (true) {
// 显示玩家单位
vector<shared_ptr<Unit>> playerUnits;
for (const auto& unit : units) {
if (unit->isPlayer() && unit->isAlive()) {
playerUnits.push_back(unit);
}
}
if (playerUnits.empty()) {
return; // 没有存活的玩家单位
}
// 显示菜单
cout << "\n请选择操作:" << endl;
cout << "1. 移动单位" << endl;
cout << "2. 攻击敌人" << endl;
cout << "3. 查看单位信息" << endl;
cout << "4. 研究科技" << endl;
cout << "5. 建造单位" << endl;
cout << "6. 结束回合" << endl;
cout << "选择: ";
char choice = _getch();
cout << choice << endl;
switch (choice) {
case '1':
moveUnit(playerUnits);
break;
case '2':
attackWithUnit(playerUnits);
break;
case '3':
viewUnitInfo(playerUnits);
break;
case '4':
researchTechMenu();
break;
case '5':
buildUnitMenu();
break;
case '6':
return; // 结束回合
default:
cout << "无效选择,请重试。" << endl;
}
system("cls");
displayGameStatus();
}
}
// 移动单位
void moveUnit(const vector<shared_ptr<Unit>>& playerUnits) {
cout << "\n选择要移动的单位:" << endl;
for (size_t i = 0; i < playerUnits.size(); i++) {
cout << i + 1 << ". " << playerUnits[i]->getName()
<< " (" << playerUnits[i]->getX() << "," << playerUnits[i]->getY() << ")" << endl;
}
cout << "选择: ";
char choice = _getch();
int index = choice - '1';
cout << choice << endl;
if (index >= 0 && index < playerUnits.size()) {
auto unit = playerUnits[index];
cout << "输入移动方向 (WASD): ";
char dir = _getch();
cout << dir << endl;
int newX = unit->getX();
int newY = unit->getY();
// 根据方向计算新位置
switch (toupper(dir)) {
case 'W': newY--; break;
case 'S': newY++; break;
case 'A': newX--; break;
case 'D': newX++; break;
default:
cout << "无效方向。" << endl;
return;
}
// 尝试移动
if (unit->move(newX, newY, map)) {
cout << unit->getName() << " 移动到了 (" << newX << "," << newY << ")" << endl;
}
else {
cout << "无法移动到该位置!" << endl;
}
}
else {
cout << "无效选择。" << endl;
}
cout << "按任意键继续..." << endl;
_getch();
}
// 攻击敌人
void attackWithUnit(const vector<shared_ptr<Unit>>& playerUnits) {
cout << "\n选择要攻击的单位:" << endl;
for (size_t i = 0; i < playerUnits.size(); i++) {
cout << i + 1 << ". " << playerUnits[i]->getName() << endl;
}
cout << "选择: ";
char choice = _getch();
int attackerIndex = choice - '1';
cout << choice << endl;
if (attackerIndex < 0 || attackerIndex >= playerUnits.size()) {
cout << "无效选择。" << endl;
_getch();
return;
}
auto attacker = playerUnits[attackerIndex];
// 找到可攻击的敌人
vector<shared_ptr<Unit>> enemies;
for (const auto& unit : units) {
if (!unit->isPlayer() && unit->isAlive()) {
// 简单的距离检查(相邻或1格距离)
int dx = abs(attacker->getX() - unit->getX());
int dy = abs(attacker->getY() - unit->getY());
if (dx + dy <= attacker->getSpeed() + 1) { // 速度决定攻击范围
enemies.push_back(unit);
}
}
}
if (enemies.empty()) {
cout << "没有可攻击的敌人在范围内!" << endl;
_getch();
return;
}
// 选择攻击目标
cout << "\n选择攻击目标:" << endl;
for (size_t i = 0; i < enemies.size(); i++) {
cout << i + 1 << ". " << enemies[i]->getName()
<< " (" << enemies[i]->getX() << "," << enemies[i]->getY() << ")" << endl;
}
cout << "选择: ";
choice = _getch();
int targetIndex = choice - '1';
cout << choice << endl;
if (targetIndex >= 0 && targetIndex < enemies.size()) {
auto target = enemies[targetIndex];
// 执行攻击
int damage = attacker->attackTarget(*target);
if (damage > 0) {
cout << attacker->getName() << " 攻击了 " << target->getName()
<< ",造成了 " << damage << " 点伤害!" << endl;
if (!target->isAlive()) {
cout << target->getName() << " 被摧毁了!" << endl;
playerMoney += 150; // 击毁奖励
}
}
else {
cout << attacker->getName() << " 的攻击未命中!" << endl;
}
}
else {
cout << "无效选择。" << endl;
}
cout << "按任意键继续..." << endl;
_getch();
}
// 查看单位信息
void viewUnitInfo(const vector<shared_ptr<Unit>>& playerUnits) {
cout << "\n选择要查看的单位:" << endl;
for (size_t i = 0; i < playerUnits.size(); i++) {
cout << i + 1 << ". " << playerUnits[i]->getName() << endl;
}
cout << "选择: ";
char choice = _getch();
int index = choice - '1';
cout << choice << endl;
if (index >= 0 && index < playerUnits.size()) {
cout << endl;
playerUnits[index]->displayInfo();
}
else {
cout << "无效选择。" << endl;
}
cout << "\n按任意键继续..." << endl;
_getch();
}
// 研究科技菜单
void researchTechMenu() {
techTree.display();
cout << "选择要研究的科技 (0 取消): ";
string input;
cin >> input;
if (input == "0") return;
try {
int index = stoi(input) - 1;
if (index >= 0 && index < techTree.getTechCount()) {
if (techTree.isTechResearched(index)) {
cout << "这项科技已经研究过了!" << endl;
}
else {
// 检查前置科技
bool canResearch = true;
for (int pre : techTree.getTechNode(index).getPrerequisites()) {
if (!techTree.isTechResearched(pre)) {
canResearch = false;
break;
}
}
if (!canResearch) {
cout << "需要先研究前置科技!" << endl;
}
else {
// 研究科技需要花费资金
int cost = (index + 1) * 100;
if (playerMoney >= cost) {
if (techTree.researchTech(index)) {
playerMoney -= cost;
cout << "成功研究了 " << techTree.getTechNode(index).getName() << "!" << endl;
}
else {
cout << "研究失败!" << endl;
}
}
else {
cout << "资金不足,无法研究这项科技!" << endl;
}
}
}
}
else {
cout << "无效选择。" << endl;
}
}
catch (...) {
cout << "无效输入。" << endl;
}
cout << "按任意键继续..." << endl;
_getch();
}
// 建造单位菜单
void buildUnitMenu() {
cout << "\n可建造的单位:" << endl;
vector<pair<string, int>> availableUnits;
if (techTree.canBuildUnit("tank")) {
availableUnits.emplace_back("坦克", 200);
}
if (techTree.canBuildUnit("plane")) {
availableUnits.emplace_back("飞机", 250);
}
if (techTree.canBuildUnit("helicopter")) {
availableUnits.emplace_back("直升机", 300);
}
if (techTree.canBuildUnit("ship")) {
availableUnits.emplace_back("船只", 400);
}
if (availableUnits.empty()) {
cout << "没有可建造的单位,请先研究相应科技!" << endl;
_getch();
return;
}
for (size_t i = 0; i < availableUnits.size(); i++) {
cout << i + 1 << ". " << availableUnits[i].first
<< " - " << availableUnits[i].second << " 金币" << endl;
}
cout << "0. 取消" << endl;
cout << "选择要建造的单位: ";
char choice = _getch();
int index = choice - '1';
cout << choice << endl;
if (index == -1) return; // 取消
if (index >= 0 && index < availableUnits.size()) {
string unitType = availableUnits[index].first;
int cost = availableUnits[index].second;
if (playerMoney >= cost) {
// 寻找一个合适的位置放置新单位(玩家初始位置附近)
int x = 5 + rand() % 3;
int y = 5 + rand() % 3;
// 确保位置可通过
while (!map.isPassable(x, y) || isUnitAtPosition(x, y)) {
x = 5 + rand() % 3;
y = 5 + rand() % 3;
}
// 创建新单位
shared_ptr<Unit> newUnit;
if (unitType == "坦克") {
newUnit = make_shared<Tank>("M4 谢尔曼 Mk.II", true);
}
else if (unitType == "飞机") {
newUnit = make_shared<Plane>("P-51 野马", true);
}
else if (unitType == "直升机") {
newUnit = make_shared<Helicopter>("UH-1 休伊", true);
}
else if (unitType == "船只") {
// 为船只找一个水域位置
for (y = 0; y < map.getHeight(); y++) {
for (x = 0; x < map.getWidth(); x++) {
if (map.isWater(x, y) && !isUnitAtPosition(x, y)) {
newUnit = make_shared<Ship>("驱逐舰", true);
goto foundPosition;
}
}
}
cout << "没有合适的位置建造船只!" << endl;
_getch();
return;
}
foundPosition:
newUnit->setPosition(x, y);
units.push_back(newUnit);
playerMoney -= cost;
cout << "成功建造了 " << unitType << "!" << endl;
}
else {
cout << "资金不足,无法建造 " << unitType << "!" << endl;
}
}
else {
cout << "无效选择。" << endl;
}
cout << "按任意键继续..." << endl;
_getch();
}
// 敌人回合
void enemyTurn() {
cout << "\n===== 敌人回合 =====" << endl;
cout << "敌人正在行动..." << endl;
Sleep(1000);
// 敌人单位列表
vector<shared_ptr<Unit>> enemyUnits;
for (const auto& unit : units) {
if (!unit->isPlayer() && unit->isAlive()) {
enemyUnits.push_back(unit);
}
}
if (enemyUnits.empty()) {
return;
}
// 每个敌人单位行动
for (const auto& enemy : enemyUnits) {
// 寻找最近的玩家单位
shared_ptr<Unit> target = nullptr;
int minDistance = 1000;
for (const auto& unit : units) {
if (unit->isPlayer() && unit->isAlive()) {
int dx = abs(enemy->getX() - unit->getX());
int dy = abs(enemy->getY() - unit->getY());
int distance = dx + dy;
if (distance < minDistance) {
minDistance = distance;
target = unit;
}
}
}
if (!target) continue;
// 如果在攻击范围内,尝试攻击
if (minDistance <= enemy->getSpeed() + 1) {
int damage = enemy->attackTarget(*target);
if (damage > 0) {
cout << enemy->getName() << " 攻击了 " << target->getName()
<< ",造成了 " << damage << " 点伤害!" << endl;
if (!target->isAlive()) {
cout << target->getName() << " 被摧毁了!" << endl;
}
}
else {
cout << enemy->getName() << " 的攻击未命中!" << endl;
}
Sleep(500);
}
else {
// 否则向目标移动
int newX = enemy->getX();
int newY = enemy->getY();
// 向目标方向移动
if (enemy->getX() < target->getX()) newX++;
else if (enemy->getX() > target->getX()) newX--;
if (enemy->getY() < target->getY()) newY++;
else if (enemy->getY() > target->getY()) newY--;
// 尝试移动
if (enemy->move(newX, newY, map)) {
cout << enemy->getName() << " 移动到了 (" << newX << "," << newY << ")" << endl;
}
Sleep(500);
}
}
// 有一定概率敌人会建造新单位
if (rand() % 100 < 30) { // 30% 概率
// 寻找合适的位置
int x = 15 + rand() % 5;
int y = 15 + rand() % 5;
while (!map.isPassable(x, y) || isUnitAtPosition(x, y)) {
x = 15 + rand() % 5;
y = 15 + rand() % 5;
}
// 随机选择单位类型
int unitType = rand() % 3;
shared_ptr<Unit> newUnit;
if (unitType == 0) {
newUnit = make_shared<Tank>("豹式坦克", false);
}
else if (unitType == 1) {
newUnit = make_shared<Plane>("BF-109", false);
}
else {
newUnit = make_shared<Ship>("U型潜艇", false);
// 为船只找水域
for (y = 0; y < map.getHeight(); y++) {
for (x = 0; x < map.getWidth(); x++) {
if (map.isWater(x, y) && !isUnitAtPosition(x, y)) {
goto shipPositionFound;
}
}
}
shipPositionFound:;
}
newUnit->setPosition(x, y);
units.push_back(newUnit);
cout << "敌人建造了新的 " << newUnit->getName() << "!" << endl;
}
cout << "\n敌人回合结束。按任意键继续..." << endl;
_getch();
}
// 检查指定位置是否有单位
bool isUnitAtPosition(int x, int y) const {
for (const auto& unit : units) {
if (unit->isAlive() && unit->getX() == x && unit->getY() == y) {
return true;
}
}
return false;
}
// 检查游戏是否结束
bool checkGameOver() {
// 检查玩家是否还有存活单位
bool playerHasUnits = false;
for (const auto& unit : units) {
if (unit->isPlayer() && unit->isAlive()) {
playerHasUnits = true;
break;
}
}
// 检查敌人是否还有存活单位
bool enemyHasUnits = false;
for (const auto& unit : units) {
if (!unit->isPlayer() && unit->isAlive()) {
enemyHasUnits = true;
break;
}
}
gameOver = !playerHasUnits || !enemyHasUnits;
return gameOver;
}
// 检查玩家是否胜利
bool isPlayerVictorious() const {
// 玩家胜利条件:所有敌人单位被摧毁
for (const auto& unit : units) {
if (!unit->isPlayer() && unit->isAlive()) {
return false;
}
}
return true;
}
};
// 主函数
int main() {
// 设置随机数种子
srand(time(NULL));
// 欢迎信息
system("cls");
setColor(YELLOW, BLACK);
cout << "=====================================" << endl;
cout << " 战争雷霆 控制台版 " << endl;
cout << "=====================================" << endl;
setColor(WHITE, BLACK);
cout << "欢迎来到战争雷霆控制台版!" << endl;
cout << "在这个游戏中,你将指挥各种作战单位与敌人战斗。" << endl;
cout << "通过研究科技,你可以解锁更强大的单位和能力。" << endl;
cout << "消灭所有敌人单位获得胜利!" << endl;
cout << "\n按任意键开始游戏..." << endl;
_getch();
// 创建并运行游戏
Game game(50, 50); // 50x50的地图
game.run();
return 0;
}
全部评论 8
?一个p社玩家路过:(
4天前 来自 浙江
0我们有救了
5天前 来自 上海
0wc是进攻D点
5天前 来自 上海
0d=====( ̄▽ ̄*)b
5天前 来自 新疆
0d=====( ̄▽ ̄*)b
1周前 来自 江苏
0666你也玩bvvd
1周前 来自 北京
0d=====( ̄▽ ̄*)b
1周前 来自 江苏
0顶!!!
1周前 来自 江苏
0
有帮助,赞一个