Class SudokuRow


  • public class SudokuRow
    extends Object
    A class that represents a row in the game sudoku. A SudokuRow owns the digits in the row.

    In the game sudoku, the player tries to complete a 9x9 grid of digits where every row, column, and the nine 3x3 subgrids contain all of the digits 1 through 9.

    A sudoku row consists of nine cells where each cell can hold one digit whose value is between 1 and 9. A solved sudoku row has all of the digits between 1 and 9 in the cells; for example the string:

    |3|7|2|4|8|5|9|6|1|

    represents a solved sudoku row. An unsolved sukoku row has one or more cells where the digit is unknown; for example the string:

    |1|8|?|3|?|7|?|9|?|

    represents an unsolved sudoku row where four cells contain unknown digits.

    An invalid sudoku row has two or more equal digits (between 1 and 9); for example the string:

    |2|?|?|2|3|5|?|1|9|

    is invalid (because it has two 2s).

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int SIZE
      The number of digits in a sudoku row
    • Constructor Summary

      Constructors 
      Constructor Description
      SudokuRow()
      Initializes this sudoku row so that the digits of this row are unknown.
      SudokuRow​(List<Digit> row)
      Initializes this sudoku row so that the digits of this row are equal to the digits in the specified list.
    • Field Detail

      • SIZE

        public static int SIZE
        The number of digits in a sudoku row
    • Constructor Detail

      • SudokuRow

        public SudokuRow()
        Initializes this sudoku row so that the digits of this row are unknown. The digit 0 is used to indicate an unknown digit.
      • SudokuRow

        public SudokuRow​(List<Digit> row)
        Initializes this sudoku row so that the digits of this row are equal to the digits in the specified list. The digit 0 is used to indicate an unknown digit.
        Parameters:
        row - a list of digits for this row
        Throws:
        IllegalArgumentException - if row.size() != SudokuRow.SIZE
    • Method Detail

      • safeGetRow

        public List<Digit> safeGetRow()
        Returns a deep copy of the digits of this row.
        Returns:
        a deep copy of the digits of this row
      • get

        public Digit get​(int index)
        Gets the digit at the specified index of this row. The returned digit cannot be used to modify this row. The first digit of this row has an index of 0.
        Parameters:
        index - the index of the digit to get
        Returns:
        a digit whose value is equal to the digit in this row at the specified index
        Throws:
        IllegalArgumentException - if index is not a valid index for this row
      • set

        public void set​(int index,
                        Digit d)
        Sets the digit at the specified index of this row to equal the value of the specified digit. The digit d cannot be used to modify this row after this method completes. The first digit of this row has an index of 0.
        Parameters:
        index - the index of the digit to get
        d - the value to use to set the digit of this row at the specified index
        Throws:
        IllegalArgumentException - if index is not a valid index for this row
      • isValid

        public boolean isValid()
        Returns true if this row is a valid sudoku row and false otherwise. A valid sudoku row has no repeated digits (except possibly for the digit 0 which is used to indicate an unknown digit).
        Returns:
        true if this row is a valid sudoku row and false otherwise
      • solve

        public SudokuRow solve()
        Returns a possible solution for this sudoku row; any valid solution might be returned by this method.

        This method creates a new SudokuRow object where the known digits of this row are in the same locations in the new row and the unknown digits of this row are replaced with digits whose values create a valid sudoku row. For example if this row has the digits:

        |5|9|?|3|8|6|1|7|?| (the 2 and 4 are missing from the row)

        then this method will return either the row:

        |5|9|2|3|8|6|1|7|4|

        or

        |5|9|4|3|8|6|1|7|2|

        Returns:
        a new SudokuRow containing a possible solution for this row
        Precondition:
        this.isValid() == true
      • toString

        public String toString()
        Returns a string representation of this sudoku row. The string representation is the digits of this row separated by a vertical bar. Unknown digits appear as a question mark. For example, the string:

        |3|8|9|1|7|2|5|6|4|

        represents one example of a solved sudoku row, and the string

        |?|3|8|?|1|7|?|9|?|

        represents one example of a sudoku row where four digits are unknown.

        Overrides:
        toString in class Object
        Returns:
        a string representation of this sudoku row
      • toString

        static String toString​(List<Digit> t)
        Recursively computes the string representation of this sudoku row. See the toString method above.
        Parameters:
        t - a list of digits
        Returns:
        the string representation of t needed by toString
      • getRow

        List<Digit> getRow()
        Returns the list for this row. Used for testing purposes.
        Returns:
        the list for this row
      • main

        public static void main​(String[] args)