From d6ce3f3efa49b60840a4dfbc42544b2a7f5ec71b Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 21:27:58 +0100 Subject: [PATCH] tictactoe: number of fields added to the gui --- src/main/java/de/tims/tictactoe/GameLogic.java | 15 +++++++++++++-- .../java/de/tims/tictactoe/GameLogicTest.java | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index 14de497..a1881d0 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/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; } } diff --git a/src/test/java/de/tims/tictactoe/GameLogicTest.java b/src/test/java/de/tims/tictactoe/GameLogicTest.java index a9fb9bd..19ab25a 100644 --- a/src/test/java/de/tims/tictactoe/GameLogicTest.java +++ b/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) {