Permalink
Browse files

Failed testRemovingOnePieceFromTablebase

  • Loading branch information...
Kestutis-Z
Kestutis-Z committed Nov 5, 2012
1 parent 9c2d523 commit 433217786ba14f0bf161d55b77ebeaddddd40104
@@ -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;
}
-
}
@@ -107,6 +107,15 @@ public void testGetAllPieces() {
assertEquals(expectedPieces, actualPieces);
}
+ @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;
Binary file not shown.

0 comments on commit 4332177

Please sign in to comment.