Browse Source

refactoring of DTSUD-47 into class - added test framework in maven and worte test for setupField and implemented gameField.

remotes/origin/develop
fdai6499 2 years ago
parent
commit
d23bda7a00
  1. 19
      src/test/java/MainTest.java
  2. 74
      sudokuExample/src/GameField.java
  3. 118
      sudokuExample/src/Main.java

19
src/test/java/MainTest.java

@ -9,13 +9,11 @@ class MainTest extends Main {
@org.junit.jupiter.api.Test
void testField() {
JPanel setupGameField = setupGameField(360);
JLabel[][] labelField = getLabelField();
GameField gameField = new GameField(360);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
labelField[i][j].setText(String.valueOf(j));
gameField.setValue(i, j, j);
}
}
@ -23,22 +21,15 @@ class MainTest extends Main {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
String s = labelField[i][j].getText();
try {
Integer integer = Integer.valueOf(s);
if (j != integer) {
a = false;
}
} catch (NumberFormatException e) {
assertTrue(false);
int value = gameField.getValue(i, j);
if (j != value) {
a = false;
}
}
}
assertTrue(a);
}
@org.junit.jupiter.api.Test

74
sudokuExample/src/GameField.java

@ -0,0 +1,74 @@
import javax.swing.*;
import java.awt.*;
public class GameField extends JPanel {
private static JPanel[][] gameField;
private static JLabel[][] labelField;
/**
* represents the game filed of sudoku
* @param widthHeight describes the field size in pixels which will be seperated into single sudoku fields
*/
public GameField(int widthHeight) {
int nrOfFields = 9;
int border = 1;
int size = widthHeight/nrOfFields - (nrOfFields*border)/nrOfFields;
this.setVisible(true);
this.setLayout(null);
gameField = new JPanel[nrOfFields][];
labelField = new JLabel[nrOfFields][];
int gapi = 0, gapj = 0;
for (int i = 0; i < nrOfFields; i++) {
gameField[i] = new JPanel[nrOfFields];
labelField[i] = new JLabel[nrOfFields];
for (int j = 0; j < nrOfFields; j++) {
gameField[i][j] = new JPanel();
gameField[i][j].setBounds(
j * size + border*(j+1),
i * size + border*(i+1),
size, size);
gameField[i][j].setBackground(Color.BLUE);
gameField[i][j].setVisible(true);
this.add(gameField[i][j]);
labelField[i][j] = new JLabel("", SwingConstants.CENTER);
labelField[i][j].setBackground(Color.GREEN);
labelField[i][j].setForeground(Color.MAGENTA);
labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25));
labelField[i][j].setBackground(Color.CYAN);
labelField[i][j].setText("0");
gameField[i][j].add(labelField[i][j]);
}
}
}
public void setValue(int x, int y, int value) {
if (value > 0 && value < 10) {
if (x > -1 && x < 9 && y > -1 && y < 9) {
labelField[x][y].setText(String.valueOf(value));
}
}
}
public int getValue(int x, int y) {
if (x > -1 && x < 9 && y > -1 && y < 9) {
Integer value = Integer.valueOf(labelField[x][y].getText());
return value;
}
return -1;
}
}

118
sudokuExample/src/Main.java

@ -6,16 +6,16 @@ import java.awt.event.ActionListener;
public class Main {
private static JPanel[][] gameField;
private static JLabel[][] labelField;
public static JPanel[][] getGameField() {
return gameField;
}
public static JLabel[][] getLabelField() {
return labelField;
}
// private static JPanel[][] gameField;
// private static JLabel[][] labelField;
//
// public static JPanel[][] getGameField() {
// return gameField;
// }
//
// public static JLabel[][] getLabelField() {
// return labelField;
// }
public static void main(String[] args) {
@ -35,9 +35,9 @@ public class Main {
rootPanel.setBackground(Color.LIGHT_GRAY);
rootPanel.setLayout(null);
JPanel gamePanel = setupGameField(360);
gamePanel.setBounds(70, 50, 360, 360);
rootPanel.add(gamePanel);
GameField gameField = new GameField(360);
gameField.setBounds(70, 50, 360, 360);
rootPanel.add(gameField);
main.setContentPane(rootPanel);
@ -57,52 +57,52 @@ public class Main {
});
}
protected static JPanel setupGameField(int widthHeight) {
int nrOfFields = 9;
int border = 1;
int size = widthHeight/nrOfFields - (nrOfFields*border)/nrOfFields;
JPanel fieldRoot = new JPanel();
fieldRoot.setVisible(true);
fieldRoot.setLayout(null);
gameField = new JPanel[nrOfFields][];
labelField = new JLabel[nrOfFields][];
int gapi = 0, gapj = 0;
for (int i = 0; i < nrOfFields; i++) {
gameField[i] = new JPanel[nrOfFields];
labelField[i] = new JLabel[nrOfFields];
for (int j = 0; j < nrOfFields; j++) {
gameField[i][j] = new JPanel();
gameField[i][j].setBounds(
j * size + border*(j+1),
i * size + border*(i+1),
size, size);
gameField[i][j].setBackground(Color.BLUE);
gameField[i][j].setVisible(true);
fieldRoot.add(gameField[i][j]);
labelField[i][j] = new JLabel("", SwingConstants.CENTER);
labelField[i][j].setBackground(Color.GREEN);
labelField[i][j].setForeground(Color.MAGENTA);
labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25));
labelField[i][j].setBackground(Color.CYAN);
labelField[i][j].setText("0");
gameField[i][j].add(labelField[i][j]);
}
}
return fieldRoot;
}
// protected static JPanel setupGameField(int widthHeight) {
//
// int nrOfFields = 9;
// int border = 1;
// int size = widthHeight/nrOfFields - (nrOfFields*border)/nrOfFields;
//
// JPanel fieldRoot = new JPanel();
// fieldRoot.setVisible(true);
// fieldRoot.setLayout(null);
//
// gameField = new JPanel[nrOfFields][];
// labelField = new JLabel[nrOfFields][];
//
// int gapi = 0, gapj = 0;
//
// for (int i = 0; i < nrOfFields; i++) {
//
// gameField[i] = new JPanel[nrOfFields];
// labelField[i] = new JLabel[nrOfFields];
//
// for (int j = 0; j < nrOfFields; j++) {
//
// gameField[i][j] = new JPanel();
// gameField[i][j].setBounds(
// j * size + border*(j+1),
// i * size + border*(i+1),
// size, size);
// gameField[i][j].setBackground(Color.BLUE);
// gameField[i][j].setVisible(true);
// fieldRoot.add(gameField[i][j]);
//
// labelField[i][j] = new JLabel("", SwingConstants.CENTER);
// labelField[i][j].setBackground(Color.GREEN);
// labelField[i][j].setForeground(Color.MAGENTA);
// labelField[i][j].setFont(new Font("Times new Roman", Font.BOLD, 25));
//
// labelField[i][j].setBackground(Color.CYAN);
// labelField[i][j].setText("0");
//
// gameField[i][j].add(labelField[i][j]);
//
// }
// }
//
// return fieldRoot;
// }
}
// JFrame jFrame = new JFrame();

Loading…
Cancel
Save