Skip to content

Repository files navigation

BattleField

中文 | English

中文

一个用 C++ 编写的命令行回合制战棋游戏,灵感来自《Into the Breach》的战场和回合机制。

这是我在《程序设计原理与方法》课程期末大作业中完成的 Task4 项目整理版。游戏运行在 8 x 8 的战场上,玩家控制己方单位,敌方单位由 AI 控制。项目实现了地图加载、单位移动与攻击、回合控制、敌方 AI、地形效果和可达区域搜索等功能。

本项目是非官方的课程学习实现,不属于《Into the Breach》官方项目。

项目特色

  • 使用 C++ 面向对象方式组织战场、单位和地形模型
  • 使用 Grid<T> 模板类管理二维网格数据
  • 根据单位类型、地形类型和移动力计算可达区域
  • 支持玩家回合和敌方 AI 回合
  • 实现近战、远程和战斗机类型三种攻击范围
  • 实现击退、治疗、缴械、地形破坏和沼泽伤害等特殊效果
  • 通过标准输入输出运行,便于自动化测试和复现对局

游戏目标

玩家控制己方的人类单位,在 8 x 8 战场上通过移动和攻击消灭所有敌方单位。

  • 敌方单位全部被消灭时,游戏输出 Won
  • 己方单位全部被消灭而敌方仍存在时,游戏输出 Failed
  • 玩家可以在自己的回合中结束回合,由敌方 AI 控制敌方单位行动

单位

单位 符号 阵营 HP ATK 位置 移动力 攻击类型
士兵 S / s 己方 2 1 陆地 3 近战
坦克 T / t 己方 3 1 陆地 2 远程 + 击退
蜜蜂 B / b 敌方 3 2 空中 4 近战
战斗机 F 己方 2 2 空中 5 战斗机类型
刺蛇 H / h 敌方 1 1 陆地 3 近战 + 击退 + 缴械

己方单位使用大写符号,敌方单位使用小写符号。地图显示单位时会附带当前生命值,例如 S2T3

地形与移动代价

地形 地图符号 陆地单位 空中单位
平原 两个空格 1 1
山脉 /\ 100 100
海洋 ~~ 100 1
沼泽 \/ 1 100
其他单位占据的格子 - 100 100

移动力消耗为 100 的格子在正常移动范围内不可达。单位移动时会避开其他单位和无法通过的地形。

核心玩法

玩家回合

每个己方单位在一个回合内最多移动一次、攻击一次,两种行动没有固定先后顺序。单位仍有可执行行动时,地图上会用 + 标记该单位。

玩家可以选择:

  • Move:显示移动范围,选择可达目标格
  • Attack:显示攻击范围,选择攻击目标
  • Skip:取消当前单位的选择
  • End this turn:结束玩家回合,进入敌方 AI 回合

敌方 AI

敌方单位按照坐标从大到小的顺序行动。每个敌方单位会:

  1. 根据自身移动力计算所有可达位置
  2. 选择距离最近己方单位最近的目标位置
  3. 如果有多个候选位置,使用坐标顺序进行稳定的平局处理
  4. 移动后攻击可攻击范围内坐标最小的己方单位

攻击规则

  • 近战攻击:攻击自身上下左右相邻的格子
  • 远程攻击:沿上下左右方向延伸,遇到单位或非平原地形后停止,并包含阻挡格
  • 战斗机攻击:攻击自身上下左右距离为 2 的格子
  • 击退:根据攻击方向将目标推向相邻格,并处理山脉、海洋、沼泽、边界和单位碰撞
  • 坦克攻击山脉时,会将山脉摧毁为平原

Task4 特殊机制

战斗机

战斗机是己方空中单位,地图输入符号为 F。它拥有 2 点攻击力,可以攻击上下左右距离为 2 的格子。

  • 攻击敌方单位时造成伤害
  • 攻击己方单位时不造成伤害,而是恢复目标 2 点生命值
  • 攻击目标后,会对目标上下左右相邻的单位产生击退效果

刺蛇

刺蛇是敌方陆地单位,地图输入符号为 H。它使用近战攻击,拥有击退效果,并会使被攻击单位在下一回合无法攻击。

沼泽

沼泽是新的特殊地形,地图输入符号为 W。每次敌方回合结束后,每块沼泽都会对其中心周围边长为 5 的正方形范围内所有单位造成 1 点伤害。

如果一个单位同时处于多个沼泽范围内,则受到对应次数的伤害;生命值小于等于 0 的单位会从战场中移除。

输入格式

程序通过标准输入读取地图和后续操作。地图部分格式如下:

NT NU
R C T
...
R C U
...
  • NT:特殊地形数量,不包括默认的平原
  • NU:单位数量
  • R C:从 0 开始的行、列坐标,范围为 0 到 7
  • T:地形符号,M 表示山脉,O 表示海洋,W 表示沼泽
  • U:单位符号,S 表示士兵,T 表示坦克,B 表示蜜蜂,F 表示战斗机,H 表示刺蛇

示例地图:

3 6
3 3 M
6 6 O
4 4 W
0 0 F
2 3 S
6 3 T
2 5 B
4 5 H
7 7 S

地图读取完成后,程序继续从同一个标准输入流中读取玩家操作,并把游戏输出写入标准输出。

编译与运行

环境要求

  • C++11 或更高版本的编译器
  • MinGW-w64 / g++,或支持 GCC 工程的 Code::Blocks
  • 不依赖第三方库

使用 g++ 编译

在项目目录中执行:

g++ -std=c++11 -Wall -Wextra -pedantic \
  actions.cpp algorithms.cpp engine.cpp field.cpp main.cpp terrain.cpp unit.cpp \
  -o BattleField

Windows PowerShell 中可以使用一行命令:

g++ -std=c++11 -Wall -Wextra -pedantic actions.cpp algorithms.cpp engine.cpp field.cpp main.cpp terrain.cpp unit.cpp -o BattleField.exe

运行示例:

./BattleField < scenario.txt

PowerShell 中:

Get-Content .\scenario.txt | .\BattleField.exe

也可以使用 Code::Blocks 打开 BattleField.cbp 编译运行。

代码结构

BattleField/
├── main.cpp             # 程序入口,创建 8 x 8 战场并启动游戏
├── Grid.h               # 二维网格模板类
├── terrain.h/.cpp       # 地形类型和地形符号
├── unit.h/.cpp          # 单位属性、生命值和行动状态
├── field.h/.cpp         # 战场中的单位与地形管理
├── actions.h/.cpp       # 单位选择、移动和行动处理
├── algorithms.h/.cpp    # 移动范围和可达区域搜索
├── engine.h/.cpp        # 地图加载、地图显示、游戏循环和 AI
├── BattleField.cbp      # Code::Blocks 工程文件
├── README.md            # 项目说明
├── LICENSE              # MIT License
└── .gitignore           # 编译产物和 IDE 文件忽略规则

模块之间的主要关系如下:

main.cpp
    └── engine.cpp
          ├── field.cpp
          │     ├── Grid.h
          │     ├── terrain.cpp
          │     └── unit.cpp
          ├── actions.cpp
          └── algorithms.cpp

项目背景与整理说明

本项目来自《程序设计原理与方法》课程期末大作业的 Task4。原课程任务要求在已有框架上逐步实现地图加载、移动、生命值、攻击、回合制、敌方 AI、高级单位和特殊地形。本仓库保留完成全部任务后的 Task4 独立代码,作为一个可以单独阅读、编译和运行的 C++ 项目。

课程 PDF、判题脚本、测试数据、参考程序和编译生成物没有放入本仓库,以保持项目结构简洁,并避免发布课程测试资料。

验证

当前源码已通过以下编译检查:

g++ -std=c++11 -Wall -Wextra -pedantic -fsyntax-only \
  actions.cpp algorithms.cpp engine.cpp field.cpp main.cpp terrain.cpp unit.cpp

License

本项目采用 MIT License


English

BattleField is a small command-line tactical game written in C++. It takes inspiration from the battlefield and turn-based mechanics of Into the Breach.

This repository contains my completed Task 4 project for the course Principles and Methods of Programming. The game runs on an 8 x 8 grid: the player commands friendly units, while enemy units are controlled by a deterministic AI. The implementation covers map loading, movement, attacks, turn management, terrain effects, and reachability search.

This is an unofficial educational implementation and is not affiliated with the official Into the Breach project.

Highlights

  • An object-oriented model for units, terrain, and the battlefield
  • A reusable Grid<T> template for two-dimensional data
  • Movement-range calculation based on unit type, terrain, and movement points
  • Separate player and enemy turns with deterministic AI behavior
  • Three attack patterns: melee, ranged, and fighter-style attacks
  • Special effects including knockback, healing, disarming, terrain destruction, and swamp damage
  • A standard-input interface that makes scenarios easy to automate and reproduce

Objective

The goal is simple: command the friendly units and destroy every enemy unit on the 8 x 8 battlefield.

  • The game prints Won when all enemy units are destroyed.
  • The game prints Failed when all friendly units are destroyed while enemies remain.
  • At the end of a player turn, the enemy AI performs its actions.

Units

Unit Symbol Faction HP ATK Class Move points Attack style
Soldier S / s Friendly 2 1 Land 3 Melee
Tank T / t Friendly 3 1 Land 2 Ranged + knockback
Bee B / b Enemy 3 2 Air 4 Melee
Fighter F Friendly 2 2 Air 5 Fighter-type
Hydralisk H / h Enemy 1 1 Land 3 Melee + knockback + disarm

Friendly units are displayed with uppercase symbols, while enemy units use lowercase symbols. The current HP appears after the symbol, for example S2 or T3.

Terrain and movement costs

Terrain Symbol Land unit Air unit
Plain two spaces 1 1
Mountain /\ 100 100
Ocean ~~ 100 1
Swamp \/ 1 100
Occupied by another unit - 100 100

A movement cost of 100 is treated as impassable during normal movement.

Core mechanics

Player turn

During a player turn, each friendly unit can move at most once and attack at most once. Units that still have an available action are marked with + on the battlefield.

The player can then choose one of the following actions:

  • Move: display reachable cells and select a destination
  • Attack: display attackable cells and select a target
  • Skip: cancel the current selection
  • End this turn: finish the player turn and start the enemy AI turn

Enemy AI

Enemy units act in descending row-and-column order. For each enemy unit, the AI calculates all reachable cells, moves toward the nearest friendly unit, and then attacks the smallest-coordinate friendly target in range. Ties are resolved deterministically according to the assignment rules.

Attack rules

  • Melee attacks reach an adjacent cell in one of the four cardinal directions.
  • Ranged attacks follow the four cardinal directions and stop at the first unit or non-plain terrain, including that blocking cell.
  • Fighter-style attacks reach a cell two steps away in one of the four cardinal directions.
  • Knockback resolves differently when the target meets a mountain, ocean, swamp, map boundary, or another unit.
  • When a tank attacks a mountain, the mountain is destroyed and becomes plain terrain.

Task 4 mechanics

Fighter

The Fighter is a friendly air unit represented by F on the map. It has 2 attack points and can attack a cell two steps away in one of the four cardinal directions.

  • An attack against an enemy deals damage.
  • An attack against a friendly unit restores 2 HP instead of dealing damage.
  • After the attack, units in the four cells adjacent to the target are also affected by knockback.

Hydralisk

The Hydralisk is an enemy land unit represented by H. It uses melee attacks, applies the same knockback effect as a tank, and disarms the target so that it cannot attack during the following turn.

Swamp

Swamp is a special terrain represented by W. At the end of every enemy turn, each swamp deals 1 damage to every unit inside the 5 x 5 square centered on that swamp. Damage from multiple swamps is cumulative, and units with HP less than or equal to zero are removed from the battlefield.

Input format

The program expects both the map and the subsequent player commands on standard input. The map begins with the following format:

NT NU
R C T
...
R C U
...
  • NT: the number of special terrain cells, excluding the default plains
  • NU: the number of units
  • R C: a zero-based row and column coordinate, from 0 to 7
  • T: a terrain symbol: M for mountain, O for ocean, and W for swamp
  • U: a unit symbol: S, T, B, F, or H

Example map:

3 6
3 3 M
6 6 O
4 4 W
0 0 F
2 3 S
6 3 T
2 5 B
4 5 H
7 7 S

Once the map has been loaded, player commands continue to come from the same input stream, and the game writes its output to standard output.

Build and run

The project requires:

  • A C++11 or newer compiler
  • MinGW-w64 / g++, or Code::Blocks with GCC support
  • No third-party libraries

Build with g++:

g++ -std=c++11 -Wall -Wextra -pedantic \
  actions.cpp algorithms.cpp engine.cpp field.cpp main.cpp terrain.cpp unit.cpp \
  -o BattleField

Run a scenario by redirecting it to standard input:

./BattleField < scenario.txt

On Windows PowerShell, use:

g++ -std=c++11 -Wall -Wextra -pedantic actions.cpp algorithms.cpp engine.cpp field.cpp main.cpp terrain.cpp unit.cpp -o BattleField.exe
Get-Content .\scenario.txt | .\BattleField.exe

Alternatively, open BattleField.cbp in Code::Blocks.

Project structure

BattleField/
├── main.cpp             # Program entry point
├── Grid.h               # Two-dimensional grid template
├── terrain.h/.cpp       # Terrain types and symbols
├── unit.h/.cpp          # Unit attributes, HP, and action state
├── field.h/.cpp         # Battlefield, unit, and terrain management
├── actions.h/.cpp       # Unit selection, movement, and actions
├── algorithms.h/.cpp    # Reachability and movement search
├── engine.h/.cpp        # Map loading, display, game loop, and AI
├── BattleField.cbp      # Code::Blocks project file
├── README.md            # Project documentation
├── LICENSE              # MIT License
└── .gitignore           # Build and IDE ignore rules

Academic context

The original coursework introduced the features step by step: map loading and movement, HP and attacks, turn-based gameplay and enemy AI, followed by advanced units and terrain. This repository keeps the completed Task 4 implementation as a standalone project that can be read, built, and run on its own.

The course PDFs, assignment judge, test data, reference programs, and generated binaries are intentionally left out so that the public repository stays focused on the implementation.

License

This project is released under the MIT License.

About

C++ turn-based tactical game and programming course project

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages