Class SudokuRow2
- java.lang.Object
-
- SudokuRow2
-
public class SudokuRow2 extends Object
A class that represents a row in the game sudoku. ASudokuRowowns 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 intSIZEThe number of digits in a sudoku row
-
Constructor Summary
Constructors Constructor Description SudokuRow2()Initializes this sudoku row so that the digits of this row are unknown.SudokuRow2(List<Digit> row)Initializes this sudoku row so that the digits of this row are equal to the digits in the specified list.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Digitget(int index)Gets the digit at the specified index of this row.(package private) List<Digit>getRow()Returns the list for this row.booleanisValid()Returns true if this row is a valid sudoku row and false otherwise.(package private) static booleanisValid(List<Digit> digits, List<Digit> digitsSeenSoFar)Returns true ifdigitsmight be the digits of a valid sudoku row.static voidmain(String[] args)voidreplace(Digit old, Digit d)Replaces the first occurrence of a digit equal tooldin this row with a digit equal tod.SudokuRow2solve()Returns a possible solution for this sudoku row; any valid solution might be returned by this method.StringtoString()Returns a string representation of this sudoku row.List<Digit>unsafeGetRow()Returns a shallow copy of the digits of this row.
-
-
-
Constructor Detail
-
SudokuRow2
public SudokuRow2()
Initializes this sudoku row so that the digits of this row are unknown. The digit0is used to indicate an unknown digit.
-
SudokuRow2
public SudokuRow2(List<Digit> row)
Initializes this sudoku row so that the digits of this row are equal to the digits in the specified list. The digit0is used to indicate an unknown digit.- Parameters:
row- a list of digits for this row- Throws:
IllegalArgumentException- if row.size() != SudokuRow.SIZE
-
-
Method Detail
-
unsafeGetRow
public List<Digit> unsafeGetRow()
Returns a shallow copy of the digits of this row.- Returns:
- a shallow 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 of0.- 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
-
replace
public void replace(Digit old, Digit d)
Replaces the first occurrence of a digit equal tooldin this row with a digit equal tod.- Parameters:
old- the value of the digit to replaced- the replacement value
-
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 digit0which is used to indicate an unknown digit).- Returns:
- true if this row is a valid sudoku row and false otherwise
-
isValid
static boolean isValid(List<Digit> digits, List<Digit> digitsSeenSoFar)
Returns true ifdigitsmight be the digits of a valid sudoku row.digitsSeenSoFaris the list of digits that have been already seen indigitsby previous recursive calls of this method.- Parameters:
digits- the remaining digits of a sudoku rowdigitsSeenSoFar- the digits that have been seen previously in the list digits by previous recursive calls of this method- Returns:
-
solve
public SudokuRow2 solve()
Returns a possible solution for this sudoku row; any valid solution might be returned by this method.This method creates a new
SudokuRowobject 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.
-
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)
-
-