fixed the equals method in piece

This commit is contained in:
Fleischer Hanno
2024-12-08 11:23:53 +01:00
parent c9c99709ba
commit ec295c94f1
8 changed files with 17 additions and 11 deletions

View File

@@ -80,7 +80,6 @@ public void received(ServerStartGameMessage msg) {
} }
} }
logic.getGame().setBoard(msg.getBoard()); logic.getGame().setBoard(msg.getBoard());
System.out.println(logic.getGame().getPlayerById(logic.getOwnPlayerId()).toString());
logic.addNotification(new GameNotification(logic.getGame().getPlayerById(logic.getOwnPlayerId()).getColor())); logic.addNotification(new GameNotification(logic.getGame().getPlayerById(logic.getOwnPlayerId()).getColor()));
for (var player : logic.getGame().getPlayers().values()) { for (var player : logic.getGame().getPlayers().values()) {
List<UUID> pieces = new ArrayList<>(); List<UUID> pieces = new ArrayList<>();

View File

@@ -46,6 +46,7 @@ public void enter() {
for(Map.Entry<Integer, Player> entry : logic.getGame().getPlayers().entrySet()){ for(Map.Entry<Integer, Player> entry : logic.getGame().getPlayers().entrySet()){
logic.addNotification(new MovePieceNotification(entry.getValue().getPieces()[0].getUuid(), entry.getValue().getStartNodeIndex(), true)); logic.addNotification(new MovePieceNotification(entry.getValue().getPieces()[0].getUuid(), entry.getValue().getStartNodeIndex(), true));
logic.getGame().getBoard().getInfield()[entry.getValue().getStartNodeIndex()].setOccupant(entry.getValue().getPieces()[0]); logic.getGame().getBoard().getInfield()[entry.getValue().getStartNodeIndex()].setOccupant(entry.getValue().getPieces()[0]);
entry.getValue().getWaitingArea()[0] = null;
animationCounter++; animationCounter++;
if(entry.getKey() == logic.getOwnPlayerId()){ if(entry.getKey() == logic.getOwnPlayerId()){
logic.addNotification(new AcquireCardNotification(entry.getValue().getHandCards().get(0).getCard())); logic.addNotification(new AcquireCardNotification(entry.getValue().getHandCards().get(0).getCard()));

View File

@@ -79,13 +79,13 @@ private StartNode createStartNode(int i) {
*/ */
public int getInfieldIndexOfPiece(Piece piece) { public int getInfieldIndexOfPiece(Piece piece) {
for (int i = 0; i < infield.length; i++) { for (int i = 0; i < infield.length; i++) {
if(infield[i].getOccupant() == null) { System.out.println(infield[i].getOccupant());
continue; if(infield[i].isOccupied()) {
}
if (infield[i].getOccupant().equals(piece)) { if (infield[i].getOccupant().equals(piece)) {
return i; return i;
} }
} }
}
return -1; return -1;
} }

View File

@@ -2,6 +2,7 @@
import com.jme3.network.serializing.Serializable; import com.jme3.network.serializing.Serializable;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
/** /**
@@ -159,7 +160,9 @@ public boolean equals(Object obj) {
if (obj instanceof Piece) { if (obj instanceof Piece) {
Piece piece = (Piece) obj; Piece piece = (Piece) obj;
return this.hashCode() == piece.hashCode(); System.out.println("own UUID: " + this.uuid + ". Other UUID: " + piece.uuid);
return Objects.equals(this.uuid.toString(), piece.uuid.toString());
} }
return false; return false;

View File

@@ -187,7 +187,7 @@ public int getHomeIndexOfPiece(Piece piece) {
public boolean pieceInsideOfHome(Piece piece) { public boolean pieceInsideOfHome(Piece piece) {
for (Node node : this.homeNodes) { for (Node node : this.homeNodes) {
if (node.getOccupant().equals(piece)) { if (piece.equals(node.getOccupant())) {
return true; return true;
} }
} }

View File

@@ -52,7 +52,7 @@ public void initializeGame() {
for (var player : this.logic.getGame().getPlayers().values()) { for (var player : this.logic.getGame().getPlayers().values()) {
player.initialize(); player.initialize();
player.addHandCard(this.logic.getGame().draw()); player.addHandCard(this.logic.getGame().draw());
Piece piece = player.getWaitingArea()[0]; Piece piece = player.getPieces()[0];
player.getWaitingArea()[0] = null; player.getWaitingArea()[0] = null;
piece.setState(PieceState.ACTIVE); piece.setState(PieceState.ACTIVE);
this.logic.getGame().getBoard().getInfield()[player.getStartNodeIndex()].setOccupant(piece); this.logic.getGame().getBoard().getInfield()[player.getStartNodeIndex()].setOccupant(piece);

View File

@@ -11,6 +11,7 @@ public abstract class ChoosePieceAutomatonState extends ServerState {
* Create ChoosePieceAutomatonState attributes. * Create ChoosePieceAutomatonState attributes.
*/ */
protected final ChoosePieceState choosePieceAutomaton; protected final ChoosePieceState choosePieceAutomaton;
private final System.Logger LOGGER = System.getLogger(ChoosePieceAutomatonState.class.getName());
/** /**
* Constructs a server state of the specified game logic. * Constructs a server state of the specified game logic.
@@ -92,6 +93,7 @@ private boolean canHomeMove(Piece piece, int moveIndex){
protected int calculateTargetIndex(Piece piece){ protected int calculateTargetIndex(Piece piece){
int steps = logic.getGame().getDiceModifier() * logic.getGame().getDiceEyes(); int steps = logic.getGame().getDiceModifier() * logic.getGame().getDiceEyes();
LOGGER.log(System.Logger.Level.INFO, "Calculating target index for piece: " + piece.getUuid() + " with steps: " + steps + "start index: " + logic.getGame().getBoard().getInfieldIndexOfPiece(piece));
return (logic.getGame().getBoard().getInfieldIndexOfPiece(piece) + steps) % logic.getGame().getBoard().getInfield().length; return (logic.getGame().getBoard().getInfieldIndexOfPiece(piece) + steps) % logic.getGame().getBoard().getInfield().length;
} }
} }

View File

@@ -30,20 +30,21 @@ public StartPieceState(ChoosePieceState choosePieceAutomaton, ServerGameLogic lo
*/ */
@Override @Override
public void enter() { public void enter() {
LOGGER.log(System.Logger.Level.DEBUG, "Exited StartPieceState state."); LOGGER.log(System.Logger.Level.INFO, "Enter StartPieceState state.");
piece = logic.getGame().getBoard().getInfield()[logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getStartNodeIndex()].getOccupant(); piece = logic.getGame().getBoard().getInfield()[logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getStartNodeIndex()].getOccupant();
logic.getServerSender().send(logic.getGame().getActivePlayerId(), new StartPieceMessage(piece.getUuid(), calculateTargetIndex(piece))); logic.getServerSender().send(logic.getGame().getActivePlayerId(), new StartPieceMessage(piece.getUuid(), calculateTargetIndex(piece)));
} }
@Override @Override
public void received(RequestMoveMessage msg, int from){ public void received(RequestMoveMessage msg, int from){
LOGGER.log(System.Logger.Level.INFO, "Received RequestMoveMessage message. is piece equals: " + piece.equals(msg.getPiece()));
if (piece.equals(msg.getPiece())) { if (piece.equals(msg.getPiece())) {
logic.getServerSender().broadcast(new MoveMessage(piece, false, calculateTargetIndex(piece)));
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(piece)].clearOccupant(); logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(piece)].clearOccupant();
Piece occ = logic.getGame().getBoard().getInfield()[logic.getGame().getPlayerByColor(piece.getColor()).getStartNodeIndex()].getOccupant(); Piece occ = logic.getGame().getBoard().getInfield()[logic.getGame().getPlayerByColor(piece.getColor()).getStartNodeIndex()].getOccupant();
if (occ != null){ if (occ != null){
logic.getGame().getPlayerByColor(occ.getColor()).addWaitingPiece(occ); logic.getGame().getPlayerByColor(occ.getColor()).addWaitingPiece(occ);
} }
logic.getServerSender().broadcast(new MoveMessage(piece, false, calculateTargetIndex(piece)));
this.choosePieceAutomaton.getTurnAutomaton().setCurrentState(this.choosePieceAutomaton.getTurnAutomaton().getMovePieceState()); this.choosePieceAutomaton.getTurnAutomaton().setCurrentState(this.choosePieceAutomaton.getTurnAutomaton().getMovePieceState());
} }
} }