Commit 59a9a544 by Paktalin

Lesson 5 Exercise 7 half done

parent e73c7a32
......@@ -47,6 +47,14 @@ class Board {
placePiece(piece, retrieveRow(position), retrieveColumn(position));
}
private int retrieveRow(String position) {
return ROW_COUNT - Integer.parseInt(position.split("")[1]);
}
private int retrieveColumn(String position) {
return fileToColumn(position.toCharArray()[0]);
}
int pieceCount() {
int count = 0;
for (int i = 0; i < ROW_COUNT; i++)
......@@ -89,8 +97,9 @@ class Board {
else
buffer.append(board[i][j].getRepresentation());
}
buffer.append(NEW_LINE);
buffer.append(" " + (ROW_COUNT - i) + NEW_LINE);
}
buffer.append("abcdefgh");
return buffer.toString();
}
......@@ -99,18 +108,30 @@ class Board {
return (int)file - (int)firstColumnLetter;
}
private int retrieveRow(String position) {
return Integer.parseInt(position.split("")[1]) - 1;
Piece getPieceAtPosition(int row, int column) {
return board[row][column];
}
private int retrieveColumn(String position) {
return fileToColumn(position.toCharArray()[0]);
Piece getPieceAtPosition(String position) {
return getPieceAtPosition(retrieveRow(position), retrieveColumn(position));
}
Piece getPieceAtPosition(String position) {
int row = retrieveRow(position);
int column = retrieveColumn(position);
double getStrength(Piece.Color color) {
double strength = 0;
for (int i = 0; i < ROW_COUNT; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) {
Piece piece = getPieceAtPosition(i, j);
if (piece != null && piece.getColor() == color)
strength += piece.getStrength();
}
}
return strength;
}
return board[row][column];
double getBlackStrength() {
return getStrength(Piece.Color.Black);
}
double getWhiteStrength() {
return getStrength(Piece.Color.White);
}
}
\ No newline at end of file
......@@ -46,14 +46,26 @@ public class Piece {
return representation;
}
double getStrength() {
if (type == Type.Queen)
return 9;
if (type == Type.Rook)
return 5;
if (type == Type.Bishop)
return 3;
if (type == Type.Knight)
return 2.5;
if (type == Type.Pawn)
return 0.5;
return 0;
}
boolean isWhite() {
return this.color == Color.White;
}
boolean isBlack() {
return this.color == Color.Black;
}
boolean isType(Piece.Type type) {
return this.getType() == type;
}
......
......@@ -20,16 +20,8 @@ public class BoardTest extends TestCase {
public void testCreate() {
board.initialize();
assertEquals(32, board.pieceCount());
String blankRank = StringUtil.appendNewLine("........");
assertEquals(
StringUtil.appendNewLine("RNBQKBNR") +
StringUtil.appendNewLine("PPPPPPPP") +
blankRank + blankRank + blankRank + blankRank +
StringUtil.appendNewLine("pppppppp") +
StringUtil.appendNewLine("rnbqkbnr"),
board.print());
assertEquals(32, board.pieceCount());
assertEquals(16, board.pieceCount(Black));
assertEquals(16, board.pieceCount(White));
......@@ -52,11 +44,11 @@ public class BoardTest extends TestCase {
Piece piece = board.getPieceAtPosition("a8");
assertEquals(Rook, piece.getType());
assertEquals(White, piece.getColor());
assertEquals(Black, piece.getColor());
piece = board.getPieceAtPosition("e1");
assertEquals(King, piece.getType());
assertEquals(Black, piece.getColor());
assertEquals(White, piece.getColor());
}
public void testPlacePieces() {
......@@ -67,4 +59,47 @@ public class BoardTest extends TestCase {
assertEquals(Bishop, bishop.getType());
assertEquals(Black, bishop.getColor());
}
public void testOverallStrength() {
verifyStrength(Piece.createQueen(Black), "e6", 9.0, 0.0);
verifyStrength(Piece.createQueen(White), "g4", 9.0, 9.0);
verifyStrength(Piece.createKing(Black), "b8", 9.0, 9.0);
verifyStrength(Piece.createKing(White), "f1", 9.0, 9.0);
verifyStrength(Piece.createRook(Black), "c8", 14.0, 9.0);
verifyStrength(Piece.createRook(White), "e1", 14.0, 14.0);
verifyStrength(Piece.createPawn(Black), "a7", 15.0, 14.0);
verifyStrength(Piece.createPawn(White), "f2", 15.0, 14.5);
verifyStrength(Piece.createPawn(Black), "c7", 16.0, 14.5);
verifyStrength(Piece.createPawn(White), "g2", 16.0, 15.0);
verifyStrength(Piece.createBishop(Black), "d7", 19.0, 15.0);
verifyStrength(Piece.createPawn(White), "f3", 19.0, 15.5);
verifyStrength(Piece.createPawn(Black), "b6", 20.0, 15.5);
verifyStrength(Piece.createPawn(White), "h3", 18.5, 16.0);
verifyStrength(Piece.createKnight(White), "f4", 18.5, 18.5);
assertEquals(
".KR..... 8\n" +
"P.PB.... 7\n" +
".P..Q... 6\n" +
"........ 5\n" +
".....nq. 4\n" +
".....p.p 3\n" +
".....pp. 2\n" +
"....rk.. 1\n" +
"abcdefgh",
board.print());
}
private void verifyStrength(Piece piece, String position, double blackStrength, double whiteStrength) {
board.placePiece(piece, position);
assertEquals(blackStrength, board.getBlackStrength());
assertEquals(whiteStrength, board.getWhiteStrength());
}
}
......@@ -29,14 +29,4 @@ 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