Browse Source

tictactoe: number of fields added to the gui

tictactoe
Malte Schellhardt 2 years ago
committed by Lorenz Hohmann
parent
commit
d6ce3f3efa
  1. 15
      src/main/java/de/tims/tictactoe/GameLogic.java
  2. 18
      src/test/java/de/tims/tictactoe/GameLogicTest.java

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

@ -1,5 +1,9 @@
package de.tims.tictactoe;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
public class GameLogic {
@ -97,8 +101,15 @@ public class GameLogic {
}
public JPanel generateGUI() {
// TODO Auto-generated method stub
return new JPanel();
JButton[] fields = new JButton[(int) Math.pow(this.board.length, 2)];
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(this.board.length, this.board.length));
for (int i = 0; i < fields.length; i++) {
fields[i] = new JButton();
contentPanel.add(fields[i]);
}
return contentPanel;
}
}

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

@ -3,8 +3,11 @@ package de.tims.tictactoe;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.awt.Component;
import java.util.stream.Stream;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.junit.jupiter.api.BeforeAll;
@ -63,6 +66,21 @@ class GameLogicTest {
assertEquals(expectedResult.getClass(), realResult.getClass());
}
@Test
void numberOfGUIFieldsTest() {
int realResult = 0;
int expectedResult = (int) Math.pow(SIZE, 2);
JPanel gui = game.generateGUI();
Component[] components = gui.getComponents();
for (Component component : components) {
if (component instanceof JButton) realResult++;
}
assertEquals(expectedResult, realResult);
}
@ParameterizedTest(name = "[{index}] {0} -> {2} fields")
@MethodSource("testCasesForCountPlayfields")
void fieldCountTest(String testName, int size, int expectedResult) {

Loading…
Cancel
Save