Minesweeper implemented in java Swing/AWT. Completed in 2022 for a high school coding class. Single source file (minesweeper.java) with image and font assets loaded from disk at runtime. DFS flood fill to search cells. all clicking logic done with coordinate arithmetic instead of button objects.
Data model
Each tile is a Cell with flags for mine, opened, marked, and a count of adjacent mines.
Mine placement
placeMines() assigns numMines mines at random coordinates; if a coordinate already holds a mine, the attempt repeats.
readBoard() fills adjacentMines for non-mine cells by scanning the eight neighbors with explicit bounds checks.
Reveal logic
openCell(row, col) sets isOpened. Mine → loss. Count 0 → recursive reveal to the eight adjacent cells (depth-first flood).
Win condition
checkWin() compares the number of unopened cells to numMines; equality means all safe cells are open.
Score / timer
A background thread increments time once per second only while gameState == 2. On win, score is set to elapsed time; lower is better.
First click
dontDieOnFirstClick regenerates the board until the first opened cell is non-mine with adjacentMines == 0. The source notes an alternative: defer generation until after the first click.
UI state machine
gameState selects menu, difficulty, play, high-score screen, loss, or win. paintComponent draws the corresponding GS*.png backdrop and the grid: Flag, Unopened, or adjacentMines[n] per cell (AM0–AM8).
Difficulties
Easy: 10×10, 10 mines. Medium: 16×16, 40 mines. Hard: 30×16, 99 mines. Horizontal placement uses difficultyOffset (and adjustmentForExtreme on hard). Right-click toggles flags subject to numFlags.
Persistence
highscores.txt supplies a 3×3 grid of ints at startup (three difficulties × three randomly generated scores). On a qualifying win, the new time is inserted and that difficulty’s row is sorted.
Asset loading
main registers two TTF fonts and reads GS0–GS5, AM0–AM8, mine.png, flag.jpg, unopened.png via relative paths. Working directory must contain those files or initialization fails (console: "image importation error").
only requires a JDK with javac and java on PATH.
cd <project root>
javac minesweeper.java
java minesweeperRun from the directory that contains minesweeper.java and the bundled assets so relative paths resolve.