| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System;
- using System.Collections.Generic;
- namespace DevilLink2.Core
- {
- public static class BoardGenerator
- {
- public static Board Generate(int columns, int rows, int seed)
- {
- if (columns <= 0) throw new ArgumentOutOfRangeException(nameof(columns));
- if (rows <= 0) throw new ArgumentOutOfRangeException(nameof(rows));
- long count = (long)columns * rows;
- if (count > int.MaxValue || count % 2 != 0)
- throw new ArgumentException("A full board requires an even, representable cell count.");
- var symbols = new List<TileSymbol>((int)count);
- for (int pair = 0; pair < count / 2; pair++)
- {
- var symbol = (TileSymbol)(pair % 12);
- symbols.Add(symbol); symbols.Add(symbol);
- }
- Shuffle(symbols, new Random(seed));
- var tiles = new List<Tile>((int)count);
- for (int i = 0; i < count; i++)
- tiles.Add(new Tile(i + 1, symbols[i], i / columns, i % columns));
- return new Board(columns, rows, tiles);
- }
- /// <summary>In-place Fisher-Yates; one unbiased bounded draw per swap.</summary>
- public static void Shuffle<T>(IList<T> values, Random random)
- {
- if (values == null) throw new ArgumentNullException(nameof(values));
- if (random == null) throw new ArgumentNullException(nameof(random));
- for (int i = values.Count - 1; i > 0; i--)
- {
- int j = random.Next(i + 1);
- T value = values[i]; values[i] = values[j]; values[j] = value;
- }
- }
- }
- }
|