Hi Leute,
ich hab ein kleines Problem bei dem ich nicht weiter komme und zwar weiss ich nicht wie ich die update und solve methode aus der Klasse sudoku programmieren soll. Also ich steh am schlauch und weiß nicht wie ich da ran gehen soll.
Klasse Cell
public class Cell {
public int value;
public int[] options;
/**
* Constructs a cell for a Sudoku of given size.
*
* @param size
* the size n of the Sudoku
* @param value
* the value of the cell, may be EMPTY
*
*/
public Cell(int size, int value) {
this.value = value;
this.options = new int[size];
for(int i = 0; i < options.length; i++) {
options** = i + 1;
}
}
/**
* Returns the value of this cell.
*
* @return the value
*
*/
public int getValue() {
return this.value;
}
/**
* Indicates if cell is empty, i.e., has no value.
*
* @return true, if empty
*
*/
public boolean isEmpty() {
return this.value == 0;
}
/**
* Erases the given value from options and returns changes.
*
* @return true, if erase action changed options
*
*/
public boolean erase(int value) {
for (int i = 0; i < this.options.length; i++) {
if (value == options** && this.value == 0) {
options** = 0;
return true;
}
}
return false;
}
/**
* Returns the current number of possible values of this cell.
*
* @return the number of possible values, 0, if the cell has a value
*
*/
public int getOptionCount() {
int count = 0;
if(!this.isEmpty()) {
return 0;
} else {
for(int i = 0; i < options.length; i++) {
if(options** != 0) {
count++;
}
}
}
return count;
}
/**
* Checks options to give this cell a value and returns changes.
*
* @return true, if cell state changed from empty to not empty
*
*/
public boolean update() {
if (this.getOptionCount() == 1) {
for (int i = 0; i < this.options.length; i++) {
if (this.options** != 0) {
this.value = options**;
return true;
}
}
}
return false;
}
}
Klasse Sudoku:
public class Sudoku {
Cell[][] s;
int[][] values;
/**
* Constructs the Sudoku.
*
* @param values
* the unsolved Sudoku (empty cells are 0)
*
*/
public Sudoku(int[][] values) {
this.values = values;
this.s = new Cell[values.length][values.length];
for(int i = 0; i < values.length; i++) {
for(int j = 0; j < values**.length; j++) {
s**[j] = new Cell(values.length, values**[j]);
}
}
}
/**
* Updates a logical part of the Sudoku by erasing known values and updating
* cells.
*
* @return true, if any cell in the part changed (including cell options)
*
*/
public boolean update(Cell[] part) {
int test = 0;
for(int i = 0; i < part.length; i++) {
for(int j = 0; j < this.values.length; j++) {
for(int k = 0; k < this.values[j].length; k++) {
test = values[j][k];
part**.erase(test);
}
}
}
if(test != 0) {
return true;
}
return false;
}
/**
* Returns the string representation of this Sudoku.
*
* @return the Sudoku string
*
*/
public String toString() {
String matrix = "";
for (int i = 0; i < this.s.length; i++) {
for (int j = 0; j < this.s**.length; j++) {
matrix = matrix + " " + this.s**[j].getValue();
}
matrix = matrix + "
";
}
return matrix;
}
/** Solves the Sudoku by updating rows, columns, and sub-grids
* as long as there is no more change. */
public void solve() {
// update(s);
}
}
Wäre nett wenn ihr mir da helfen könnt
lg
Angabe:
*** Edit ***
Keiner einen Tipp für mich?