Browse Source

tictactoe: refactored gui playfields and related test cases

tictactoe
Malte Schellhardt 3 years ago
committed by Lorenz Hohmann
parent
commit
0b3e24bb55
  1. 25
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 31
      src/test/java/de/tims/tictactoe/GameLogicTest.java

25
src/main/java/de/tims/tictactoe/GameLogic.java

@ -14,7 +14,7 @@ public class GameLogic implements ActionListener {
private char[][] board;
private final char[] occupiedFields = { PLAYER_1, PLAYER_2 };
private JButton[] fields;
private JButton[][] fields;
private JPanel contentPanel;
public GameLogic(int size) {
@ -105,26 +105,33 @@ public class GameLogic implements ActionListener {
}
public JPanel generateGUI() {
this.fields = new JButton[(int) Math.pow(this.board.length, 2)];
this.fields = new JButton[this.board.length][this.board.length];
this.contentPanel = new JPanel();
this.contentPanel.setLayout(new GridLayout(this.board.length, this.board.length));
for (int i = 0; i < this.fields.length; i++) {
this.fields[i] = new JButton();
this.fields[i].addActionListener(this);
this.contentPanel.add(this.fields[i]);
for (int j = 0; j < this.fields.length; j++) {
this.fields[i][j] = new JButton();
this.fields[i][j].addActionListener(this);
this.contentPanel.add(this.fields[i][j]);
}
}
return this.contentPanel;
}
public JButton getGUIField(int number) {
// TODO Auto-generated method stub
return this.fields[number];
public JButton getGUIField(int column, int row) {
return this.fields[column][row];
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
for (int i = 0; i < this.fields.length; i++) {
for (int j = 0; j < this.fields[0].length; j++) {
if (e.getSource() == this.fields[i][j]) {
this.fields[i][j].setText("clicked");
}
}
}
}
}

31
src/test/java/de/tims/tictactoe/GameLogicTest.java

@ -81,18 +81,6 @@ class GameLogicTest {
assertEquals(expectedResult, realResult);
}
@Test
void buttonStateTest() {
boolean expectedResult = true;
game.generateGUI();
JButton currentField = game.getGUIField(0);
((JButton) currentField).doClick();
boolean realResult = currentField.isEnabled();
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0} -> {2} fields")
@MethodSource("testCasesForCountPlayfields")
void fieldCountTest(String testName, int size, int expectedResult) {
@ -136,6 +124,18 @@ class GameLogicTest {
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0}: should be {1}")
@MethodSource("testCasesForCheckButtonState")
void buttonStateTest(String testName, boolean expectedResult, boolean doClick, int column, int row) {
game.generateGUI();
JButton currentField = game.getGUIField(column, row);
if (doClick) currentField.doClick();
boolean realResult = !currentField.getText().isEmpty();
assertEquals(expectedResult, realResult);
}
private static Stream<Arguments> testCasesForCountPlayfields() {
return Stream.of(
Arguments.of("1x1 board with too few fields", 1, 9),
@ -285,5 +285,12 @@ class GameLogicTest {
{'x', 'o', 'x'}})
);
}
private static Stream<Arguments> testCasesForCheckButtonState() {
return Stream.of(
Arguments.of("trigger gui field [0][0]", true, true, 0, 0),
Arguments.of("dont't trigger gui field [1][1]", false, false, 1, 1)
);
}
}
Loading…
Cancel
Save