Added 'ServerCardVisitor' class.
Added the 'ServerCardVisitor' class to this project. It will be used as a visitor on the server to differentiate between all types of power cards.
This commit is contained in:
@@ -0,0 +1,147 @@
|
|||||||
|
package pp.mdga.visitor;
|
||||||
|
|
||||||
|
import pp.mdga.game.Piece;
|
||||||
|
import pp.mdga.game.PieceState;
|
||||||
|
import pp.mdga.game.card.HiddenCard;
|
||||||
|
import pp.mdga.game.card.PowerCard;
|
||||||
|
import pp.mdga.game.card.ShieldCard;
|
||||||
|
import pp.mdga.game.card.SwapCard;
|
||||||
|
import pp.mdga.game.card.TurboCard;
|
||||||
|
import pp.mdga.server.ServerGameLogic;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents the visitor for all types of power cards.
|
||||||
|
*/
|
||||||
|
public class ServerCardVisitor implements Visitor {
|
||||||
|
/**
|
||||||
|
* Create ServerCardVisitor attributes
|
||||||
|
*/
|
||||||
|
private final List<PowerCard> cards = new ArrayList<>();
|
||||||
|
private final List<Piece> shieldPieces = new ArrayList<>();
|
||||||
|
private final List<Piece> swapOwnPieces = new ArrayList<>();
|
||||||
|
private final List<Piece> swapOtherPieces = new ArrayList<>();
|
||||||
|
private final ServerGameLogic logic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param logic as the logic of this server which holds the model as a ServerGameLogic object.
|
||||||
|
*/
|
||||||
|
public ServerCardVisitor(ServerGameLogic logic) {
|
||||||
|
this.logic = logic;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to visit the given card parameter.
|
||||||
|
*
|
||||||
|
* @param card as a TurboCard object.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void visit(TurboCard card) {
|
||||||
|
this.cards.add(card);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to visit the given card parameter.
|
||||||
|
*
|
||||||
|
* @param card as a SwapCard object.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void visit(SwapCard card) {
|
||||||
|
List<Piece> possibleOwnPieces = new ArrayList<>();
|
||||||
|
for (Piece piece : this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor()).getPieces()) {
|
||||||
|
if (piece.getState() == PieceState.ACTIVE) {
|
||||||
|
if (!possibleOwnPieces.contains(piece)) {
|
||||||
|
possibleOwnPieces.add(piece);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Piece> possibleOtherPieces = new ArrayList<>();
|
||||||
|
for (var player : this.logic.getGame().getPlayers().values()) {
|
||||||
|
if (player != this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor())) {
|
||||||
|
for (Piece piece : player.getPieces()) {
|
||||||
|
if (piece.getState() == PieceState.ACTIVE) {
|
||||||
|
if (!possibleOtherPieces.contains(piece)) {
|
||||||
|
possibleOtherPieces.add(piece);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!possibleOtherPieces.isEmpty() && !possibleOwnPieces.isEmpty()) {
|
||||||
|
this.swapOwnPieces.addAll(possibleOtherPieces);
|
||||||
|
this.swapOtherPieces.addAll(possibleOwnPieces);
|
||||||
|
this.cards.add(card);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to visit the given card parameter.
|
||||||
|
*
|
||||||
|
* @param card as a ShieldCard object.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void visit(ShieldCard card) {
|
||||||
|
for (Piece piece : this.logic.getGame().getPlayerByColor(this.logic.getGame().getActiveColor()).getPieces()) {
|
||||||
|
if (piece.getState() == PieceState.ACTIVE) {
|
||||||
|
if (!this.shieldPieces.contains(piece)) {
|
||||||
|
this.shieldPieces.add(piece);
|
||||||
|
}
|
||||||
|
if (!this.cards.contains(card)) {
|
||||||
|
this.cards.add(card);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to visit the given card parameter.
|
||||||
|
*
|
||||||
|
* @param card as a HiddenCard object.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void visit(HiddenCard card) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to return cards attribute of ServerCardVisitor class.
|
||||||
|
*
|
||||||
|
* @return cards as a List of PowerCard objects.
|
||||||
|
*/
|
||||||
|
public List<PowerCard> getCards() {
|
||||||
|
return this.cards;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to return shieldPieces attribute of ServerCardVisitor class.
|
||||||
|
*
|
||||||
|
* @return shieldPieces as a List of Piece objects.
|
||||||
|
*/
|
||||||
|
public List<Piece> getShieldPieces() {
|
||||||
|
return this.shieldPieces;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to return swapOwnPieces attribute of ServerCardVisitor class.
|
||||||
|
*
|
||||||
|
* @return swapOwnPieces as a List of Piece objects.
|
||||||
|
*/
|
||||||
|
public List<Piece> getSwapOwnPieces() {
|
||||||
|
return this.swapOwnPieces;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method will be used to return swapOtherPieces attribute of ServerCardVisitor class.
|
||||||
|
*
|
||||||
|
* @return swapOtherPieces as a List of Piece objects.
|
||||||
|
*/
|
||||||
|
public List<Piece> getSwapOtherPieces() {
|
||||||
|
return this.swapOtherPieces;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user