Browse Source

DTSUD-56

Notizen für ein Spiel feld Ermöglichen + Tests
remotes/origin/develop
fdai6499 2 years ago
parent
commit
89f9b3df0b
  1. 158
      src/test/java/GameFieldTest.java
  2. 76
      src/test/java/NotesTest.java
  3. 56
      sudokuExample/src/GameField.java
  4. 1
      sudokuExample/src/Main.java
  5. 85
      sudokuExample/src/Notes.java

158
src/test/java/GameFieldTest.java

@ -0,0 +1,158 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class GameFieldTest {
@Test
void setValue() {
GameField gameField = new GameField(360);
for (int i = 0, count = 1; i < 9; i++) {
for (int j = 0; j < 9; j++, count += 1) {
gameField.setValue(i, j, count%10);
}
}
boolean a = true;
for (int i = 0, count = 1; i < 9; i++) {
for (int j = 0; j < 9; j++, count += 1) {
String text = gameField.getLabelField()[j][i].getText();
if (!(Integer.valueOf(text) == count%10)) {
a = false;
}
}
}
assertTrue(a);
}
@Test
void getValue() {
GameField gameField = new GameField(360);
for (int i = 0, count = 1; i < 9; i++) {
for (int j = 0; j < 9; j++, count += 1) {
gameField.setValue(i, j, count%10);
}
}
boolean a = true;
for (int i = 0, count = 1; i < 9; i++) {
for (int j = 0; j < 9; j++, count += 1) {
int value = gameField.getValue(i, j);
if (!(value == count%10)) {
a = false;
}
}
}
assertTrue(a);
}
@Test
void setHint() {
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 1; k < 10; k++) {
gameField.setHint(i, j, k);
}
Notes notes = gameField.getNoteField()[i][j];
boolean a = true;
for (int k = 0, count = 1; k < 3; k++) {
for (int l = 0; l < 3; l++, count += 1) {
boolean visible = notes.getNotes()[l][k].isVisible();
if (!visible) {
a = false;
}
}
}
assertTrue(a);
}
}
}
@Test
void unsetHint() {
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 1; k < 10; k++) {
gameField.setHint(i, j, k);
}
for (int k = 1; k < 10; k++) {
gameField.unsetHint(i, j, k);
}
Notes notes = gameField.getNoteField()[i][j];
boolean a = true;
for (int k = 0, count = 1; k < 3; k++) {
for (int l = 0; l < 3; l++, count += 1) {
boolean visible = notes.getNotes()[l][k].isVisible();
if (visible) {
a = false;
}
}
}
assertTrue(a);
}
}
}
@Test
void unsetAllHintsFromField() {
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 1; k < 10; k++) {
gameField.setHint(i, j, k);
}
gameField.unsetAllHintsFromField(i, j);
Notes notes = gameField.getNoteField()[i][j];
boolean a = true;
for (int k = 0, count = 1; k < 3; k++) {
for (int l = 0; l < 3; l++, count += 1) {
boolean visible = notes.getNotes()[l][k].isVisible();
if (visible) {
a = false;
}
}
}
assertTrue(a);
}
}
}
}

76
src/test/java/NotesTest.java

@ -0,0 +1,76 @@
import org.junit.jupiter.api.Test;
import javax.swing.*;
import static org.junit.jupiter.api.Assertions.*;
class NotesTest {
@Test
void setHint() {
Notes notes = new Notes(50);
for (int i = 1; i < 10; i++) {
notes.setHint(i);
}
boolean a = true;
for (int i = 0, count = 1; i < 3; i++) {
for (int j = 0; j < 3; j++, count += 1) {
JLabel jLabel = notes.getNotes()[j][i];
String text = jLabel.getText();
if (!text.equals(String.valueOf(count))) {
a = false;
}
}
}
assertTrue(a);
}
@Test
void isSet() {
Notes notes = new Notes(50);
for (int i = 1; i < 10; i++) {
notes.setHint(i);
}
boolean a = true;
for (int i = 1; i < 10; i++) {
if (!notes.isSet(i)) {
a = false;
}
}
assertTrue(a);
}
@Test
void unsetHint() {
Notes notes = new Notes(50);
for (int i = 1; i < 10; i++) {
notes.setHint(i);
}
boolean a = true;
for (int i = 1; i < 10; i++) {
notes.unsetHint(i);
}
for (int i = 1; i < 10; i++) {
if (notes.isSet(i)) {
a = false;
}
}
assertTrue(a);
}
}

56
sudokuExample/src/GameField.java

@ -1,10 +1,24 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*; import java.awt.*;
public class GameField extends JPanel { public class GameField extends JPanel {
private static JPanel[][] gameField;
private static JLabel[][] labelField;
private Notes[][] noteField;
private JPanel[][] gameField;
private JLabel[][] labelField;
public Notes[][] getNoteField() {
return noteField;
}
public JPanel[][] getGameField() {
return gameField;
}
public JLabel[][] getLabelField() {
return labelField;
}
/** /**
* represents the game filed of sudoku * represents the game filed of sudoku
@ -21,6 +35,7 @@ public class GameField extends JPanel {
gameField = new JPanel[nrOfFields][]; gameField = new JPanel[nrOfFields][];
labelField = new JLabel[nrOfFields][]; labelField = new JLabel[nrOfFields][];
noteField = new Notes[nrOfFields][];
int gapi = 0, gapj = 0; int gapi = 0, gapj = 0;
@ -28,10 +43,13 @@ public class GameField extends JPanel {
gameField[i] = new JPanel[nrOfFields]; gameField[i] = new JPanel[nrOfFields];
labelField[i] = new JLabel[nrOfFields]; labelField[i] = new JLabel[nrOfFields];
noteField[i] = new Notes[nrOfFields];
for (int j = 0; j < nrOfFields; j++) { for (int j = 0; j < nrOfFields; j++) {
gameField[i][j] = new JPanel(); gameField[i][j] = new JPanel();
gameField[i][j].setBorder(new LineBorder(Color.BLACK, 2));
gameField[i][j].setLayout(null);
gameField[i][j].setBounds( gameField[i][j].setBounds(
j * size + border*(j+1), j * size + border*(j+1),
i * size + border*(i+1), i * size + border*(i+1),
@ -40,9 +58,15 @@ public class GameField extends JPanel {
gameField[i][j].setVisible(true); gameField[i][j].setVisible(true);
this.add(gameField[i][j]); this.add(gameField[i][j]);
noteField[i][j] = new Notes(size);
noteField[i][j].setBounds(border, border, size-2*border, size-2*border);
gameField[i][j].add(noteField[i][j]);
labelField[i][j] = new JLabel("", SwingConstants.CENTER); labelField[i][j] = new JLabel("", SwingConstants.CENTER);
labelField[i][j].setBounds(0, 0, size, size);
labelField[i][j].setBackground(Color.GREEN); labelField[i][j].setBackground(Color.GREEN);
labelField[i][j].setForeground(Color.MAGENTA); labelField[i][j].setForeground(Color.MAGENTA);
labelField[i][j].setVisible(false);
labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25)); labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25));
labelField[i][j].setBackground(Color.CYAN); labelField[i][j].setBackground(Color.CYAN);
@ -58,17 +82,41 @@ public class GameField extends JPanel {
if (value > 0 && value < 10) { if (value > 0 && value < 10) {
if (x > -1 && x < 9 && y > -1 && y < 9) { if (x > -1 && x < 9 && y > -1 && y < 9) {
labelField[x][y].setText(String.valueOf(value));
labelField[y][x].setText(String.valueOf(value));
labelField[y][x].setVisible(true);
} }
} }
} }
public int getValue(int x, int y) { public int getValue(int x, int y) {
if (x > -1 && x < 9 && y > -1 && y < 9) { if (x > -1 && x < 9 && y > -1 && y < 9) {
Integer value = Integer.valueOf(labelField[x][y].getText());
Integer value = Integer.valueOf(labelField[y][x].getText());
return value; return value;
} }
return -1; return -1;
} }
public void setHint(int x, int y, int value) {
if (value > 0 && value < 10) {
if (x > -1 && x < 9 && y > -1 && y < 9) {
noteField[x][y].setHint(value);
}
}
}
public void unsetHint(int x, int y, int value) {
if (noteField[x][y].isSet(value)) {
setHint(x, y, value);
}
}
public void unsetAllHintsFromField(int x, int y) {
for (int j = 1; j < 10; j++) {
noteField[x][y].unsetHint(j);
}
}
} }

1
sudokuExample/src/Main.java

@ -1,5 +1,4 @@
import javax.swing.*; import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;

85
sudokuExample/src/Notes.java

@ -0,0 +1,85 @@
import javax.swing.*;
import java.awt.*;
public class Notes extends JPanel {
private JLabel[][] notes;
public JLabel[][] getNotes() {
return notes;
}
public Notes(int size) {
setLayout(null);
int fontSize = 11;
this.setBounds(0, 0, size, size);
notes = new JLabel[3][];
this.setVisible(false);
for (int i = 0; i < 3; i++) {
notes[i] = new JLabel[3];
for (int j = 0; j < 3; j++) {
notes[i][j] = new JLabel("0", SwingConstants.CENTER);
notes[i][j].setBounds(2+i*fontSize, 2+j*fontSize, fontSize, fontSize);
notes[i][j].setFont(new Font("Times new Roman", Font.BOLD, fontSize));
notes[i][j].setVisible(false);
this.add(notes[i][j]);
}
}
for (int i = 0, count = 1; i < 3; i++) {
for (int j = 0; j < 3; j++) {
notes[j][i].setText(String.valueOf(count++));
}
}
}
/**
* shows the value on the notes board
* @param l will represent the value on the sudoku
*/
public void setHint(int l) {
l -= 1;
notes[l%3][l/3].setVisible(!notes[l%3][l/3].isVisible());
boolean a = false;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
boolean visible = notes[j][i].isVisible();
if (visible) {
a = true;
break;
}
}
}
this.setVisible(a);
}
public boolean isSet(int hint) {
hint -= 1;
JLabel jLabel = notes[hint % 3][hint / 3];
boolean set = jLabel.isVisible();
return set;
}
public void unsetHint(int hint) {
if (isSet(hint)) {
setHint(hint);
}
}
}
Loading…
Cancel
Save