using Minesweeper.Game;
namespace Minesweeper.Tests;
/// Builds fixed boards from a layout where 'x' marks a mine.
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();
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;
}
}