diff --git a/src/de/itsblue/ConnectFour/ButtonRow.java b/src/de/itsblue/ConnectFour/ButtonRow.java index dce7ec3..b1ed645 100644 --- a/src/de/itsblue/ConnectFour/ButtonRow.java +++ b/src/de/itsblue/ConnectFour/ButtonRow.java @@ -78,6 +78,28 @@ public class ButtonRow extends JPanel { } } + /** + * Function to remove an ActionListener from all contained buttons + * + * @param listener + */ + public void removeActionListemerFromButtons(ActionListener listener) { + for (int i = 0; i < this.buttonCount; i++) { + this.inputButtons[i].removeActionListener(listener); + } + } + + /** + * Function to change the color of all buttons + * + * @param color color to change to + */ + public void setColor(Color color) { + for (PlateInsertButton plateInsertButton : inputButtons) { + plateInsertButton.setBackground(color); + } + } + /** * Override the paint function in order to adjust the button size when rescaling */ diff --git a/src/de/itsblue/ConnectFour/ConnectFour.java b/src/de/itsblue/ConnectFour/ConnectFour.java index 8cdc47b..5556ae7 100644 --- a/src/de/itsblue/ConnectFour/ConnectFour.java +++ b/src/de/itsblue/ConnectFour/ConnectFour.java @@ -19,12 +19,13 @@ package de.itsblue.ConnectFour; import java.awt.*; -import java.awt.event.*; import javax.swing.*; import de.itsblue.ConnectFour.Plate.PlateType; +import de.itsblue.ConnectFour.player.Player; +import de.itsblue.ConnectFour.player.LocalPlayer; -public class ConnectFour extends JFrame implements ActionListener { +public class ConnectFour extends JFrame { /** * */ @@ -36,6 +37,14 @@ public class ConnectFour extends JFrame implements ActionListener { private int player = 0; + private Player players[] = new Player[2]; + + enum GameType { + Local, + RemoteServer, + RemoteClient + } + /** * Constructor */ @@ -55,7 +64,6 @@ public class ConnectFour extends JFrame implements ActionListener { // initialize ButtonRow this.buttonRow = new ButtonRow(this.gameBoard.getColumns()); - buttonRow.addActionListenerToButtons(this); // add components to window c.gridy = 0; @@ -68,6 +76,23 @@ public class ConnectFour extends JFrame implements ActionListener { this.pack(); this.setVisible(true); + 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; + } } /** @@ -75,16 +100,11 @@ public class ConnectFour extends JFrame implements ActionListener { * * @param column The column to insert the plate into */ - private void insertNextPlate(int column) { + public void insertNextPlate(int column) { + String res; - - - if (player == 0) { - res = this.gameBoard.insertPlate(new Plate(PlateType.X), column); - } else { - res = this.gameBoard.insertPlate(new Plate(PlateType.O), column); - } + res = this.gameBoard.insertPlate(new Plate(this.players[player].usingPlateType), column); if (res == "err") { // beep in case of error @@ -103,12 +123,16 @@ public class ConnectFour extends JFrame implements ActionListener { * Function to switch the player */ private void switchPlayer() { + this.players[player].setIsMyTurn(false); + if (player == 0) { player = 1; } else{ player = 0; } + + this.players[player].setIsMyTurn(true); } /** @@ -142,19 +166,6 @@ public class ConnectFour extends JFrame implements ActionListener { 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()); } diff --git a/src/de/itsblue/ConnectFour/Plate.java b/src/de/itsblue/ConnectFour/Plate.java index c09518a..b80a913 100644 --- a/src/de/itsblue/ConnectFour/Plate.java +++ b/src/de/itsblue/ConnectFour/Plate.java @@ -34,7 +34,7 @@ public class Plate { /** * Enum containing all plate types */ - enum PlateType { + public enum PlateType { X, O } @@ -48,7 +48,7 @@ public class Plate { * * @param type type of the plate */ - Plate(PlateType type) { + public Plate(PlateType type) { this.type = type; } @@ -67,13 +67,22 @@ public class Plate { * @return plate type as int (O=1; X=-1) */ public int getValue() { - switch (this.type) { - case O: - return 1; - case X: - return -1; - default: - return 0; + return Plate.getValue(this.type); + } + + /** + * Function to get a PlateType as int + * + * @return plate type as int (O=1; X=-1) + */ + public static int getValue(PlateType type) { + switch (type) { + case O: + return 1; + case X: + return -1; + default: + return 0; } } @@ -83,13 +92,22 @@ public class Plate { * @return the color of the plate */ public Color getColor() { - switch (this.type) { - case O: - return Color.BLACK; - case X: - return Color.RED; - default: - return Color.WHITE; + return Plate.getColor(this.type); + } + + /** + * Function to get the color of a certian PlateType + * + * @return the color of the PlateType + */ + public static Color getColor(PlateType type) { + switch (type) { + case O: + return Color.BLACK; + case X: + return Color.RED; + default: + return Color.WHITE; } } diff --git a/src/de/itsblue/ConnectFour/PlateInsertButton.java b/src/de/itsblue/ConnectFour/PlateInsertButton.java index b7e6ad7..7603414 100644 --- a/src/de/itsblue/ConnectFour/PlateInsertButton.java +++ b/src/de/itsblue/ConnectFour/PlateInsertButton.java @@ -27,7 +27,7 @@ import javax.swing.*; * * @author Dorian Zedler */ -class PlateInsertButton extends JButton { +public class PlateInsertButton extends JButton { /** * diff --git a/src/de/itsblue/ConnectFour/player/LocalPlayer.java b/src/de/itsblue/ConnectFour/player/LocalPlayer.java new file mode 100644 index 0000000..67303c0 --- /dev/null +++ b/src/de/itsblue/ConnectFour/player/LocalPlayer.java @@ -0,0 +1,74 @@ +/* + 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.player; + +import java.awt.event.*; + +import de.itsblue.ConnectFour.Plate.*; +import de.itsblue.ConnectFour.*; + +/** + * LocalPlayer is a class meant for usage with de.itsblue.ConnectFour. It is + * used for a player controlled by the local buttons in the button row. + * + * @author Dorian Zedler + */ +public class LocalPlayer extends Player implements ActionListener { + + /** + * Constructor + * + * @param playingInGame The game the Player is plaing in The funtion + * insertNextPlate() of the game will + * be called when a move is done. + * @param controlledByButtonRow The button row used to control the player. + * @param usingPlateType The type of plate the player is using. + */ + public LocalPlayer(ConnectFour playingInGame, ButtonRow controlledByButtonRow, PlateType usingPlateType) { + super(playingInGame, controlledByButtonRow, usingPlateType); + } + + /** + * Function to set wether it is this player's turn + */ + @Override + public void setIsMyTurn(boolean isMyTurn) { + super.setIsMyTurn(isMyTurn); + + // Remove the action listener from the button row when inactive + if (isMyTurn) + this.gameControllingButtonRow.addActionListenerToButtons(this); + else + this.gameControllingButtonRow.removeActionListemerFromButtons(this); + } + + /** + * Catch the actions from the button row + */ + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() instanceof PlateInsertButton && this.isMyTurn) { + // if the action source was a PlateInsertButton + // -> trigger a plate insertion + PlateInsertButton source = (PlateInsertButton) e.getSource(); + this.doMove(source.getIndex()); + } + } + +} \ No newline at end of file diff --git a/src/de/itsblue/ConnectFour/player/Player.java b/src/de/itsblue/ConnectFour/player/Player.java new file mode 100644 index 0000000..8f9d5ef --- /dev/null +++ b/src/de/itsblue/ConnectFour/player/Player.java @@ -0,0 +1,88 @@ +/* + 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.player; + +import de.itsblue.ConnectFour.Plate.*; +import de.itsblue.ConnectFour.*; + +/** + * Player is an abstract class meant for usage with de.itsblue.ConnectFour. It + * is a template for a connect four player. + * + * @author Dorian Zedler + */ +public abstract class Player { + + /** + * The game the player is playing in. The funtion insertNextPlate() + * of the game will be called when a move is done. + */ + private ConnectFour playingInGame; + + /** + * The button row used to control the game. + */ + public ButtonRow gameControllingButtonRow; + + /** + * The type of plate the player is using. + */ + public PlateType usingPlateType; + + /** + * Whether it is this player's turn + */ + public boolean isMyTurn = false; + + /** + * Constructor + * + * @param playingInGame The game the Player is plaing in The funtion + * insertNextPlate() of the game + * will be called when a move is done. + * @param gameControllingButtonRow The button row used to control the game. + * @param usingPlateType The type of plate the player is using. + */ + public Player(ConnectFour playingInGame, ButtonRow gameControllingButtonRow, PlateType usingPlateType) { + this.playingInGame = playingInGame; + this.gameControllingButtonRow = gameControllingButtonRow; + this.usingPlateType = usingPlateType; + } + + /** + * Function to set wether it is this player's turn + */ + public void setIsMyTurn(boolean isMyTurn) { + this.isMyTurn = isMyTurn; + + if (isMyTurn) + this.gameControllingButtonRow.setColor(Plate.getColor(this.usingPlateType)); + + } + + /** + * Function to perform a move for the Player + * + * @param column the column to insert the plate into + */ + public void doMove(int column) { + if (this.isMyTurn) + this.playingInGame.insertNextPlate(column); + } +} \ No newline at end of file