private static bool CheckSeveralMissingColumn(GridSquareScript selection, GridSquareScript[] gridSquares) { int col = GridMaths.ColumnForSquare(selection.Index); GridSquareScript[] emptyColSquares = EmptyGridsAtIndexes(GridMaths.GridColumnIndices(col), gridSquares, selection); int[] emptyIndices = emptyColSquares.Select(x => x.Index).Distinct().ToArray(); int[] bigSquareIndices = emptyColSquares.Select(x => GridMaths.BigSquareForSquare(x.Index)).Distinct().ToArray(); List <int> usableSquares = new List <int>(); for (int i = 0; i < bigSquareIndices.Length; i++) { int box = bigSquareIndices[i]; if (!DoesBigSquareContainNumber(box, selection, gridSquares)) { int[] checks = emptyIndices.Intersect(GridMaths.GridColumnInSquare(col, box)).ToArray(); if (checks.Length > 0) { usableSquares.AddRange(checks); } } } if (usableSquares.Count > 0) { return(usableSquares.All(x => DoesRowContainNumber(GridMaths.RowForSquare(x), selection, gridSquares))); } return(true); }
public void HighlightColumn(int index) { int col = GridMaths.ColumnForSquare(index); int[] colSquares = GridMaths.GridColumnIndices(col); for (int i = 0; i < colSquares.Length; i++) { gridSquares[colSquares[i]].HighlightSquare(index); } }
/// <summary> /// Naked single check, if all numbers except one are accounted for in the square for index /// </summary> /// <param name="index"></param> /// <param name="gridSquares"></param> /// <returns>True if all numbers are accounted for with selection entry</returns> private static GridSolutionType CheckForNakedSingle(int index, GridSquareScript[] gridSquares) { int[] rowExisting = ExistingNumbersInRow(gridSquares, GridMaths.RowForSquare(index)).ToArray(); int[] colExisting = ExistingNumbersInColumn(gridSquares, GridMaths.ColumnForSquare(index)).ToArray(); int[] bigExisting = ExistingNumbersInBigSquare(gridSquares, GridMaths.BigSquareForSquare(index)).ToArray(); int[] allExisting = rowExisting.Concat(colExisting).Concat(bigExisting).Distinct().ToArray(); //.Where(x => x != number) bool countCheck = allExisting.Length == GridMaths.gridSize; print($"[CHECK] Naked single check - count: {countCheck}"); return(countCheck ? GridSolutionType.NakedSingle : GridSolutionType.None); //allExisting.Length == GridMaths.gridSize; }
private static GridSolutionType OnlyAvailableNumberCheck(GridSquareScript selection, GridSquareScript[] gridSquares) { int row = GridMaths.RowForSquare(selection.Index); int col = GridMaths.ColumnForSquare(selection.Index); int bigSquare = GridMaths.BigSquareForSquare(selection.Index); int[] rowMissing = MissingNumbersInRow(gridSquares, row); int[] colMissing = MissingNumbersInColumn(gridSquares, col); int[] bigMissing = MissingNumbersInBigSquare(gridSquares, bigSquare); bool overlapCheck = rowMissing.Intersect(colMissing).Intersect(bigMissing).ToArray().Length == 0; print($"Only available row: {string.Join(",", rowMissing)} col; {string.Join(",", colMissing)} big; {string.Join(",", bigMissing)} "); return(overlapCheck ? GridSolutionType.SingleRule : GridSolutionType.None); }
private static bool DoesSquareForceColumnNumber(int big, GridSquareScript selection, int col, GridSquareScript[] gridSquares) { print($"DoesSquare {big} ForceColumn {col} Number: {selection.Number}"); bool numberCheck = DoesBigSquareContainNumber(big, selection, gridSquares); if (DoesBigSquareContainNumber(big, selection, gridSquares)) { return(false); } int[] emptyIndices = EmptyGridsAtIndexes(GridMaths.GridBigSquareIndices(big), gridSquares, selection).Select(x => x.Index).Distinct().ToArray(); int[] noColIndices = emptyIndices.Where(x => !DoesColumnContainNumber(GridMaths.ColumnForSquare(x), selection, gridSquares)).ToArray(); int[] noRowIndices = emptyIndices.Where(x => !DoesRowContainNumber(GridMaths.RowForSquare(x), selection, gridSquares)).ToArray(); int[] forcedColumns = noColIndices.Intersect(noRowIndices).Select(GridMaths.ColumnForSquare).Distinct().ToArray(); print($"Empty indices in square {big} = {string.Join(",", emptyIndices)} - no col indices ({string.Join(",", noColIndices)}) no row indices ({string.Join(",", noRowIndices)}) = {string.Join(",", forcedColumns)} = {col}"); return(forcedColumns.Length == 1 && forcedColumns[0] == col); }
/// <summary> /// Simple completion check to see if hte row, column or square has all nine numbers in them. /// WARNING Doesn't check for duplicates!! /// </summary> /// <param name="index"></param> /// <param name="gridSquares"></param> /// <returns>True if any of the checks are true</returns> private static GridSolutionType SimpleCompletionCheck(int index, GridSquareScript[] gridSquares) //TODO can be simplified { if (RowCompleteCheck(gridSquares, GridMaths.RowForSquare(index))) { print("[CHECK] Simple completion test - Row complete"); return(GridSolutionType.CompleteRule); } else if (ColumnCompleteCheck(gridSquares, GridMaths.ColumnForSquare(index))) { print("[CHECK] Simple completion test - Column complete"); return(GridSolutionType.CompleteRule); } else if (BigSquareCompleteCheck(gridSquares, GridMaths.BigSquareForSquare(index))) { print("[CHECK] Simple completion test - Big Square complete"); return(GridSolutionType.CompleteRule); } return(GridSolutionType.None); }
/// <summary> /// Simple overlap check to duplicate numbers in the same row, column or big square /// </summary> /// <param name="number"></param> /// <param name="index"></param> /// <param name="gridSquares"></param> /// <returns>True if no duplicate numbers for the selected index</returns> private static bool SingleRuleCheck(GridSquareScript selection, GridSquareScript[] gridSquares) { int row = GridMaths.RowForSquare(selection.Index); int col = GridMaths.ColumnForSquare(selection.Index); int bigSquare = GridMaths.BigSquareForSquare(selection.Index); //print($"Grid - keyboard number: {number} on square: {index}"); bool existsInRow = DoesRowContainNumber(row, selection, gridSquares); bool existsInCol = DoesColumnContainNumber(col, selection, gridSquares); bool existsInBig = DoesBigSquareContainNumber(bigSquare, selection, gridSquares); bool failed = existsInRow || existsInCol || existsInBig; if (!existsInRow && !existsInCol && !existsInBig) { //TODO add check for only number } print($"[CHECK] Simple overlap test: row: {existsInRow} col: {existsInCol} big: {existsInBig} => {failed}"); return(failed); }
private static int[] GridEmptyIntersectIndices(GridSquareScript[] gridSquares, GridSquareScript selection) { int square = GridMaths.BigSquareForSquare(selection.Index); GridSquareScript[] emptySquares = EmptyGridsAtIndexes(GridMaths.GridBigSquareIndices(square), gridSquares, selection); int[] emptyIndices = EmptyGridsAtIndexes(GridMaths.GridBigSquareIndices(square), gridSquares, selection).Select(x => x.Index).Distinct().ToArray(); //check how many row are in the empty indicies int[] emptyCols = emptyIndices.Where(x => !DoesColumnContainNumber(GridMaths.ColumnForSquare(x), selection, gridSquares)).ToArray(); int[] emptyRows = emptyIndices.Where(x => !DoesRowContainNumber(GridMaths.RowForSquare(x), selection, gridSquares)).ToArray(); int[] intersectIndices = emptyCols.Intersect(emptyRows).ToArray(); print($"GridEmptyIntersectIndices: ({string.Join(",", emptyIndices)}) empty rows: {string.Join(",", emptyRows)} cols: {string.Join(",", emptyCols)} intersect: {string.Join(",", intersectIndices)}"); return(emptyCols.Intersect(emptyRows).ToArray()); }
private static GridSolutionType CheckDoubleDeduction(GridSquareScript[] gridSquares, GridSquareScript selection) { int[] intersectIndices = GridEmptyIntersectIndices(gridSquares, selection); int[] missingRows = intersectIndices.Select(GridMaths.RowForSquare).Where(x => x != GridMaths.RowForSquare(selection.Index)).Distinct().ToArray(); int[] missingCols = intersectIndices.Select(GridMaths.ColumnForSquare).Where(x => x != GridMaths.ColumnForSquare(selection.Index)).Distinct().ToArray(); print($"Intersect indices: {string.Join(", ", intersectIndices)} Missing rows: {string.Join(", ", missingRows)} cols: {string.Join(", ", missingCols)}"); int forceRowCount = 0; int big = GridMaths.BigSquareForSquare(selection.Index); for (int i = 0; i < missingRows.Length; i++) { int[] otherBigRows = GridMaths.OtherBigSquaresForRow(missingRows[i], big); for (int j = 0; j < otherBigRows.Length; j++) { if (DoesSquareForceRowNumber(otherBigRows[j], selection, missingRows[i], gridSquares)) { forceRowCount += 1; } } } int forceColCount = 0; for (int i = 0; i < missingCols.Length; i++) { int[] otherBigCols = GridMaths.OtherBigSquaresForColumn(missingCols[i], big); for (int j = 0; j < otherBigCols.Length; j++) { if (DoesSquareForceColumnNumber(otherBigCols[j], selection, missingCols[i], gridSquares)) { forceColCount += 1; } } } print($"[CHECK] Double deduction: Forced rows: {missingRows.Length} = {forceRowCount} && cols: {missingCols.Length} = {forceColCount} check: {forceColCount == missingCols.Length && forceRowCount == missingRows.Length}"); return((forceColCount == missingCols.Length && forceRowCount == missingRows.Length) ? GridSolutionType.DoubleDeduction : GridSolutionType.None); }