/* Connect four - written in java Copyright (C) 2020 Oliver Schappacher and Dorian Zedler This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package de.itsblue.ConnectFour; import java.awt.*; import java.awt.event.*; import javax.swing.*; import de.itsblue.ConnectFour.Plate.PlateType; public class ConnectFour extends JFrame implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private GameBoard gameBoard; private ButtonRow buttonRow; /** * Constructor */ ConnectFour() { // initialize window this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setTitle("Connect 4"); this.setPreferredSize(new Dimension(600, 600)); // initialize layout this.getContentPane().setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); // initialize GameBoard this.gameBoard = new GameBoard(); // initialize ButtonRow this.buttonRow = new ButtonRow(this.gameBoard.getColumns()); buttonRow.addActionListenerToButtons(this); // add components to window c.gridy = 0; add(buttonRow, c); c.gridy = 1; this.add(gameBoard, c); // finish up this.pack(); this.setVisible(true); } /** * Function to handle the next plate insertion * * @param column The column to insert the plate into */ private void insertNextPlate(int column) { // TODO: change player everytime String res = this.gameBoard.insertPlate(new Plate(PlateType.X), column); if (res == "err") { // beep in case of error Toolkit.getDefaultToolkit().beep(); } else if (res != "ok" && res != "err") { PlateType winnerType = PlateType.valueOf(res); System.out.println("A player won: " + winnerType); } } /** * Function to check if the window is in landscape or portrait mode * * @return true if window is in landscape mode, false * if it is not */ public boolean landscape() { return this.getSize().height < this.getSize().width; } /** * Override validate in order to resacle the components when the window is * rescaled */ @Override public void validate() { // determine optimal size for board Dimension boardSize = new Dimension(); boardSize.width = this.landscape() ? this.getSize().height / 2 : this.getSize().width / 2; boardSize.height = this.landscape() ? this.getSize().width : this.getSize().height; // set board size this.gameBoard.setPreferredSize(boardSize); // set button row size this.buttonRow.setPreferredSize( new Dimension(this.gameBoard.getPreferredSize().width, this.gameBoard.getPreferredSize().height / 7)); // call super super.validate(); } /** * Catch the actions from the button row */ @Override public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof PlateInsertButton) { // if the action source was a PlateInsertButton // -> trigger a plate insertion PlateInsertButton source = (PlateInsertButton) e.getSource(); this.insertNextPlate(source.getIndex()); } } public static void main(final String[] args) { System.out.println(new ConnectFour()); } }