This repository has been archived on 2022-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
connect-four/src/de/itsblue/ConnectFour/ConnectFour.java

221 lines
6.6 KiB
Java
Raw Normal View History

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;
import java.awt.*;
import java.io.Serializable;
import javax.swing.*;
2020-02-05 15:01:40 +01:00
import de.itsblue.ConnectFour.Plate.PlateType;
import de.itsblue.ConnectFour.player.Player;
import de.itsblue.ConnectFour.player.*;
public class ConnectFour extends JFrame implements PlayerMoveListener {
2020-02-05 15:01:40 +01:00
/**
*
*/
2020-02-17 16:55:21 +01:00
private static final long serialVersionUID = 1L;
private GameBoard gameBoard;
private ButtonRow buttonRow;
private ControllRow controllRow;
2020-02-24 16:12:02 +01:00
private int player = 0;
private Player players[] = new Player[2];
private GameType gameType;
enum GameType {
Local, RemoteServer, RemoteClient
}
/**
* Constructor
*/
2020-02-05 15:01:40 +01:00
ConnectFour() {
// initialize window
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Connect 4");
2020-02-17 16:55:21 +01:00
this.setPreferredSize(new Dimension(600, 600));
2020-02-05 15:29:52 +01:00
// initialize layout
this.getContentPane().setLayout(new GridBagLayout());
2020-02-21 15:48:56 +01:00
GridBagConstraints c = new GridBagConstraints();
// initialize GameBoard
this.gameBoard = new GameBoard();
2020-02-21 15:48:56 +01:00
// initialize ButtonRow
this.buttonRow = new ButtonRow(this.gameBoard.getColumns());
// initialize ContollRow
this.controllRow = new ControllRow(this.gameBoard.getColumns());
// add components to window
c.gridx = 0;
c.gridy = 0;
add(buttonRow, c);
2020-02-21 15:48:56 +01:00
c.gridx = 13;
c.gridy = 1;
add(controllRow, c);
c.gridx = 0;
c.gridy = 1;
this.add(gameBoard, c);
2020-02-05 15:01:40 +01:00
// finish up
this.pack();
this.setVisible(true);
}
public void startNewGame(GameType type) {
this.gameType = type;
switch (type) {
case Local: {
this.players[0] = new LocalPlayer(this.buttonRow, PlateType.O);
this.players[0].addMoveListener(this);
this.players[1] = new LocalPlayer(this.buttonRow, PlateType.X);
this.players[1].addMoveListener(this);
this.player = 0;
this.players[player].setIsMyTurn(true);
break;
}
case RemoteClient: {
this.players[0] = new LocalPlayer(this.buttonRow, null);
this.players[1] = new RemotePlayerClient(this.buttonRow, "localhost", this.players[0]);
this.players[0].addMoveListener(this);
this.players[1].addMoveListener(this);
break;
}
2020-02-24 16:12:02 +01:00
case RemoteServer: {
this.players[0] = new LocalPlayer(this.buttonRow, PlateType.O);
this.players[1] = new RemotePlayerServer(this.buttonRow, PlateType.X, this.players[0]);
this.players[0].addMoveListener(this);
this.players[1].addMoveListener(this);
this.player = 0;
this.players[player].setIsMyTurn(true);
this.players[1].setIsMyTurn(false);
2020-02-24 16:12:02 +01:00
break;
}
}
2020-02-24 16:12:02 +01:00
}
/**
* Function to switch the player
*/
private void switchPlayer() {
this.players[player].setIsMyTurn(false);
2020-02-24 16:12:02 +01:00
if (player == 0) {
player = 1;
} else {
2020-02-24 16:12:02 +01:00
player = 0;
}
this.players[player].setIsMyTurn(true);
}
/**
* 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
*/
public boolean landscape() {
return this.getSize().height < this.getSize().width;
}
/**
* Catch player moves
*/
@Override
public void movePerformed(int column, Player src) {
if (this.players[this.player].equals(src) || this.gameType == GameType.RemoteClient) {
String res;
res = this.gameBoard.insertPlate(new Plate(src.usingPlateType), 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);
}
if (this.gameType != GameType.RemoteClient)
switchPlayer();
}
}
/**
* 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));
// set button column size
this.controllRow.setPreferredSize(
new Dimension(this.gameBoard.getPreferredSize().width / 4, this.gameBoard.getPreferredSize().height));
// call super
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) {
ConnectFour game;
System.out.println(game = new ConnectFour());
System.out.println("argc: " + args.length);
System.out.println("args[0]: " + args[0]);
if (args.length <= 0)
game.startNewGame(GameType.Local);
else if (args[0].equals("server"))
game.startNewGame(GameType.RemoteServer);
else if (args[0].equals("client"))
game.startNewGame(GameType.RemoteClient);
else
System.out.println("Usage: java ConnectFour.java [server|client]");
2020-02-05 15:01:40 +01:00
}
}