//returns true when this is a buddy of g // A buddy is a square that has a row, column or box in common. public bool isBuddy(gridSquare g) { if (X == g.X || Y == g.Y) { return(true); } if (Y / 3 == g.Y / 3 && X / 3 == g.X / 3) { return(true); } return(false); }
/// <summary> /// Instantiate a blank SudokuGrid. /// </summary> public SudokuGrid() { solveGrid = new gridSquare[9, 9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { solveGrid[i, j] = new gridSquare(i, j); } } setUnsolvedValuesToInitialState(); }
//Make this instance a deep copy of toCopy private void copy(gridSquare toCopy) { this.x = toCopy.x; this.y = toCopy.y; this.knownValue = toCopy.knownValue; for (int i = 1; i <= 9; ++i) { this.possibilities[i] = toCopy.possibilities[i]; } this.possList = new List <int>(toCopy.possList); this.solveType = toCopy.solveType; this.setList = new PossibilitySet(toCopy.setList); }
/// <summary> /// Copy constructor. Does a deep copy. /// </summary> /// <param name="toCopy">instance to copy</param> public SudokuGrid(SudokuGrid toCopy) { solveGrid = new gridSquare[9, 9]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { solveGrid[i, j] = new gridSquare(toCopy.solveGrid[i, j]); } } unsolvedValues = new HashSet <int> [3, 9]; //3 types of section (row, col, box), nine of each type for (int i = 0; i < 9; i++) { unsolvedValues[(int)iterateBy.Box, i] = new HashSet <int>(toCopy.unsolvedValues[0, i]); unsolvedValues[(int)iterateBy.Row, i] = new HashSet <int>(toCopy.unsolvedValues[1, i]); unsolvedValues[(int)iterateBy.Col, i] = new HashSet <int>(toCopy.unsolvedValues[2, i]); } }
//copy constructor. does a deep copy. public gridSquare(gridSquare toCopy) { possibilities = new bool[9 + 1]; this.copy(toCopy); }
//scan for and use standard Y-Wing elimination method private bool ywingEliminationScan() { bool madeChanges = false; //don't loop inside this one; it's too expensive //fill out a list with all gridsquares that contain only a pair of possibilities (this will be faster than scanning entire grid for each pair combo.) List <gridSquare> pairs = new List <gridSquare>(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (solveGrid[i, j].PossibilityCount == 2) { pairs.Add(solveGrid[i, j]); } } } //scan through for any ABC alignment of pairs for (int i = 0; i < pairs.Count; i++) { gridSquare AB = pairs[i]; gridSquare AC = null; gridSquare BC = null; int A = AB.PossibilityList[0]; int B = AB.PossibilityList[1]; int C = 0; //test for ABC triple of pairs (AB, AC, BC) //find a matching XZ for (int j = 0; j < pairs.Count; j++) { if (pairs[j].isPossible(B)) //don't allow to match same pair { continue; } if (!pairs[j].isBuddy(AB)) { continue; } if (pairs[j].isPossible(A)) { AC = pairs[j]; //extract C value if (AC.PossibilityList[0] != A) { C = AC.PossibilityList[0]; } else { C = AC.PossibilityList[1]; } //find a matching BC for (int k = 0; k < pairs.Count; k++) { if (k == i) { continue; } if (!pairs[k].isBuddy(AB)) { continue; } if (pairs[k].isPossible(B) && pairs[k].isPossible(C)) { BC = pairs[k]; //eliminate other possibilites foreach (gridSquare g in solveGrid) { if (g.isBuddy(BC) && g.isBuddy(AC) && g != BC && g != AC) { madeChanges |= g.eliminate(C); } } if (madeChanges) { return(true); } } } } } } return(madeChanges); }