From 6ee0ec873ca03747940a4b30bf9307c11f6b0631 Mon Sep 17 00:00:00 2001 From: Steffen Helmke Date: Wed, 2 Feb 2022 15:50:14 +0100 Subject: [PATCH] 4gewinnt: refactored gui elements to enable variable boardsize --- .../java/de/tims/viergewinnt/ai/Logic.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/viergewinnt/ai/Logic.java b/src/main/java/de/tims/viergewinnt/ai/Logic.java index eaa9aca..91b14c5 100644 --- a/src/main/java/de/tims/viergewinnt/ai/Logic.java +++ b/src/main/java/de/tims/viergewinnt/ai/Logic.java @@ -20,6 +20,8 @@ public class Logic { public Logic(int size) { this.board = new int[size][size]; + this.buttons = new JButton[getBoardSize()]; + this.gamefield = new JLabel[getBoardSize() * getBoardSize()]; } public void setField(int player, int row, int column) { @@ -81,6 +83,8 @@ public class Logic { updateGui(); if(testForWin()) { gameover(); + } else if(!checkButtons()) { + gameover(); } setCurrentPlayer((getCurrentPlayer() % 2) + 1); return 0; @@ -97,14 +101,14 @@ public class Logic { JPanel contentPanel; JPanel buttonPanel; JPanel playfieldPanel; - JButton[] buttons = new JButton[6]; - JLabel[] gamefield = new JLabel[36]; + JButton[] buttons; + JLabel[] gamefield; public JPanel create4gewinntGui() { contentPanel = new JPanel(); contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS)); - buttonPanel = new JPanel(new GridLayout(1, 6)); - playfieldPanel = new JPanel(new GridLayout(6, 6)); + buttonPanel = new JPanel(new GridLayout(1, buttons.length)); + playfieldPanel = new JPanel(new GridLayout(buttons.length, buttons.length)); contentPanel.add(buttonPanel); contentPanel.add(playfieldPanel); @@ -129,11 +133,11 @@ public class Logic { for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[i].length; j++) { if(getField(i,j) == 0) { - gamefield[j + 6 * i].setBackground(Color.white); + gamefield[j + getBoardSize() * i].setBackground(Color.white); } else if(getField(i,j) == 1) { - gamefield[j + 6 * i].setBackground(Color.red); + gamefield[j + getBoardSize() * i].setBackground(Color.red); } else { - gamefield[j + 6 * i].setBackground(Color.yellow); + gamefield[j + getBoardSize() * i].setBackground(Color.yellow); } } }