private void InferFromNeighbouringLines(Board b) { for (int x = 0; x < 9; x++) { int?[] line = b.Row(x); for (int y = 0; y < 9; y++) { // If a blank item is found ... if (line[y] == null) { // ... start by figuring which threeBy it's in ... } } } for (int y = 0; y < 9; y++) { b.Column(y); } }
/// <summary> /// Check each row and column - /// if there's only the one unfilled cell, /// it's easy to figure out what the value should be. /// </summary> private void OneOnALineCheck(Board b) { // [TODO] Should probably look at using a local method NullCount nullCount = delegate(int?[] line) { int nullsOnLineCount = 0; for (int x = 0; x < 9; x++) { if (line[x] == null) { nullsOnLineCount++; } } return(nullsOnLineCount); }; ReplaceSingleValue replaceSingleValue = delegate(int?[] line) { // ... figure out the missing value ... // 45 int summedV = 0; foreach (int?v in line) { summedV += v ?? 0; } // ... and insert it. for (int i = 0; i < 9; i++) { if (line[i] == null) { line[i] = 45 - summedV; break; } } return(line); }; // Check each row for how many nulls for (int x = 0; x < 9; x++) { if (nullCount(b.Row(x)) == 1) { b.Row(x, replaceSingleValue(b.Row(x))); } } // Check each column for how many nulls for (int y = 0; y < 9; y++) { if (nullCount(b.Column(y)) == 1) { b.Column(y, replaceSingleValue(b.Column(y))); } } // Check each threeBy for how many nulls for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (nullCount(b.ThreeBy(x, y)) == 1) { b.ThreeBy(x, y, replaceSingleValue(b.ThreeBy(x, y))); } } } // var row = b.Line(x); //for (int y = 0; y < 9; y++) //{ // if (row[y] == null) // { // nullsOnRowCount++; // } //} //// If there's only the one empty cell ... //if (nullsOnRowCount == 1) //{ // // ... figure out the missing value ... // // 45 // int summedV = 0; // foreach (int? v in row) // summedV += v ?? 0; // // ... and insert it. // for(int i = 0; i < 9; i++) // { // if (row[i] == null) // { // row[i] = 45 - summedV; // break; // } // } //} // } // Check each column for how many nulls for (int y = 0; y < 9; y++) { } }