tests + ado equals override

This commit is contained in:
Janet-Doe
2025-04-16 10:55:27 +02:00
parent 3f06a9eb17
commit 076288a400
9 changed files with 89 additions and 4 deletions

View File

@@ -301,4 +301,11 @@ public class ChessBoard {
this.lastMove = lastMove; this.lastMove = lastMove;
} }
@Override
public boolean equals(Object otherBoard){
if (!(otherBoard instanceof ChessBoard)) return false;
return ((hashPlayerPieces(Color.White) == ((ChessBoard)otherBoard).hashPlayerPieces(Color.White))
&& (hashPlayerPieces(Color.Black) == ((ChessBoard)otherBoard).hashPlayerPieces(Color.Black)));
}
} }

View File

@@ -28,4 +28,7 @@ public abstract class Piece {
public abstract <T> T accept(PieceVisitor<T> visitor); public abstract <T> T accept(PieceVisitor<T> visitor);
@Override
public abstract boolean equals(Object other);
} }

View File

@@ -20,4 +20,9 @@ public class Bishop extends Piece {
return 0; return 0;
} }
@Override
public boolean equals(Piece obj) {
return (obj instanceof Bishop && ((Bishop) obj).getColor() == this.getColor());
}
} }

View File

@@ -19,4 +19,9 @@ public class King extends Piece {
public int hashCode() { public int hashCode() {
return 1; return 1;
} }
@Override
public boolean equals(Piece obj) {
return (obj instanceof King && ((King) obj).getColor() == this.getColor());
}
} }

View File

@@ -19,4 +19,9 @@ public class Knight extends Piece {
public int hashCode() { public int hashCode() {
return 2; return 2;
} }
@Override
public boolean equals(Piece obj) {
return (obj instanceof Knight && ((Knight) obj).getColor() == this.getColor());
}
} }

View File

@@ -23,4 +23,9 @@ public class Pawn extends Piece {
public int hashCode() { public int hashCode() {
return 3; return 3;
} }
@Override
public boolean equals(Piece obj) {
return (obj instanceof Pawn && ((Pawn) obj).getColor() == this.getColor());
}
} }

View File

@@ -19,4 +19,9 @@ public class Queen extends Piece {
public int hashCode() { public int hashCode() {
return 4; return 4;
} }
@Override
public boolean equals(Piece obj) {
return (obj instanceof Queen && ((Queen) obj).getColor() == this.getColor());
}
} }

View File

@@ -19,4 +19,9 @@ public class Rook extends Piece {
public int hashCode() { public int hashCode() {
return 5; return 5;
} }
@Override
public boolean equals(Piece obj) {
return (obj instanceof Rook && ((Rook) obj).getColor() == this.getColor());
}
} }

View File

@@ -3,12 +3,57 @@
*/ */
package chess; package chess;
import chess.ai.DumbAI;
import chess.controller.Command;
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.controller.commands.UndoCommand;
import chess.controller.event.GameAdaptator;
import chess.model.*;
import chess.model.pieces.*;
import chess.simulator.Simulator;
import chess.view.simplerender.Window;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import java.util.Objects;
class AppTest { class AppTest {
@Test void appHasAGreeting() { @Test void functionalRollback(){
// App classUnderTest = new App(); Game game = new Game(new ChessBoard());
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting"); CommandExecutor commandExecutor = new CommandExecutor(game);
DumbAI ai = new DumbAI(commandExecutor, Color.Black);
commandExecutor.addListener(ai);
DumbAI ai2 = new DumbAI(commandExecutor, Color.White);
commandExecutor.addListener(ai2);
commandExecutor.addListener(new GameAdaptator() {
@Override
public void onGameEnd() {
Command.CommandResult result;
do {
result = commandExecutor.executeCommand(new UndoCommand());
} while (result != Command.CommandResult.NotAllowed);
Game initialGame = new Game(new ChessBoard());
CommandExecutor initialCommandExecutor = new CommandExecutor(initialGame);
commandExecutor.executeCommand(new NewGameCommand());
assert(game.getBoard().equals(initialGame.getBoard()));
}
});
commandExecutor.executeCommand(new NewGameCommand());
}
@Test void functionalRollbacks(){
for (int i=0; i<100; i++) {
functionalRollback();
}
} }
} }