/// <summary> /// Get's the center coordinate of a puzzle, if PuzzleSize.IsOdd() is true! /// </summary> /// <param name="puzzleSize">The PuzzleSize of the puzzle.</param> /// <returns>The center coordinate of the puzzle, if it has one.</returns> public static PuzzleCoordinate GetCenterCoordinate(this PuzzleSize puzzleSize) { if (puzzleSize != PuzzleSize.NineByNine) { throw new ArgumentOutOfRangeException(nameof(puzzleSize), "Must be PuzzleSize.NineByNine"); } var center = new PuzzleCoordinate(4, 4); return(center); }
/// <summary> /// Analyzes the puzzle's values to determine its current status. /// </summary> /// <param name="puzzle"></param> /// <returns>The state of the puzzle.</returns> public static PuzzleStatus GetPuzzleStatus(this Puzzle puzzle) { if (puzzle == null) { throw new ArgumentNullException(nameof(puzzle)); } // buffer for tracking used numbers in a ror, column or box var puzzleSize = puzzle.Size.ToInt32(); PuzzleSet numbersUsed; // Make sure every column contains the right numbers. It's ok if a column has holes // as long as those cells have possibilities, in which case it's a puzzle in progress. // However, two numbers can't be used in the same column, even if there are holes. foreach (var row in puzzle.ByRow()) { numbersUsed = new PuzzleSet(puzzle.Size); foreach (var cell in row) { if (cell.Value.HasValue) { var value = cell.Value.Value; if (numbersUsed.Contains(value)) { return(PuzzleStatus.Invalid); } numbersUsed.Add(value); } } } // Same for columns foreach (var col in puzzle.ByCol()) { numbersUsed = new PuzzleSet(puzzle.Size); foreach (var cell in col) { if (cell.Value.HasValue) { var value = cell.Value.Value; if (numbersUsed.Contains(value)) { return(PuzzleStatus.Invalid); } numbersUsed.Add(value); } } } // Same for boxes foreach (var box in puzzle.ByBox()) { numbersUsed = new PuzzleSet(puzzle.Size); foreach (var cell in box) { if (cell.Value.HasValue) { var value = cell.Value.Value; if (numbersUsed.Contains(value)) { return(PuzzleStatus.Invalid); } numbersUsed.Add(value); } } } // Now figure out if this is a solved puzzle or a work in progress // based on if there are any holes for (var row = 0; row < puzzleSize; row++) { for (var col = 0; col < puzzleSize; col++) { var coord = new PuzzleCoordinate(row, col); if (!puzzle[coord].HasValue) { return(PuzzleStatus.InProgress); } } } // If we made it this far, this state is a valid solution! Woo hoo! return(PuzzleStatus.Solved); }
public PuzzleCoordinateAndValue(PuzzleCoordinate coordinate, byte?value) { Coordinate = coordinate; Value = value; }
public PuzzleCoordinateAndValue() { Coordinate = new PuzzleCoordinate(-1, -1); }