Commit 1642774c by Paktalin

Lesson 5, exercise 5 done

parent 94b90657
......@@ -36,9 +36,7 @@ class Board {
}
private void setPosition(Piece piece, int rank, char file) {
char firstColumnLetter = 'a';
int column = (int)file - (int)firstColumnLetter;
setPosition(piece, rank, column);
setPosition(piece, rank, fileToColumn(file));
}
private void setPosition(Piece piece, int rank, int column) {
......@@ -91,4 +89,18 @@ class Board {
}
return buffer.toString();
}
private int fileToColumn(char file) {
char firstColumnLetter = 'a';
return (int)file - (int)firstColumnLetter;
}
Piece getPieceAtLocation(String location) {
int row = Integer.parseInt(location.split("")[1]) - 1;
int column = fileToColumn(location.toCharArray()[0]);
return positionState[row][column];
}
}
\ No newline at end of file
......@@ -43,8 +43,16 @@ public class BoardTest extends TestCase {
assertEquals(2, board.pieceCount(Piece.Type.Rook, Piece.Color.Black));
}
public void testBoardDimensions() {
assertEquals(8, board.ROW_COUNT);
assertEquals(8, board.COLUMN_COUNT);
public void testPieceLocation() {
board.initialize();
Piece piece = board.getPieceAtLocation("a8");
assertEquals(Piece.Type.Rook, piece.getType());
assertEquals(Piece.Color.White, piece.getColor());
piece = board.getPieceAtLocation("e1");
assertEquals(Piece.Type.King, piece.getType());
assertEquals(Piece.Color.Black, piece.getColor());
}
}
......@@ -26,4 +26,14 @@ public class PieceTest extends TestCase {
assertEquals(type, blackPiece.getType());
assertEquals(Character.toUpperCase(representation), blackPiece.getRepresentation());
}
public void testLanguage() {
assertEquals(8, Integer.parseInt("a8".split("")[1]));
assertEquals(0, fileToColumn("a8".toCharArray()[0]));
}
private int fileToColumn(char file) {
char firstColumnLetter = 'a';
return (int)file - (int)firstColumnLetter;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment