Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Failed testRemovingOnePieceFromTablebase

  • Loading branch information...
commit 433217786ba14f0bf161d55b77ebeaddddd40104 1 parent 9c2d523
Kestutis-Z authored
View
36 endgame-oracle/src/main/java/tablebases/Tablebase.java
@@ -124,6 +124,41 @@ protected static int getConsequtiveDuplicateCharsInArrayUpToIndex(
return allPieces;
}
+ /**
+ * @return tablebase that would be obtained by removing the
+ * specified piece from this tablebase
+ */
+ public Tablebase removePiece(Piece piece) {
+ if (piece.getPieceType() == PieceType.KING)
+ throw new IllegalArgumentException("Cannot remove " + piece);
+ char ch = piece.getPieceType().getPieceTypeAbbreviation();
+ char kingCh = PieceType.KING.getPieceTypeAbbreviation();
+ String oldTB = this.name();
+ String newTB = oldTB;
+
+ if (piece.getPieceColour() == PieceColour.WHITE) {
+ for (int i = 1; oldTB.charAt(i) != kingCh; i++) {
+ if (oldTB.charAt(i) == ch) {
+ newTB = deleteCharAt(oldTB, i);
+ break;
+ }
+ }
+ } else
+ for (int i = oldTB.length() - 1; oldTB.charAt(i) != kingCh; i--) {
+ if (oldTB.charAt(i) == ch) {
+ newTB = deleteCharAt(oldTB, i);
+ break;
+ }
+ }
+ return valueOf(newTB);
+ }
+
+ private String deleteCharAt(String s, int index) {
+ StringBuffer buf = new StringBuffer(s.length() - 1);
+ buf.append(s.substring(0, index)).append(s.substring(index + 1));
+ return buf.toString();
+ }
+
@Override
public String toString() {
List<Piece> whitePieces = getWhitePieces();
@@ -144,6 +179,5 @@ public String toString() {
return output;
}
-
}
View
9 endgame-oracle/src/test/java/tablebases/TablebaseTest.java
@@ -108,6 +108,15 @@ public void testGetAllPieces() {
}
@Test
+ public void testRemovingOnePieceFromTablebase() {
+ Tablebase tablebase = Tablebase.KRPKR;
+ Tablebase expectedTablebase = Tablebase.KRKP;
+ Tablebase actualTablebase = tablebase.removePiece(Piece.WHITE_ROOK);
+
+ assertEquals(expectedTablebase, actualTablebase);
+ }
+
+ @Test
public void testTablebaseToStringOutput() {
Tablebase tablebase = Tablebase.KPK;
String actualString = tablebase.toString();
View
BIN  endgame-oracle/target/classes/tablebases/Tablebase.class
Binary file not shown
View
BIN  endgame-oracle/target/test-classes/tablebases/TablebaseTest.class
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.