package de.tims.tictactoe; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; public class GameLogic implements ActionListener { private static final char EMPTY_FIELD = '-'; private static final char PLAYER_1 = 'x'; private static final char PLAYER_2 = 'o'; private char[][] board; private final char[] occupiedFields = { PLAYER_1, PLAYER_2 }; private char currentPlayer = PLAYER_1; private boolean gui = false; private JButton[][] fields; private JPanel contentPanel; public GameLogic(int size) { if (size < 3) { size = 3; } this.board = new char[size][size]; this.resetBoard(); } public GameLogic(char[][] board) { this.board = board; } public char[][] getBoard() { return this.board; } public int countFields() { return this.board[0].length * this.board.length; } public void setField(int column, int row, char player) { if (this.fieldIsEmpty(column, row)) { this.board[column][row] = player; if (gui) { this.fields[column][row].setText("" + this.getCurrentPlayer()); this.updateGUI(); } } } public boolean fieldIsEmpty(int column, int row) { for (char field : this.occupiedFields) { if (this.board[column][row] == field) return false; } return true; } public boolean checkForWin(char player) { boolean won = false; int countFields = 0; // check columns for (int i = 0; i < this.board.length; i++) { for (int j = 0; j < this.board[0].length; j++) { if (this.board[j][i] == player) countFields++; } if (countFields == this.board.length) return won = true; countFields = 0; } // check rows for (int i = 0; i < this.board[0].length; i++) { for (int j = 0; j < this.board.length; j++) { if (this.board[i][j] == player) countFields++; } if (countFields == this.board.length) return won = true; countFields = 0; } // check diagonal left for (int i = this.board.length - 1, j = this.board.length - 1; i >= 0; i--, j--) { if (this.board[i][j] == player) countFields++; } if (countFields == this.board.length) return won = true; countFields = 0; // check diagonal right for (int i = this.board.length - 1, j = 0; i >= 0; i--, j++) { if (this.board[i][j] == player) countFields++; } if (countFields == this.board.length) return won = true; return won; } public boolean checkEndOfGame() { return this.checkForWin(PLAYER_1) || this.checkForWin(PLAYER_2); } public char getCurrentPlayer() { return this.currentPlayer; } public void switchPlayer() { this.currentPlayer = this.currentPlayer == PLAYER_1 ? PLAYER_2 : PLAYER_1; } public void resetBoard() { for (int i = 0; i < this.board.length; i++) { for (int j = 0; j < this.board.length; j++) { this.board[i][j] = EMPTY_FIELD; } } } public JPanel generateGUI() { this.fields = new JButton[this.board.length][this.board.length]; this.contentPanel = new JPanel(); this.contentPanel.setLayout(new GridLayout(this.board.length, this.board.length)); for (int i = 0; i < this.fields.length; i++) { for (int j = 0; j < this.fields.length; j++) { this.fields[i][j] = new JButton(); this.fields[i][j].addActionListener(this); this.contentPanel.add(this.fields[i][j]); } } this.gui = true; return this.contentPanel; } public JButton getGUIField(int column, int row) { return this.fields[column][row]; } private void updateGUI() { if (this.checkEndOfGame()) { for (int i = 0; i < this.fields.length; i++) { for (int j = 0; j < this.fields.length; j++) { this.fields[i][j].setEnabled(false); } } JOptionPane.showMessageDialog(contentPanel, "Spieler " + this.currentPlayer + " hat gewonnen."); this.resetGUI(); } this.switchPlayer(); } private void resetGUI() { for (int i = 0; i < this.fields.length; i++) { for (int j = 0; j < this.fields.length; j++) { this.resetBoard(); this.fields[i][j].setText(""); this.fields[i][j].setEnabled(true); } } this.switchPlayer(); } @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < this.fields.length; i++) { for (int j = 0; j < this.fields[0].length; j++) { if (e.getSource() == this.fields[i][j]) { this.setField(i, j, currentPlayer); this.fields[i][j].getText(); } } } } }