public static IEnumerable <Cell[, ]> Solve(Cell[,] board) { board = board.Copy(); var i = 0; var unknowns = board.Points() .Where(s => board[s.x, s.y] == Cell.Unknown) .ToArray(); while (i >= 0) { var(x, y) = unknowns[i]; var cell = board[x, y]; int nextGuessIndex = cell == Cell.Unknown ? 0 : Array.IndexOf(FillOptions, cell) + 1; if (nextGuessIndex >= FillOptions.Length) { board[x, y] = Cell.Unknown; i--; continue; } board[x, y] = FillOptions[nextGuessIndex]; if (board.LegalSquare(x, y) && board.LegalNumbers(x, y)) { if (i < unknowns.Length - 1) { i++; } else if (board[x, y] == Cell.Full && board.LegalPath(x, y) || board[x, y] != Cell.Full && board.LegalPath()) { yield return(board.Copy()); } } } }
public static bool AddRandomNumber(Cell[,] board, Random random) { var vacantSpots = board.Points() .Where(p => board[p.x, p.y] == Cell.Unknown || board[p.x, p.y] == Cell.Empty) .ToArray(); if (vacantSpots.Length == 0) { return(false); } var p = vacantSpots.RandomItem(random); Generation.FillNumber(board, p); return(true); }
public static bool LegalNumbers(this Cell[,] board) => !board.Points() .Any(s => board[s.x, s.y] >= 0 && !board.LegalNumbers(s.x, s.y));
public static bool LegalPath(this Cell[,] board) => !board.Points().TryFirst(s => board[s.x, s.y] == Cell.Full, out var spot) || board.LegalPath(spot.x, spot.y);