public class SudokuEngine
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private int |
gameRowElements
Number of elements in one row or column of the game
|
private byte[][] |
resultArray
Array of the numbers of the solved game ordered as
byte[row][column] . |
private int |
squareRowElements
Number of elements in one row or column of a square
|
private byte[][] |
startArray
Partially filled array with numbers on startup (working array).
|
Constructor and Description |
---|
SudokuEngine() |
Modifier and Type | Method and Description |
---|---|
int |
check(int row,
int col,
int value)
Checks if value exists already in a row, column or square.
|
boolean |
checkForDoubleEntries(int row,
int col)
Checks if there is more than one entry of a specific position at
row|col either in a row or in a column. |
boolean |
checkHorizontal(int row,
int col,
int value)
|
private boolean |
checkRandomEntry(byte[] arr)
Checks if
arr only contains distinct values. |
boolean |
checkSquare(int row,
int col,
int value)
Checks if
value occures in the square to which the element
placed in row/col belongs. |
boolean |
checkVertical(int row,
int col,
int value)
|
private byte[] |
createPrototypeArray()
Create a prototype array with distinct random numbers from 1 to
gameRowElements |
boolean |
createSudoku(int row,
int col)
Computes the solution of the game recursively.
|
byte[][] |
getResultArray() |
byte[][] |
getStartArray() |
int |
getSumOfFieldValues() |
void |
initEngine(int size)
Initialises the size of
squareRowElements and
gameRowElements with the number of elements per row/col/square. |
void |
initFields()
Initialises the array, that will fill the game at startup containing a
bunch of '-1' to display empty fields and the array used to store the
game's solution entries.
|
private boolean |
isFilled(int anz)
Checks if
anz positions of the start array are set to -1. |
void |
preallocate(int anz)
Sets
anz positions of the start array to -1. |
void |
setResultArray(byte[][] arr) |
void |
setStartArray(byte[][] arr) |
void |
setValueToStartArrayPos(int row,
int col,
byte value)
|
private int squareRowElements
private int gameRowElements
private byte[][] startArray
private byte[][] resultArray
byte[row][column]
.public void initEngine(int size)
squareRowElements
and
gameRowElements
with the number of elements per row/col/square.size
- int
number of elements in a row/col/square.public int check(int row, int col, int value)
value
multiple times. Used to show
mistakes.row
- int
, the rowcol
- int
, the columnvalue
- int
, the value to be checked
int
according to the table below
-1 | without match |
0 | horizontal match |
1 | vertical match |
2 | horizontal and vertical match |
3 | match in a square |
4 | horizontal match and match in a square |
5 | vertical match and match in a square |
6 | horizontal and vertical match and match in a square |
public boolean checkHorizontal(int row, int col, int value)
row
- int
col
- int
value
- int
the value to be checkedtrue
if value
occures in row number
row
in addition to the tested position
col
otherwise false
.public boolean checkVertical(int row, int col, int value)
row
- int
col
- int
value
- int
the value to be checkedtrue
if value
exists in
col
otherwise false
.public boolean checkSquare(int row, int col, int value)
value
occures in the square to which the element
placed in row/col
belongs. The comparision is done based on
the startArray
.row
- int
col
- int
value
- int
the value to be checkedtrue
if value
exists in the square
otherwise false
.public int getSumOfFieldValues()
public boolean createSudoku(int row, int col)
gameRowElements
is filled with distinct random numbers. While
checking for its existance in a row, column or square each time, two
identical int arrays are initialized recursively by random numbers.
One array
represents the game's solution,
the other one
will be prepared with -1 at some random
positions, according to the difficulty of the game. These values are
translated to blank spaces displayed on the gui's fields.
startArray
will initialize the game on startup.row
- int
index of the game's rowcol
- int
index of the game's columnboolean
true
if the computing succeeds.private byte[] createPrototypeArray()
gameRowElements
byte[]
the array of numbersprivate boolean checkRandomEntry(byte[] arr)
arr
only contains distinct values.arr
- the array that has to be checked.false
if there are only distinct values in
arr
, otherwise true
.public boolean checkForDoubleEntries(int row, int col)
row|col
either in a row or in a column.row
- int
the rowcol
- int
the columnfalse
if each entry in a row, column or square only
exists one time, otherwise true
.public void preallocate(int anz)
anz
positions of the start array to -1. Their indexes
are calculated randomly. Afterwords the according gui-positions are
filled by blank spaces.anz
- int
the number of fields that have to be emptyprivate boolean isFilled(int anz)
anz
positions of the start array are set to -1.
prüft, ob anz
Felder des Sudokus auf -1 gesetzt sindanz
- int
the number of array positionstrue
if anz
positions are set to -1.public void initFields()
public byte[][] getStartArray()
public void setStartArray(byte[][] arr)
public byte[][] getResultArray()
public void setResultArray(byte[][] arr)
public void setValueToStartArrayPos(int row, int col, byte value)
row
- int
, the rowcol
- int
, the columnvalue
- byte
, value to be set