diff --git a/src/test/java/GameFieldTest.java b/src/test/java/GameFieldTest.java new file mode 100644 index 0000000..fb6b74a --- /dev/null +++ b/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); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/NotesTest.java b/src/test/java/NotesTest.java new file mode 100644 index 0000000..9f38fac --- /dev/null +++ b/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); + } +} \ No newline at end of file diff --git a/sudokuExample/src/GameField.java b/sudokuExample/src/GameField.java index e5d8162..f57aebe 100644 --- a/sudokuExample/src/GameField.java +++ b/sudokuExample/src/GameField.java @@ -1,10 +1,24 @@ import javax.swing.*; +import javax.swing.border.LineBorder; import java.awt.*; 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 @@ -21,6 +35,7 @@ public class GameField extends JPanel { gameField = new JPanel[nrOfFields][]; labelField = new JLabel[nrOfFields][]; + noteField = new Notes[nrOfFields][]; int gapi = 0, gapj = 0; @@ -28,10 +43,13 @@ public class GameField extends JPanel { gameField[i] = new JPanel[nrOfFields]; labelField[i] = new JLabel[nrOfFields]; + noteField[i] = new Notes[nrOfFields]; for (int j = 0; j < nrOfFields; j++) { gameField[i][j] = new JPanel(); + gameField[i][j].setBorder(new LineBorder(Color.BLACK, 2)); + gameField[i][j].setLayout(null); gameField[i][j].setBounds( j * size + border*(j+1), i * size + border*(i+1), @@ -40,9 +58,15 @@ public class GameField extends JPanel { gameField[i][j].setVisible(true); 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].setBounds(0, 0, size, size); labelField[i][j].setBackground(Color.GREEN); 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].setBackground(Color.CYAN); @@ -58,17 +82,41 @@ public class GameField extends JPanel { if (value > 0 && value < 10) { 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) { 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 -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); + + } + } } diff --git a/sudokuExample/src/Main.java b/sudokuExample/src/Main.java index da20e7a..752183a 100644 --- a/sudokuExample/src/Main.java +++ b/sudokuExample/src/Main.java @@ -1,5 +1,4 @@ import javax.swing.*; -import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; diff --git a/sudokuExample/src/Notes.java b/sudokuExample/src/Notes.java new file mode 100644 index 0000000..8faab85 --- /dev/null +++ b/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); + } + } +}