uk.co.keang.sudoku
Class Puzzle

java.lang.Object
  extended by uk.co.keang.sudoku.Puzzle
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class Puzzle
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

A Sudoku puzzle

Author:
A.G.Docherty
See Also:
Serialized Form

Nested Class Summary
static class Puzzle.Cell
           
static class Puzzle.PuzzleDifficulty
           
 
Field Summary
static Puzzle.Cell allPossibleNumbers
           
 
Constructor Summary
Puzzle()
          Constructs an empty puzzle
Puzzle(java.io.BufferedReader r)
          Constructs a puzzle from this input stream
Puzzle(Puzzle.Cell[][] grid, Puzzle.PuzzleDifficulty dif)
          Constructs a puzzle from this grid
 
Method Summary
 void clear()
          Clears this puzzle of all values
 Puzzle clone()
           
(package private) static Puzzle.Cell[][] copyGrid(Puzzle.Cell[][] grid)
          creates an copy of the grid
(package private) static Puzzle.Cell[][] copyGrid(Puzzle.Cell[][] src, Puzzle.Cell[][] dest)
          Copies this grid to another grid
 int countKnownValues()
          Counts the number of known values in this puzzle
 boolean equals(java.lang.Object obj)
           
 Puzzle.Cell[] getBlock(int i)
          Gets the options block
 Puzzle.Cell[] getBlock(int x, int y)
          Gets the options block
 int getBlockIndex(int x, int y)
          Gets the options block
 Puzzle.Cell[] getColumn(int i)
          Gets the options column
 Puzzle.PuzzleDifficulty getDifficulty()
          Gets an indication of how difficult it is to solve this puzzle
static int[] getFrequencyOfPossibilities(Puzzle.Cell[] sets)
          Gets the frequency of possible values.
 Puzzle.Cell[][] getGrid()
          Gets this puzzle grid
 Puzzle.Cell getKnownValues(java.awt.Point p)
          Gets the known values from the row, column or block for this point
 java.awt.Point getLocation(Puzzle.Cell set)
          Gets the zero based x, y coordinates of this set in the grid
static java.lang.String getLocationsAsText(java.util.List<java.awt.Point> loc)
          Gets these locations as string of textual coordinates with letters for the x axis and numbers for the y axis
static java.lang.String getNumbersAsText(java.util.Collection<java.lang.Integer> removed)
          Gets these numbers as a formatted text string
static java.lang.String getNumbersAsText(Puzzle.Cell cell)
          Gets these numbers as a formatted text string
 Puzzle.Cell getPossibleValues(java.awt.Point p)
          Gets the possible values for this point
static java.lang.String[] getXAxisLabels()
           
static java.lang.String[] getYAxisLabels()
           
 int hashCode()
           
 boolean isComplete()
          Checks to see if the sudoku is complete
 boolean isLegal()
          Checks all resolved values to check they are legal
 boolean isUnique()
          Checks to see if the sudoku is unique by using the brute force solver several times and comparing the complete grids
 int makeUnique()
          Makes the sudoku have a unique solution by adding discrete cell values to the starting point This can only be successfully called on puzzles that have a solution.
 void read(java.io.BufferedReader r)
          Reads this file from the input stream
 void rotate()
          Rotates the grid clockwise 90 degrees
 Puzzle.Cell set(int i, int num)
          Sets the value at square denoted by the sequential (1 based) location from top left to bottom right.
 Puzzle.Cell set(int x, int y, int num)
          Sets the value at square denoted by the x, y (0 based) location
 Puzzle.Cell set(int x, int y, Puzzle.Cell nums)
          Sets the values at square denoted by the x, y (0 based) location.
 void setDifficulty(AbstractSolver.SolverDifficulty solver, int factor)
          Sets an level indicating how difficult it is to solve this puzzle
 void setDifficulty(Puzzle.PuzzleDifficulty dif)
          Sets an level indicating how difficult it is to solve this puzzle
 void setGrid(Puzzle.Cell[][] grid)
          Sets the values for each cell of the sudoku grid.
 java.lang.String toString()
           
 void write(java.io.PrintWriter w)
          Writes this file to the output stream
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

allPossibleNumbers

public static final Puzzle.Cell allPossibleNumbers
Constructor Detail

Puzzle

public Puzzle()
Constructs an empty puzzle

Parameters:
hGrid -

Puzzle

public Puzzle(Puzzle.Cell[][] grid,
              Puzzle.PuzzleDifficulty dif)
Constructs a puzzle from this grid

Parameters:
grid -

Puzzle

public Puzzle(java.io.BufferedReader r)
       throws java.io.IOException,
              IllegalGridException
Constructs a puzzle from this input stream

Throws:
IllegalGridException
java.io.IOException
Method Detail

getXAxisLabels

public static java.lang.String[] getXAxisLabels()

getYAxisLabels

public static java.lang.String[] getYAxisLabels()

rotate

public void rotate()
Rotates the grid clockwise 90 degrees


clear

public void clear()
Clears this puzzle of all values


makeUnique

public int makeUnique()
Makes the sudoku have a unique solution by adding discrete cell values to the starting point This can only be successfully called on puzzles that have a solution.

Returns:
The number of values added

isUnique

public boolean isUnique()
Checks to see if the sudoku is unique by using the brute force solver several times and comparing the complete grids

Returns:
true if this has a unique solution. False if the solution is not unique or not possible

isComplete

public boolean isComplete()
Checks to see if the sudoku is complete

Returns:
true if all cells have been resolved and all of the values are legal.

isLegal

public boolean isLegal()
Checks all resolved values to check they are legal

Returns:
true if all resolved values are legal

getPossibleValues

public Puzzle.Cell getPossibleValues(java.awt.Point p)
Gets the possible values for this point

Parameters:
p -
Returns:

getKnownValues

public Puzzle.Cell getKnownValues(java.awt.Point p)
Gets the known values from the row, column or block for this point

Parameters:
p -
Returns:

getColumn

public Puzzle.Cell[] getColumn(int i)
Gets the options column

Parameters:
i - the 0 based column number
Returns:

getBlockIndex

public int getBlockIndex(int x,
                         int y)
Gets the options block

Parameters:
x - the 0 based x coordinate
y - the 0 based y coordinate
Returns:

getBlock

public Puzzle.Cell[] getBlock(int x,
                              int y)
Gets the options block

Parameters:
x - the 0 based x coordinate
y - the 0 based y coordinate
Returns:

getBlock

public Puzzle.Cell[] getBlock(int i)
Gets the options block

Parameters:
i - the 0 based block number
Returns:

getLocation

public java.awt.Point getLocation(Puzzle.Cell set)
Gets the zero based x, y coordinates of this set in the grid

Parameters:
set - the set to find
Returns:
the x,y coordinates

getLocationsAsText

public static java.lang.String getLocationsAsText(java.util.List<java.awt.Point> loc)
Gets these locations as string of textual coordinates with letters for the x axis and numbers for the y axis

Parameters:
loc -
Returns:

getNumbersAsText

public static java.lang.String getNumbersAsText(Puzzle.Cell cell)
Gets these numbers as a formatted text string

Parameters:
cell -
Returns:

getNumbersAsText

public static java.lang.String getNumbersAsText(java.util.Collection<java.lang.Integer> removed)
Gets these numbers as a formatted text string

Parameters:
removed -
Returns:

getFrequencyOfPossibilities

public static int[] getFrequencyOfPossibilities(Puzzle.Cell[] sets)
Gets the frequency of possible values. Known values are ignored.

Parameters:
sets - the line of sets to get the frequencies from
Returns:
an array where each index represents the number - 1. The value is the frequency of occurrence of possible values and where - 1 signifies this number is known.

clone

public Puzzle clone()
Overrides:
clone in class java.lang.Object

copyGrid

static Puzzle.Cell[][] copyGrid(Puzzle.Cell[][] grid)
creates an copy of the grid

Parameters:
grid -
Returns:
the copy

copyGrid

static Puzzle.Cell[][] copyGrid(Puzzle.Cell[][] src,
                                Puzzle.Cell[][] dest)
Copies this grid to another grid

Parameters:
grid -
Returns:
the destination

getGrid

public Puzzle.Cell[][] getGrid()
Gets this puzzle grid

Returns:

set

public Puzzle.Cell set(int i,
                       int num)
Sets the value at square denoted by the sequential (1 based) location from top left to bottom right. Cell 1 is at the top left and the cells are contiguously numbered from left to right. At the end of the row the next cell is the leftmost cell on the next row down.

Parameters:
i - the 1 based cell number
num - the number. If the number is not legal ie < 1 or > Sudoku.GRID_SIZE the call is set to all possible values
Returns:
the original value

set

public Puzzle.Cell set(int x,
                       int y,
                       int num)
Sets the value at square denoted by the x, y (0 based) location

Parameters:
x - the 0 based x value
y - the 0 based y value
num - the number
Returns:
the original value

set

public Puzzle.Cell set(int x,
                       int y,
                       Puzzle.Cell nums)
Sets the values at square denoted by the x, y (0 based) location. If the set of numbers is empty then all possible numbers will be used

Parameters:
x - the 0 based x value
y - the 0 based y value
num - the numbers
Returns:
the original value

setGrid

public void setGrid(Puzzle.Cell[][] grid)
Sets the values for each cell of the sudoku grid. A cell with no values will be assigned all possible values

Parameters:
a - 2D array of all possible values for each cell

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

countKnownValues

public int countKnownValues()
Counts the number of known values in this puzzle

Returns:

write

public void write(java.io.PrintWriter w)
Writes this file to the output stream

Parameters:
w - the writer to write to

read

public void read(java.io.BufferedReader r)
          throws java.io.IOException,
                 IllegalGridException
Reads this file from the input stream

Parameters:
r - the reader to read from
Throws:
java.io.IOException
IllegalGridException

setDifficulty

public void setDifficulty(AbstractSolver.SolverDifficulty solver,
                          int factor)
Sets an level indicating how difficult it is to solve this puzzle

Parameters:
solver - the solver difficulty
factor - the difficulty factor

setDifficulty

public void setDifficulty(Puzzle.PuzzleDifficulty dif)
Sets an level indicating how difficult it is to solve this puzzle

Parameters:
dif - the difficulty

getDifficulty

public Puzzle.PuzzleDifficulty getDifficulty()
Gets an indication of how difficult it is to solve this puzzle

Returns:
the difficulty