Browse Source

Added equals method to ChessFigure with test

feature-chess
Nick Stolbov 2 years ago
parent
commit
c6c9940d8d
  1. 19
      src/main/java/Game/ChessObj/ChessFigure.java
  2. 12
      src/test/java/Game/ChessObj/ChessFigureTest.java

19
src/main/java/Game/ChessObj/ChessFigure.java

@ -34,7 +34,7 @@ public class ChessFigure {
public String getSymbol() {
String symbol = "";
switch(getType()) {
switch (getType()) {
case KING:
symbol = "K";
break;
@ -56,9 +56,22 @@ public class ChessFigure {
default:
}
symbol = ((this.getTeam()==Team.WHITE)?" ":"|") + symbol + ((this.getTeam()==Team.WHITE)?" ":"|");
symbol = ((this.getTeam() == Team.WHITE) ? " " : "|") + symbol + ((this.getTeam() == Team.WHITE) ? " " : "|");
return symbol;
return symbol;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ChessFigure)) {
return false;
}
ChessFigure x = (ChessFigure) o;
if (this.getType() != x.getType() || this.getTeam() != x.getTeam())
return false;
return true;
}
}

12
src/test/java/Game/ChessObj/ChessFigureTest.java

@ -31,4 +31,16 @@ class ChessFigureTest {
assertEquals("|Z|", new ChessFigure(ChessFigure.Type.KNIGHT, ChessFigure.Team.BLACK).getSymbol());
assertEquals("|o|", new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.BLACK).getSymbol());
}
@Test
void testEquals() {
ChessFigure kw1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE);
ChessFigure kw2 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.WHITE);
ChessFigure kb1 = new ChessFigure(ChessFigure.Type.KING, ChessFigure.Team.BLACK);
ChessFigure pw1 = new ChessFigure(ChessFigure.Type.PAWN, ChessFigure.Team.WHITE);
assertEquals(kw1, kw2);
assertNotEquals(kw2, kb1);
assertNotEquals(pw1, kb1);
}
}
Loading…
Cancel
Save