#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) {
}
};
// 地图类
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));
};
// 实现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) {
}
};
// 飞机类
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) {
}
};
// 直升机类
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) {
}
};
// 船只类
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) {
}
};
// 科技树节点类
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) {
}
};
// 科技树类
class TechTree {
private:
vector<TechNode> nodes;
public:
TechTree() {
// 初始化科技树节点
nodes.emplace_back(
"基础装甲技术",
"提高所有装甲单位的防御力",
vector<int>(),
"tank"
);
};
// 游戏类
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);
};
// 主函数
int main() {
// 设置随机数种子
srand(time(NULL));
}