Skip to content

alisterchristie/MineSweeper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

167 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minesweeper

A Delphi implementation of Minesweeper, built as a study in clean architecture across multiple UI frameworks and grid topologies. A single game engine drives two front-ends (VCL and FireMonkey), each supporting five grid types — square, hexagonal, octagon-square (4.8.8), triangle, and trihexagonal (3.6.3.6) — all backed by a shared DUnitX test suite.

Variants

Project Framework Grids Notes
MineSweeperVCL VCL Square, Hex, OctSqr, Triangle, TriHex Windows desktop; geometry selectable in Settings
MineSweeperFM FireMonkey (FMX) Square, Hex, OctSqr, Triangle, TriHex Cross-platform (Windows / macOS / iOS / Android); geometry selectable in Settings

Both share MineSweeperEngine.pas and select grid behaviour through the INeighbourCalculator strategy. Each front-end has its own renderer interface (IBoardRenderer for FMX, IVCLBoardRenderer for VCL) with all five implementations sharing the same framework-agnostic geometry units.

Architecture

MineSweeperEngine ──► INeighbourCalculator ──┬─► TSquareNeighbourCalculator
                  │                          ├─► THexagonNeighbourCalculator
                  │                          ├─► TOctSqrNeighbourCalculator
                  │                          ├─► TTriangleNeighbourCalculator
                  │                          └─► TTriHexNeighbourCalculator
                  ├─► IMineSweeperSound ─────┬─► TMinesweeperDummySound
                  │                          ├─► TMinesweeperWindowsSound  (MMSystem)
                  │                          └─► TMineSweeperAudioManagerSound (cross-platform)
                  └─► TGameSettings (Easy / Medium / Expert / Custom)

Key design points:

  • Engine is UI-agnostic. TMineSweeperEngine exposes LeftClick, RightClick, MiddleClick (chord) and a board grid; the forms only translate mouse events into those calls and paint the result.
  • First click is always safe. Mines are placed after the first reveal, with the clicked cell excluded via PlaceMines(MineCount, AvoidX, AvoidY) (Fisher–Yates partial shuffle).
  • Grid topology is pluggable. INeighbourCalculator lets the same engine drive square (8-connected), hex (6-connected), oct-sqr (variable), triangle (12-connected), and trihex (hex+triangle, 12/6) boards.
  • Rendering is pluggable. IBoardRenderer (FMX) and IVCLBoardRenderer (VCL) bundle cell layout, painting, and hit-testing behind one interface per framework, so each form handles all five grids without conditional logic. The geometry math is Single-based and shared between the two.
  • Sound is pluggable. IMineSweeperSound has Windows-resource, cross-platform AudioManager, dummy, and string-recording (test) implementations.
  • Win condition is incremental. The engine tracks FUnresolvedCells and FInvalidFlags rather than rescanning the board.
  • High-scores are persisted via SQLite/FireDAC, shared by both front-ends (MineSweeperHighScores.pas).
  • Save / load. MineSweeperSaveGame.pas serialises full engine state to a human-readable text format (key/value header + ASCII grid). Files round-trip byte-identically and can be hand-edited.

Command line

Both front-ends accept the following switches (any of -name value, --name value, -name=value, etc.):

Switch Effect
--load <file> Open a save file (absolute path or relative to ~/MineSweeper/saves/)
--gametype <square|hex|octsqr|triangle|trihex> Preselect the grid geometry
--difficulty <easy|medium|expert> Preselect difficulty (hard aliases expert)
--webport <N> FMX debug builds: override the debug WebServer port (default 3002), so multiple instances can run side by side

Debug WebServer (FMX, DEBUG builds)

The FMX build embeds a debug-only HTTP server (Indy) on http://127.0.0.1:3002. It exposes endpoints for screenshots, board state, RTTI property read/write, action execution, save/load, and synthetic clicks — enough surface for automated agent-driven testing and visual verification. See AGENTS.md for the full endpoint table. The server is gated behind {$IFDEF DEBUG} and is not built into release.

Build

Building requires RAD Studio (tested with Delphi 12 / version 37.0). rsvars.bat sets the MSBuild environment.

Build everything:

call "C:\Program Files (x86)\Embarcadero\Studio\37.0\bin\rsvars.bat"
msbuild MineSweeperGroup.groupproj

Build an individual project:

msbuild MineSweeperVCL.dproj /p:Config=Debug /p:Platform=Win32
msbuild MineSweeperFM.dproj  /p:Config=Debug /p:Platform=Win32

The projects can also be opened directly in the RAD Studio IDE via MineSweeperGroup.groupproj.

Tests

The test suite uses DUnitX and covers the engine, neighbour calculators, settings, timer, sound dispatch, geometry layouts, and end-to-end gameplay (square, hex, and oct-sqr).

msbuild Testing\MineSweeperTests.dproj /p:Config=Debug /p:Platform=Win32
Testing\Win32\Debug\MineSweeperTests.exe --exit:Continue

Test fixtures of note:

  • MineSweeperEngineTests — unit-level engine behaviour (mine placement, flagging, flood-fill, chord click, first-move safety).
  • FullGameTests / FullHexGameTests / FullOctSqrGameTests / FullTriangleGameTests / FullTriHexGameTests — scripted full games on a known board, asserting state transitions and the correct sound on each click.
  • SquareGridTests / HexGridTests / TriangleGridTests / TriHexGridTests — per-geometry tests (layout + neighbour calculator).
  • SaveGameTests — round-trip for every cell code, all geometries, malformed input, and disk I/O.

Repository Layout

MineSweeperEngine.pas            game engine + TEngineState transfer record
MineSweeperNeighbourCalculator.pas  shared neighbour-calculator base
MineSweeperSettings.pas          difficulty / board size / CLI parsing
MineSweeperSaveGame.pas          human-readable save/load + JSON serialiser
MineSweeperHighScores*.pas       SQLite-backed high score system

MineSweeperVCL*.pas              VCL desktop front-end
MineSweeperVCLRenderer.pas       IVCLBoardRenderer implementations
MineSweeperFM*.pas               FireMonkey cross-platform front-end
MineSweeperRenderer.pas          IBoardRenderer implementations
WebServerFMX.pas                 FMX debug HTTP server (DEBUG-only)

SquareGridGeometry.pas           square-grid layout math + neighbour calc
HexGridGeometry.pas              hex-grid layout math + neighbour calc
OctSqrGridGeometry.pas           octagonal-square layout + neighbour calc
TriangleGridGeometry.pas         triangle layout + neighbour calc
TriHexGridGeometry.pas           trihexagonal layout + neighbour calc

MinesweeperSoundInterface.pas    sound abstraction
MinesweeperWindowSound.pas       Windows MMSystem implementation
MinesweeperAudioManagerSound.pas cross-platform implementation
AudioManager.pas                 Embarcadero sample audio manager (bundled)

Testing/                         DUnitX project + fixtures
Sounds/, images/                 game assets

License

Personal project; no license declared. Contact the author before reusing.

About

A series minesweeper games

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages