Browse Source

4gewinnt: refactored gui elements to enable variable boardsize

4gewinnt
Steffen Helmke 3 years ago
committed by Lorenz Hohmann
parent
commit
6ee0ec873c
  1. 18
      src/main/java/de/tims/viergewinnt/ai/Logic.java

18
src/main/java/de/tims/viergewinnt/ai/Logic.java

@ -20,6 +20,8 @@ public class Logic {
public Logic(int size) { public Logic(int size) {
this.board = new int[size][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) { public void setField(int player, int row, int column) {
@ -81,6 +83,8 @@ public class Logic {
updateGui(); updateGui();
if(testForWin()) { if(testForWin()) {
gameover(); gameover();
} else if(!checkButtons()) {
gameover();
} }
setCurrentPlayer((getCurrentPlayer() % 2) + 1); setCurrentPlayer((getCurrentPlayer() % 2) + 1);
return 0; return 0;
@ -97,14 +101,14 @@ public class Logic {
JPanel contentPanel; JPanel contentPanel;
JPanel buttonPanel; JPanel buttonPanel;
JPanel playfieldPanel; JPanel playfieldPanel;
JButton[] buttons = new JButton[6];
JLabel[] gamefield = new JLabel[36];
JButton[] buttons;
JLabel[] gamefield;
public JPanel create4gewinntGui() { public JPanel create4gewinntGui() {
contentPanel = new JPanel(); contentPanel = new JPanel();
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS)); 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(buttonPanel);
contentPanel.add(playfieldPanel); contentPanel.add(playfieldPanel);
@ -129,11 +133,11 @@ public class Logic {
for(int i = 0; i < board.length; i++) { for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++) { for(int j = 0; j < board[i].length; j++) {
if(getField(i,j) == 0) { 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) { } else if(getField(i,j) == 1) {
gamefield[j + 6 * i].setBackground(Color.red);
gamefield[j + getBoardSize() * i].setBackground(Color.red);
} else { } else {
gamefield[j + 6 * i].setBackground(Color.yellow);
gamefield[j + getBoardSize() * i].setBackground(Color.yellow);
} }
} }
} }

Loading…
Cancel
Save