Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Passed testGetPieceFromAbbreviation

  • Loading branch information...
commit b51948570e89d29b5637dedb831118bafaae3160 1 parent fe8ed35
Kestutis-Z authored
View
12 endgame-oracle/src/main/java/chess/Piece.java
@@ -76,4 +76,16 @@ public String getPieceAbbreviation() {
}
return allAbbreviations;
}
+
+ public static Piece getPieceFromAbbreviation(String abbreviation) {
+ String pieceName = "";
+ pieceName += PieceColour.getPieceColourFromAbbreviation(abbreviation.charAt(0));
+ pieceName += "_";
+ pieceName += PieceType.getPieceTypeFromAbbreviation(abbreviation.charAt(1));
+ if (abbreviation.length() == 3) {
+ pieceName += "_";
+ pieceName += abbreviation.charAt(2);
+ }
+ return Piece.valueOf(pieceName);
+ }
}
View
11 endgame-oracle/src/main/java/chess/PieceColour.java
@@ -24,5 +24,16 @@ private PieceColour(char pieceColourAbbreviation) {
public char getPieceColourAbbreviation() {
return pieceTypeColourFirstLetter;
}
+
+ public static PieceColour getPieceColourFromAbbreviation(char abbreviation) {
+ switch(abbreviation) {
+ case 'W':
+ return WHITE;
+ case 'B':
+ return BLACK;
+ default:
+ throw new AssertionError("Not supported abbreviation: " + abbreviation);
+ }
+ }
}
View
55 endgame-oracle/src/test/java/chess/ChessPositionDiagramTest.java
@@ -4,12 +4,14 @@
import org.junit.Test;
+import com.google.common.collect.BiMap;
+import com.google.common.collect.EnumBiMap;
+
public class ChessPositionDiagramTest {
@Test
public void testGetChessPositionDiagram() throws IncorrectChessDiagramDrawingException {
- String drawing =
-
+ String drawing =
" _______________________________________________________ \n" +
" | | | | | | | | | \n" +
" 8| WK | | | | | | | | \n" +
@@ -45,5 +47,54 @@ public void testGetChessPositionDiagram() throws IncorrectChessDiagramDrawingExc
assertEquals(drawing, actual);
}
+
+ @Test
+ public void testGetPiecesWithSquares() throws IncorrectChessDiagramDrawingException {
+ String drawing =
+ " _______________________________________________________ \n" +
+ " | | | | | | | | | \n" +
+ " 8| | | | | | | | | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 7| | | | | | | | | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 6| | | | | | | BP3 | | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 5| | | | | WB | | | | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 4| | | | | | | | BP | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 3| WK | | | | WN | | | BP2 | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 2| | | | | | | BK | | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " | | | | | | | | | \n" +
+ " 1| | | | | | | | | \n" +
+ " |______|______|______|______|______|______|______|______| \n" +
+ " a b c d e f g h \n" ;
+
+ ChessPositionDiagram cpDiagram = ChessPositionDiagram.createFromTextDiagram(drawing);
+
+ BiMap<Piece, Square> expectedPiecesWithSquaresKBNKPPP =
+ EnumBiMap.create(Piece.class, Square.class);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.WHITE_KING, Square.A3);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.WHITE_BISHOP, Square.E5);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.WHITE_KNIGHT, Square.E3);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.BLACK_KING, Square.G2);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.BLACK_PAWN, Square.H4);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.BLACK_PAWN_2, Square.H3);
+ expectedPiecesWithSquaresKBNKPPP.put(Piece.BLACK_PAWN_3, Square.G6);
+
+ BiMap<Piece, Square> actualPiecesWithSquaresKBNKPPP = cpDiagram.getPiecesWithSquares();
+
+ assertEquals(expectedPiecesWithSquaresKBNKPPP, actualPiecesWithSquaresKBNKPPP);
+ }
+
+
}
View
15 endgame-oracle/src/test/java/chess/PieceTest.java
@@ -63,6 +63,21 @@ public void testAllAbbreviationsOfPieces() {
List<String> actualList = Piece.allAbbreviationsOfPieces();
assertEquals(expectedList, actualList);
+ }
+
+ @Test
+ public void testGetPieceFromAbbreviation() {
+ String abbr1 = "BR2";
+ Piece expectedPiece = Piece.BLACK_ROOK_2;
+ Piece actualPiece = Piece.getPieceFromAbbreviation(abbr1);
+
+ assertEquals(expectedPiece, actualPiece);
+
+ String abbr2 = "WQ";
+ expectedPiece = Piece.WHITE_QUEEN;
+ actualPiece = Piece.getPieceFromAbbreviation(abbr2);
+
+ assertEquals(expectedPiece, actualPiece);
}
}
View
BIN  endgame-oracle/target/classes/chess/Piece.class
Binary file not shown
View
BIN  endgame-oracle/target/classes/chess/PieceColour.class
Binary file not shown
View
BIN  endgame-oracle/target/test-classes/chess/PieceTest.class
Binary file not shown
Please sign in to comment.
Something went wrong with that request. Please try again.