001package algs13.xbacktrack.xsudoku; 002 003/** 004 * A mutable cell in a Sudoku grid. A mutable cell's digit can be 005 * changed as the solver works to find the solution to the puzzle. 006 */ 007final class XMutableCell extends XSudokuCell { 008 009 // The next cell for which a choice will be made after 010 // a choice is made for this cell. If the next cell is null, 011 // this cell is the last cell for which we need to choose a digit. 012 final XMutableCell nextCell; 013 014 // The current digit assigned to this mutable cell. 015 // A digit value of 0 indicates that the digit for the cell 016 // has not been assigned. 017 private int digit; 018 019 // Constructor for a Mutable cell. 020 XMutableCell(final int x, final int y, final XMutableCell next) { 021 super(x, y); 022 this.nextCell = next; 023 digit = 0; 024 } 025 026 // Get the current digit assigned to this cell. 027 int getDigit() { 028 return digit; 029 } 030 031 // Get the next digit to try for this cell. 032 // If the digit value wraps around to 0, we know we've tried all valid 033 // digits for this cell that could be tried given all the previous choices 034 // we have made. In this case we cannot try anything else, so the digit we 035 // assigned to the previously chosen cell cannot yield a valid result for 036 // the Sudoku puzzle. 037 int nextDigit() { 038 digit = (digit + 1) % (MySudoku.GRID_SIZE + 1); 039 return digit; 040 } 041 042 @Override 043 public String toString() { 044 return String.format("(%d, %d): %d", x, y, digit); 045 } 046}