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/player/Player.java

144 lines
4.0 KiB
Java

/*
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 java.util.ArrayList;
import java.awt.event.*;
import java.awt.*;
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 button row used to control the game.
*/
protected ButtonRow gameControllingButtonRow;
/**
* The type of plate the player is using.
*/
protected PlateType usingPlateType;
/**
* An array containing all action listeners
*/
private ArrayList<ActionListener> playerActionListeners = new ArrayList<ActionListener>();
/**
* The current action id
*/
private int currentActionId = 0;
/**
* Whether it is this player's turn
*/
protected boolean isMyTurn = false;
/**
* Constructor
*
* @param gameControllingButtonRow The button row used to control the game.
* @param usingPlateType The type of plate the player is using.
*/
public Player(ButtonRow gameControllingButtonRow, PlateType usingPlateType) {
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));
this.fireActionListeners(this,"isMyTurnChanged " + (isMyTurn ? "true" : "false"));
}
/**
* Function to perform a move for the Player
*
* @param column the column to insert the plate into
*/
protected void doMove(int column) {
if (this.isMyTurn) {
this.fireActionListeners(this, "doMove " + column);
}
}
/**
* Function to get the plate type used by the player
*
* @return used plate type
*/
public PlateType getUsedPlateType() {
return this.usingPlateType;
}
/**
* Function to get the name of the player
*
* @return player name
*/
public String getName() {
return Plate.getName(this.usingPlateType);
}
/**
* Function to fire all actionListeners with a given action command
*
* @param actionCommand The action command to fire the listeners with
*/
protected void fireActionListeners(Object source, String command) {
for (ActionListener playerActionListener : playerActionListeners) {
playerActionListener.actionPerformed(new ActionEvent(source, this.currentActionId, command));
this.currentActionId++;
}
}
/**
* Function to add an ActionListener
*
* @param listener the listener to add
*/
public void addActionListener(ActionListener listener) {
this.playerActionListeners.add(listener);
}
/**
* Function to remove an ActionListener
*
* @param listener the listener to remove
*/
public void removeMoveListener(ActionListener listener) {
if (this.playerActionListeners.contains(listener))
this.playerActionListeners.remove(listener);
}
}