- implemented a whole new approach to player handlig

This commit is contained in:
Dorian Zedler 2020-02-24 22:54:37 +01:00
parent de3543513f
commit c9557aa24a
6 changed files with 254 additions and 41 deletions

View file

@ -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
*/

View file

@ -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());
}

View file

@ -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;
}
}

View file

@ -27,7 +27,7 @@ import javax.swing.*;
*
* @author Dorian Zedler
*/
class PlateInsertButton extends JButton {
public class PlateInsertButton extends JButton {
/**
*

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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
* <code>insertNextPlate()</code> 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());
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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 <code>insertNextPlate()</code>
* 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
* <code>insertNextPlate()</code> 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);
}
}