A playable console Minesweeper (arrow keys, chording, flags, question marks, pause, difficulty presets) plus a deduction-and-guess solver that can play the game headlessly. Game logic lives in src/Minesweeper/Game and has no console dependencies, so it is covered by 65 unit tests. Includes publishing helpers for self-contained, trimmed single-file binaries (publish.sh) and an .editorconfig that the sources conform to: UTF-8 BOM and 4-space indents for C#, 2-space indents for project files, LF endings and 2-space indents for shell scripts, System-first usings, no redundant `this.`, and s_/ _ prefixed private static and instance fields.
53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using Minesweeper.Game;
|
|
|
|
namespace Minesweeper.Tests;
|
|
|
|
/// <summary>Builds fixed boards from a layout where 'x' marks a mine.</summary>
|
|
internal static class TestBoards
|
|
{
|
|
public const char Mine = 'x';
|
|
|
|
public static Board Create(params string[] layout)
|
|
{
|
|
if (layout.Length == 0)
|
|
throw new ArgumentException("A layout needs at least one row.", nameof(layout));
|
|
|
|
var height = layout.Length;
|
|
var width = layout[0].Length;
|
|
var mines = new List<CellRef>();
|
|
|
|
for (var y = 0; y < height; y++)
|
|
{
|
|
if (layout[y].Length != width)
|
|
throw new ArgumentException($"Layout row {y} is {layout[y].Length} wide, expected {width}.", nameof(layout));
|
|
for (var x = 0; x < width; x++)
|
|
if (layout[y][x] == Mine)
|
|
mines.Add(new CellRef(x, y));
|
|
}
|
|
|
|
var board = new Board(width, height, 0);
|
|
board.PlantMines(mines);
|
|
return board;
|
|
}
|
|
|
|
public static MinesweeperGame Game(int seed, params string[] layout) =>
|
|
new(Create(layout), new Random(seed));
|
|
|
|
public static MinesweeperGame SingleMine(int width = 4, int height = 4, int seed = 1) =>
|
|
Game(seed, Rows(width, height, new CellRef(0, 0)));
|
|
|
|
public static string[] Rows(int width, int height, params CellRef[] mines)
|
|
{
|
|
var rows = new string[height];
|
|
for (var y = 0; y < height; y++)
|
|
{
|
|
var characters = new char[width];
|
|
for (var x = 0; x < width; x++)
|
|
characters[x] = mines.Contains(new CellRef(x, y)) ? Mine : '.';
|
|
rows[y] = new string(characters);
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
}
|