From 82890b25aa3041b1a9bd909176b12d2a43a02801 Mon Sep 17 00:00:00 2001 From: Malte Schellhardt Date: Mon, 14 Feb 2022 21:31:10 +0100 Subject: [PATCH] tictactoe: refactored generateGUI method --- src/main/java/de/tims/tictactoe/GameLogic.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/tictactoe/GameLogic.java b/src/main/java/de/tims/tictactoe/GameLogic.java index a1881d0..1591cd1 100644 --- a/src/main/java/de/tims/tictactoe/GameLogic.java +++ b/src/main/java/de/tims/tictactoe/GameLogic.java @@ -12,6 +12,9 @@ public class GameLogic { private static final char PLAYER_2 = 'o'; private char[][] board; private final char[] occupiedFields = { PLAYER_1, PLAYER_2 }; + + private JButton[] fields; + private JPanel contentPanel; public GameLogic(int size) { if (size < 3) { @@ -101,15 +104,15 @@ public class GameLogic { } public JPanel generateGUI() { - 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)); + this.fields = new JButton[(int) Math.pow(this.board.length, 2)]; + this.contentPanel = new JPanel(); + this.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]); + for (int i = 0; i < this.fields.length; i++) { + this.fields[i] = new JButton(); + this.contentPanel.add(this.fields[i]); } - return contentPanel; + return this.contentPanel; } }