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.*;
|
2020-02-25 11:17:17 +01:00
|
|
|
import java.awt.event.*;
|
|
|
|
import java.io.IOException;
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-14 19:21:49 +01:00
|
|
|
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;
|
2020-02-25 02:10:57 +01:00
|
|
|
import de.itsblue.ConnectFour.player.*;
|
2020-02-12 15:01:51 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
public class ConnectFour extends JFrame implements ActionListener {
|
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-25 10:44:24 +01:00
|
|
|
private ControllRow controllRow;
|
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
private JLabel statusLabel;
|
|
|
|
|
|
|
|
private int currentPlayer = -1;
|
|
|
|
private int winnerPlayer = -1;
|
2020-02-24 16:12:02 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
private Player players[] = new Player[2];
|
|
|
|
|
2020-02-25 02:10:57 +01:00
|
|
|
private GameType gameType;
|
2020-03-04 14:19:37 +01:00
|
|
|
|
|
|
|
String serverIp;
|
2020-02-25 02:10:57 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
enum GameType {
|
2020-02-25 10:44:24 +01:00
|
|
|
Local, RemoteServer, RemoteClient
|
2020-02-24 22:54:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
private GameState gameState = GameState.Idle;
|
|
|
|
|
|
|
|
enum GameState {
|
|
|
|
Idle, Waiting, Running, Over
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:34:07 +01:00
|
|
|
enum GameOverReason {
|
|
|
|
Winner, Draw, OpponentDisconnected, Error
|
|
|
|
}
|
|
|
|
|
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-25 11:17:17 +01:00
|
|
|
this.gameBoard.addActionListener(this);
|
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());
|
|
|
|
|
2020-02-25 10:44:24 +01:00
|
|
|
// initialize ContollRow
|
|
|
|
this.controllRow = new ControllRow(this.gameBoard.getColumns());
|
2020-02-25 11:17:17 +01:00
|
|
|
this.statusLabel = new JLabel();
|
2020-02-25 10:44:24 +01:00
|
|
|
|
2020-02-22 10:40:38 +01:00
|
|
|
// add components to window
|
2020-02-25 10:44:24 +01:00
|
|
|
c.gridx = 0;
|
2020-02-22 09:29:52 +01:00
|
|
|
c.gridy = 0;
|
|
|
|
add(buttonRow, c);
|
2020-02-21 15:48:56 +01:00
|
|
|
|
2020-02-25 10:44:24 +01:00
|
|
|
c.gridx = 13;
|
|
|
|
c.gridy = 1;
|
|
|
|
add(controllRow, c);
|
|
|
|
|
|
|
|
c.gridx = 0;
|
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-25 11:17:17 +01:00
|
|
|
c.gridy = 2;
|
|
|
|
this.add(this.statusLabel, c);
|
|
|
|
|
2020-02-05 15:01:40 +01:00
|
|
|
// finish up
|
|
|
|
this.pack();
|
|
|
|
this.setVisible(true);
|
2020-02-24 22:54:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void startNewGame(GameType type) {
|
2020-02-25 11:17:17 +01:00
|
|
|
if (this.gameState != GameState.Idle)
|
|
|
|
return;
|
|
|
|
|
2020-02-25 02:10:57 +01:00
|
|
|
this.gameType = type;
|
2020-02-25 11:17:17 +01:00
|
|
|
|
|
|
|
this.setGameState(GameState.Waiting);
|
2020-02-24 22:54:37 +01:00
|
|
|
switch (type) {
|
|
|
|
case Local: {
|
2020-02-25 02:10:57 +01:00
|
|
|
this.players[0] = new LocalPlayer(this.buttonRow, PlateType.O);
|
|
|
|
this.players[1] = new LocalPlayer(this.buttonRow, PlateType.X);
|
2020-02-25 10:44:24 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
this.players[0].addActionListener(this);
|
|
|
|
this.players[1].addActionListener(this);
|
|
|
|
|
|
|
|
this.currentPlayer = 0;
|
|
|
|
this.players[this.currentPlayer].setIsMyTurn(true);
|
2020-02-24 22:54:37 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-02-25 02:10:57 +01:00
|
|
|
|
|
|
|
case RemoteClient: {
|
|
|
|
this.players[0] = new LocalPlayer(this.buttonRow, null);
|
2020-02-25 10:44:24 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
try {
|
2020-03-04 14:19:37 +01:00
|
|
|
this.players[1] = new RemotePlayerClient(this.buttonRow, this.serverIp, this.players[0]);
|
2020-02-25 11:17:17 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:34:07 +01:00
|
|
|
// do not add the listener of the local player here, as the client player will
|
2020-03-04 14:19:37 +01:00
|
|
|
// handle its moves in order to make sure they are legal
|
2020-02-25 11:17:17 +01:00
|
|
|
this.players[1].addActionListener(this);
|
2020-02-25 10:44:24 +01:00
|
|
|
|
2020-02-24 22:54:37 +01:00
|
|
|
break;
|
2020-02-25 02:10:57 +01:00
|
|
|
}
|
2020-02-24 16:12:02 +01:00
|
|
|
|
2020-02-25 02:10:57 +01:00
|
|
|
case RemoteServer: {
|
|
|
|
this.players[0] = new LocalPlayer(this.buttonRow, PlateType.O);
|
2020-02-22 10:40:38 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
try {
|
|
|
|
this.players[1] = new RemotePlayerServer(this.buttonRow, PlateType.X, this.players[0]);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-02-25 10:44:24 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
this.players[0].addActionListener(this);
|
|
|
|
this.players[1].addActionListener(this);
|
|
|
|
|
|
|
|
this.currentPlayer = 0;
|
|
|
|
this.players[0].setIsMyTurn(true);
|
2020-02-25 02:10:57 +01:00
|
|
|
this.players[1].setIsMyTurn(false);
|
2020-02-24 16:12:02 +01:00
|
|
|
|
2020-02-25 02:10:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 11:17:17 +01:00
|
|
|
|
|
|
|
this.setGameState(GameState.Running);
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:34:07 +01:00
|
|
|
private void gameOver(GameOverReason reason, String attributes) {
|
2020-02-25 11:29:23 +01:00
|
|
|
if (!this.gameState.equals(GameState.Running))
|
|
|
|
return;
|
|
|
|
|
2020-02-25 15:34:07 +01:00
|
|
|
if (this.gameType.equals(GameType.RemoteServer))
|
|
|
|
((RemotePlayerServer) this.players[1]).notifyGameOver(reason.toString() + " " + attributes);
|
|
|
|
|
|
|
|
switch (reason) {
|
|
|
|
case Draw:
|
|
|
|
this.winnerPlayer = -1;
|
|
|
|
this.statusLabel.setText("Game over. This was a draw!");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Winner:
|
|
|
|
PlateType winnerPlateType = PlateType.valueOf(attributes);
|
|
|
|
this.winnerPlayer = this.players[0].getUsedPlateType().equals(winnerPlateType) ? 0 : 1;
|
|
|
|
if (!this.gameType.equals(GameType.Local) && this.players[this.winnerPlayer] instanceof LocalPlayer)
|
|
|
|
this.statusLabel.setText("Game over. You won the game!");
|
|
|
|
else
|
|
|
|
this.statusLabel
|
|
|
|
.setText("Game over. " + this.players[this.winnerPlayer].getName() + " won the game!");
|
|
|
|
|
|
|
|
case OpponentDisconnected:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Error:
|
|
|
|
break;
|
2020-02-25 11:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.setGameState(GameState.Over);
|
2020-02-24 16:12:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-25 11:29:23 +01:00
|
|
|
public void resetGame() {
|
|
|
|
if (!this.gameState.equals(GameState.Over))
|
|
|
|
return;
|
|
|
|
|
2020-02-25 15:34:07 +01:00
|
|
|
if(this.gameType.equals(GameType.RemoteServer))
|
|
|
|
((RemotePlayerServer) this.players[1]).notifyGameReset();
|
|
|
|
|
2020-02-25 11:29:23 +01:00
|
|
|
this.players[0] = null;
|
|
|
|
this.players[1] = null;
|
|
|
|
this.currentPlayer = -1;
|
|
|
|
this.winnerPlayer = -1;
|
|
|
|
this.gameBoard.clearBoard();
|
|
|
|
this.setGameState(GameState.Idle);
|
|
|
|
}
|
|
|
|
|
2020-02-24 16:12:02 +01:00
|
|
|
/**
|
2020-02-25 11:17:17 +01:00
|
|
|
* Function to switch the current player
|
2020-02-24 16:12:02 +01:00
|
|
|
*/
|
|
|
|
private void switchPlayer() {
|
2020-02-25 11:17:17 +01:00
|
|
|
this.players[this.currentPlayer].setIsMyTurn(false);
|
2020-02-24 22:54:37 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
if (this.currentPlayer == 0) {
|
|
|
|
this.currentPlayer = 1;
|
2020-02-25 10:44:24 +01:00
|
|
|
} else {
|
2020-02-25 11:17:17 +01:00
|
|
|
this.currentPlayer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.players[this.currentPlayer].setIsMyTurn(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
|
|
|
// catch player moves
|
|
|
|
if (e.getSource() instanceof Player) {
|
|
|
|
Player src = (Player) e.getSource();
|
|
|
|
if (e.getActionCommand().startsWith("doMove")
|
2020-02-25 15:34:07 +01:00
|
|
|
// handle moves
|
2020-02-25 11:17:17 +01:00
|
|
|
&& (this.gameType == GameType.RemoteClient || this.players[this.currentPlayer].equals(src))) {
|
|
|
|
|
|
|
|
boolean res = this.gameBoard.insertPlate(new Plate(src.getUsedPlateType()),
|
|
|
|
Integer.parseInt(e.getActionCommand().split(" ")[1]));
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
// beep in case of error
|
|
|
|
Toolkit.getDefaultToolkit().beep();
|
2020-03-04 14:19:37 +01:00
|
|
|
return;
|
2020-02-25 11:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gameType != GameType.RemoteClient && this.gameState.equals(GameState.Running))
|
|
|
|
switchPlayer();
|
2020-02-25 15:34:07 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
} else if (e.getActionCommand().startsWith("isMyTurnChanged")) {
|
2020-02-25 15:34:07 +01:00
|
|
|
// handle a turn change
|
|
|
|
|
2020-02-25 11:29:23 +01:00
|
|
|
if (e.getActionCommand().split(" ")[1].equals("true")) {
|
|
|
|
if (!this.gameType.equals(GameType.Local) && src instanceof LocalPlayer)
|
|
|
|
this.statusLabel.setText("Running, it's your turn!'");
|
2020-02-25 11:17:17 +01:00
|
|
|
else
|
2020-02-25 11:29:23 +01:00
|
|
|
this.statusLabel.setText("Running, it's " + src.getName() + "'s turn!'");
|
2020-02-25 11:17:17 +01:00
|
|
|
}
|
2020-02-25 15:34:07 +01:00
|
|
|
|
|
|
|
} else if (e.getActionCommand().startsWith("gameReset")) {
|
|
|
|
if(!this.gameState.equals(GameState.Over))
|
|
|
|
this.gameOver(GameOverReason.OpponentDisconnected, "");
|
|
|
|
else
|
|
|
|
this.resetGame();
|
2020-02-25 11:17:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// catch board events
|
|
|
|
if (e.getSource() instanceof GameBoard) {
|
|
|
|
if (e.getActionCommand().startsWith("gameOver")) {
|
2020-02-25 15:34:07 +01:00
|
|
|
if(e.getActionCommand().split(" ")[1].equals("draw"))
|
|
|
|
this.gameOver(GameOverReason.Draw, "");
|
|
|
|
else
|
|
|
|
this.gameOver(GameOverReason.Winner, e.getActionCommand().split(" ")[1]);
|
2020-02-25 11:17:17 +01:00
|
|
|
}
|
2020-02-24 16:12:02 +01:00
|
|
|
}
|
2020-02-25 11:17:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setGameState(GameState newState) {
|
|
|
|
this.gameState = newState;
|
|
|
|
|
|
|
|
switch (this.gameState) {
|
|
|
|
case Idle:
|
|
|
|
this.statusLabel.setText("IDLE");
|
|
|
|
this.buttonRow.setEnabled(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Waiting:
|
|
|
|
this.statusLabel.setText("Waiting for opponent to connect");
|
|
|
|
this.buttonRow.setEnabled(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Running:
|
|
|
|
this.buttonRow.setEnabled(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Over:
|
|
|
|
this.buttonRow.setEnabled(false);
|
|
|
|
break;
|
2020-02-24 22:54:37 +01:00
|
|
|
|
2020-02-25 11:17:17 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
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();
|
2020-02-25 20:07:11 +01:00
|
|
|
boardSize.width = (int)(this.landscape() ? this.getSize().height * 0.7 : this.getSize().width * 0.7);
|
2020-02-22 09:29:52 +01:00
|
|
|
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-25 10:44:24 +01:00
|
|
|
// set button column size
|
|
|
|
this.controllRow.setPreferredSize(
|
|
|
|
new Dimension(this.gameBoard.getPreferredSize().width / 4, this.gameBoard.getPreferredSize().height));
|
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-25 02:10:57 +01:00
|
|
|
ConnectFour game;
|
|
|
|
System.out.println(game = new ConnectFour());
|
|
|
|
|
2020-02-25 10:44:24 +01:00
|
|
|
if (args.length <= 0)
|
|
|
|
game.startNewGame(GameType.Local);
|
|
|
|
else if (args[0].equals("server"))
|
|
|
|
game.startNewGame(GameType.RemoteServer);
|
2020-03-04 14:19:37 +01:00
|
|
|
else if (args[0].equals("client")) {
|
|
|
|
game.serverIp = args[1];
|
2020-02-25 10:44:24 +01:00
|
|
|
game.startNewGame(GameType.RemoteClient);
|
2020-03-04 14:19:37 +01:00
|
|
|
}
|
2020-02-25 10:44:24 +01:00
|
|
|
else
|
|
|
|
System.out.println("Usage: java ConnectFour.java [server|client]");
|
2020-02-05 15:01:40 +01:00
|
|
|
}
|
|
|
|
}
|