Compare commits

...

2 Commits

Author SHA1 Message Date
b90d94bf41 Merge branch 'main' of https://git.sfs.ddnss.org/peet/oop 2024-06-05 00:11:37 +02:00
096a49e0a8 implemented ue06 2024-06-05 00:11:35 +02:00
8 changed files with 231 additions and 0 deletions

BIN
bin/uebung06/Field.class Normal file

Binary file not shown.

BIN
bin/uebung06/Sudoku.class Normal file

Binary file not shown.

Binary file not shown.

BIN
bin/uebung06/VALUE.class Normal file

Binary file not shown.

87
src/uebung06/Field.java Normal file
View File

@ -0,0 +1,87 @@
package uebung06;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class Field {
private Sudoku sudoku;
private int x;
private int y;
private VALUE value = null;
private List<Field> dependents;
private Set<VALUE> domain;
public Field(Sudoku sudoku, int x, int y) {
this.sudoku = sudoku;
this.x = x;
this.y = y;
}
public Sudoku getSudoku() {
return sudoku;
}
public int getx() {
return x;
}
public int gety() {
return y;
}
public VALUE getValue() {
return value;
}
public void setValue(VALUE value) {
this.value = value;
}
public List<Field> getDependents() {
if (dependents == null) {
dependents = calculateDependents();
}
return dependents;
}
public Set<VALUE> getDomainSet() {
return domain;
}
public boolean isEmpty() {
return value == null;
}
@Override
public String toString() {
if (value != null) {
return value.toString();
} else {
return ".";
}
}
//TODO
private List<Field> calculateDependents() {
List<Field> rtn = new ArrayList<>();
//add all the ones in the same col
for (int i = 0; i < 9; i++) {
rtn.add(sudoku.getField(x, i));
}
// //add all the ones in the same row
for (int i = 0; i < 9; i++) {
rtn.add(sudoku.getField(i, y));
}
return rtn;
}
}

66
src/uebung06/Sudoku.java Normal file
View File

@ -0,0 +1,66 @@
package uebung06;
public class Sudoku {
private Field[][] board;
public Sudoku() {
board = new Field[9][9];
createEmptyboard();
}
public Field getField(int x, int y){
return board[x][y];
}
public void setField(int x, int y, VALUE value){
board[x][y].setValue(value);
}
private void createEmptyboard() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9 ; j++) {
board[i][j] = new Field(this, i, j);
}
}
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
str.append(board[i][j].toString() + " ");
if (j == 2 || j == 5) {
str.append("| ");
}
}
str.append("\n");
if (i == 2 || i ==5) {
str.append("------+-------+------\n");
}
}
return str.toString();
}
public void initialize(int[] values) {
int c = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j].setValue(VALUE.of(values[c]));
c++;
}
}
}
public boolean solve() {
return true;
}
}

View File

@ -0,0 +1,28 @@
package uebung06;
public class SudokuApp {
private static Sudoku sud;
public static void main(String[] args) {
sud = new Sudoku();
// sud1.setField(0, 0, VALUE.FOUR);
// sud1.setField(0, 1, VALUE.FIVE);
// sud1.setField(0, 6, VALUE.TWO);
// sud1.setField(0, 0, VALUE.FOUR);
// sud1.setField(0, 0, VALUE.FOUR);
// sud1.setField(0, 0, VALUE.FOUR);
sud.initialize(new int[]{
4, 5, 0, 0, 0, 0, 2, 0, 0,
6, 0, 0, 0, 2, 4, 8, 0, 0,
8, 0, 0, 0, 6, 1, 3, 0, 0,
0, 9, 0, 4, 0, 0, 0, 5, 0,
0, 1, 0, 2, 0, 8, 0, 7, 0,
0, 3, 0, 0, 0, 9, 0, 8, 0,
0, 0, 7, 1, 4, 0, 0, 0, 8,
0, 0, 2, 7, 9, 0, 0, 0, 6,
0, 0, 5, 0, 0, 0, 0, 2, 1});
System.out.println(sud.toString());
}
}

50
src/uebung06/VALUE.java Normal file
View File

@ -0,0 +1,50 @@
package uebung06;
public enum VALUE {
ZERO,
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE;
@Override
public String toString() {
switch (this) {
case ONE: return "1";
case TWO: return "2";
case THREE: return "3";
case FOUR: return "4";
case FIVE: return "5";
case SIX: return "6";
case SEVEN: return "7";
case EIGHT: return "8";
case NINE: return "9";
default:
return "0";
}
}
public static VALUE of(int id) {
switch (id) {
// case 0: return null; handle via default
case 1: return ONE;
case 2: return TWO;
case 3: return THREE;
case 4: return FOUR;
case 5: return FIVE;
case 6: return SIX;
case 7: return SEVEN;
case 8: return EIGHT;
case 9: return NINE;
default:
return ZERO;
}
}
}