2020-02-13 19:46:00 +01:00
|
|
|
/*
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-05 15:01:40 +01:00
|
|
|
package de.itsblue.ConnectFour;
|
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
import java.awt.*;
|
|
|
|
import javax.swing.*;
|
2020-02-05 15:01:40 +01:00
|
|
|
|
2020-02-13 19:24:32 +01:00
|
|
|
import de.itsblue.ConnectFour.Plate.PlateType;
|
2020-02-24 22:54:37 +01:00
|
|
|
import de.itsblue.ConnectFour.player.Player;
|
|
|
|
import de.itsblue.ConnectFour.player.LocalPlayer;
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
public class ConnectFour extends JFrame {
|
2020-02-05 15:01:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2020-02-17 16:55:21 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2020-02-22 09:29:52 +01:00
|
|
|
private GameBoard gameBoard;
|
|
|
|
|
|
|
|
private ButtonRow buttonRow;
|
|
|
|
|
2020-02-24 16:12:02 +01:00
|
|
|
private int player = 0;
|
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
private Player players[] = new Player[2];
|
|
|
|
|
|
|
|
enum GameType {
|
|
|
|
Local,
|
|
|
|
RemoteServer,
|
|
|
|
RemoteClient
|
|
|
|
}
|
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2020-02-05 15:01:40 +01:00
|
|
|
ConnectFour() {
|
2020-02-11 21:46:24 +01:00
|
|
|
// initialize window
|
|
|
|
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
2020-02-12 15:30:59 +01:00
|
|
|
this.setTitle("Connect 4");
|
2020-02-17 16:55:21 +01:00
|
|
|
|
2020-02-11 21:46:24 +01:00
|
|
|
this.setPreferredSize(new Dimension(600, 600));
|
2020-02-05 15:29:52 +01:00
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
// initialize layout
|
2020-02-14 19:21:49 +01:00
|
|
|
this.getContentPane().setLayout(new GridBagLayout());
|
2020-02-21 15:48:56 +01:00
|
|
|
GridBagConstraints c = new GridBagConstraints();
|
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
// initialize GameBoard
|
2020-02-22 09:29:52 +01:00
|
|
|
this.gameBoard = new GameBoard();
|
2020-02-21 15:48:56 +01:00
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
// initialize ButtonRow
|
|
|
|
this.buttonRow = new ButtonRow(this.gameBoard.getColumns());
|
|
|
|
|
|
|
|
// add components to window
|
2020-02-22 09:29:52 +01:00
|
|
|
c.gridy = 0;
|
|
|
|
add(buttonRow, c);
|
2020-02-21 15:48:56 +01:00
|
|
|
|
2020-02-22 09:29:52 +01:00
|
|
|
c.gridy = 1;
|
|
|
|
this.add(gameBoard, c);
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-05 15:01:40 +01:00
|
|
|
// finish up
|
|
|
|
this.pack();
|
|
|
|
this.setVisible(true);
|
2020-02-22 09:29:52 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
this.startNewGame(GameType.Local);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startNewGame(GameType type) {
|
|
|
|
switch (type) {
|
|
|
|
case Local: {
|
|
|
|
this.players[0] = new LocalPlayer(this, this.buttonRow, PlateType.O);
|
|
|
|
this.players[1] = new LocalPlayer(this, this.buttonRow, PlateType.X);
|
|
|
|
|
|
|
|
this.player = 0;
|
|
|
|
this.players[player].setIsMyTurn(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-22 09:29:52 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
/**
|
|
|
|
* Function to handle the next plate insertion
|
|
|
|
*
|
|
|
|
* @param column The column to insert the plate into
|
|
|
|
*/
|
2020-02-24 22:54:37 +01:00
|
|
|
public void insertNextPlate(int column) {
|
2020-02-24 16:12:02 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
String res;
|
2020-02-22 10:40:38 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
res = this.gameBoard.insertPlate(new Plate(this.players[player].usingPlateType), column);
|
2020-02-24 16:12:02 +01:00
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-02-24 16:12:02 +01:00
|
|
|
|
|
|
|
switchPlayer();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to switch the player
|
|
|
|
*/
|
|
|
|
private void switchPlayer() {
|
2020-02-24 22:54:37 +01:00
|
|
|
this.players[player].setIsMyTurn(false);
|
|
|
|
|
2020-02-24 16:12:02 +01:00
|
|
|
if (player == 0) {
|
|
|
|
player = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
player = 0;
|
|
|
|
}
|
2020-02-24 22:54:37 +01:00
|
|
|
|
|
|
|
this.players[player].setIsMyTurn(true);
|
2020-02-22 10:40:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to check if the window is in landscape or portrait mode
|
|
|
|
*
|
|
|
|
* @return <code>true</code> if window is in landscape mode, <code>false</code>
|
|
|
|
* if it is not
|
|
|
|
*/
|
2020-02-22 09:29:52 +01:00
|
|
|
public boolean landscape() {
|
|
|
|
return this.getSize().height < this.getSize().width;
|
|
|
|
}
|
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
/**
|
|
|
|
* Override validate in order to resacle the components when the window is
|
|
|
|
* rescaled
|
|
|
|
*/
|
2020-02-22 09:29:52 +01:00
|
|
|
@Override
|
|
|
|
public void validate() {
|
2020-02-22 10:40:38 +01:00
|
|
|
// determine optimal size for board
|
2020-02-22 09:29:52 +01:00
|
|
|
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;
|
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
// set board size
|
2020-02-22 09:29:52 +01:00
|
|
|
this.gameBoard.setPreferredSize(boardSize);
|
2020-02-22 10:40:38 +01:00
|
|
|
// set button row size
|
2020-02-22 09:29:52 +01:00
|
|
|
this.buttonRow.setPreferredSize(
|
|
|
|
new Dimension(this.gameBoard.getPreferredSize().width, this.gameBoard.getPreferredSize().height / 7));
|
2020-02-22 10:40:38 +01:00
|
|
|
|
|
|
|
// call super
|
2020-02-22 09:29:52 +01:00
|
|
|
super.validate();
|
2020-02-05 15:01:40 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:29:52 +01:00
|
|
|
public static void main(final String[] args) {
|
2020-02-05 15:01:40 +01:00
|
|
|
System.out.println(new ConnectFour());
|
|
|
|
}
|
|
|
|
}
|