Commit af649a49 by Paktalin

Lesson 5 exercise 8 in process

parent a774fec3
......@@ -50,15 +50,48 @@ class Board {
private void placePiece(Piece piece, int row, char file) {
placePiece(piece, row, fileToColumn(file));
}
private void placePiece(Piece piece, int row, int column) {
pieces[row][column] = piece;
setPieceStrength(piece, column);
}
void placePiece(Piece piece, String position) {
placePiece(piece, retrieveRow(position), retrieveColumn(position));
}
private void setPieceStrength(Piece piece, int column) {
if (piece.isType(Piece.Type.Pawn))
updatePawnsStrength(piece.getColor(), column);
else piece.setStrength();
}
private void updatePawnsStrength(Piece.Color color, int column) {
double strength;
if (pawnsPerColumn(color, column) > 1)
strength = 0.5;
else strength = 1.0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
currentPiece.setStrength(strength);
}
}
private int pawnsPerColumn(Piece.Color color, int column) {
int count = 0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
count++;
}
return count;
}
private int retrieveRow(String position) {
return ROW_COUNT - Integer.parseInt(position.split("")[1]);
}
......@@ -134,8 +167,9 @@ class Board {
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
Piece piece = getPieceAtPosition(row, column);
if (piece != null && piece.getColor() == color)
strength += piece.getStrength(this, column, color);
if (piece != null && piece.getColor() == color) {
strength += piece.getStrength();
}
}
}
return strength;
......@@ -147,15 +181,4 @@ class Board {
double getWhiteStrength() {
return getStrength(Piece.Color.White);
}
int getPawnsPerColumn(int column, Piece.Color color) {
int count = 0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
count++;
}
return count;
}
}
\ No newline at end of file
......@@ -11,6 +11,8 @@ public class Piece {
private Type type;
private Color color;
private double strength;
private Piece() {}
private Piece(final Color color, final Type type) {
......@@ -46,23 +48,26 @@ public class Piece {
return representation;
}
double getStrength(Board board, int column, Color color) {
void setStrength() {
if (type == Type.Queen)
return 9;
strength = 9;
if (type == Type.Rook)
return 5;
strength = 5;
if (type == Type.Bishop)
return 3;
strength = 3;
if (type == Type.Knight)
return 2.5;
if (type == Type.Pawn) {
if (board.getPawnsPerColumn(column, color) > 1)
return 0.5;
return 1.0;
}
return 0;
strength = 2.5;
if (type == Type.King)
strength = 0.0;
}
void setStrength(double strength) {
this.strength = strength;
}
double getStrength() {
return strength;
}
boolean isColor(Color color) {
return this.color == color;
......
......@@ -57,28 +57,28 @@ public class BoardTest extends TestCase {
public void testOverallStrength() {
board = Board.createEmpty();
verifyStrength(Piece.createQueen(Black), "e6", 9.0, 0.0);
verifyStrength(Piece.createQueen(White), "g4", 9.0, 9.0);
verifyStrength(Piece.createQueen(Black), "e6", 9.0, 0.0, 9.0);
verifyStrength(Piece.createQueen(White), "g4", 9.0, 9.0, 9.0);
verifyStrength(Piece.createKing(Black), "b8", 9.0, 9.0);
verifyStrength(Piece.createKing(White), "f1", 9.0, 9.0);
verifyStrength(Piece.createKing(Black), "b8", 9.0, 9.0, 0.0);
verifyStrength(Piece.createKing(White), "f1", 9.0, 9.0, 0.0);
verifyStrength(Piece.createRook(Black), "c8", 14.0, 9.0);
verifyStrength(Piece.createRook(White), "e1", 14.0, 14.0);
verifyStrength(Piece.createRook(Black), "c8", 14.0, 9.0, 5.0);
verifyStrength(Piece.createRook(White), "e1", 14.0, 14.0, 5.0);
verifyStrength(Piece.createPawn(Black), "a7", 15.0, 14.0);
verifyStrength(Piece.createPawn(White), "f2", 15.0, 15.0);
verifyStrength(Piece.createPawn(Black), "a7", 15.0, 14.0, 1.0);
verifyStrength(Piece.createPawn(White), "f2", 15.0, 15.0, 1.0);
verifyStrength(Piece.createPawn(Black), "c7", 16.0, 15.0);
verifyStrength(Piece.createPawn(White), "g2", 16.0, 16.0);
verifyStrength(Piece.createPawn(Black), "c7", 16.0, 15.0, 1.0);
verifyStrength(Piece.createPawn(White), "g2", 16.0, 16.0, 1.0);
verifyStrength(Piece.createBishop(Black), "d7", 19.0, 16.0);
verifyStrength(Piece.createPawn(White), "f3", 19.0, 16.0);
verifyStrength(Piece.createBishop(Black), "d7", 19.0, 16.0, 3.0);
verifyStrength(Piece.createPawn(White), "f3", 19.0, 16.0, 0.5);
verifyStrength(Piece.createPawn(Black), "b6", 20.0, 16.0);
verifyStrength(Piece.createPawn(White), "h3", 20.0, 17.0);
verifyStrength(Piece.createPawn(Black), "b6", 20.0, 16.0, 1.0);
verifyStrength(Piece.createPawn(White), "h3", 20.0, 17.0, 1.0);
verifyStrength(Piece.createKnight(White), "f4", 20.0, 19.5);
verifyStrength(Piece.createKnight(White), "f4", 20.0, 19.5, 2.5);
assertEquals(
".KR..... 8\n" +
......@@ -93,9 +93,11 @@ public class BoardTest extends TestCase {
board.print());
}
private void verifyStrength(Piece piece, String position, double blackStrength, double whiteStrength) {
private void verifyStrength(Piece piece, String position,
double blackStrength, double whiteStrength, double pieceStrength) {
board.placePiece(piece, position);
assertEquals(blackStrength, board.getBlackStrength());
assertEquals(whiteStrength, board.getWhiteStrength());
assertEquals(pieceStrength, piece.getStrength());
}
}
......@@ -29,4 +29,14 @@ public class PieceTest extends TestCase {
assertEquals(type, blackPiece.getType());
assertEquals(Character.toUpperCase(representation), blackPiece.getRepresentation());
}
public void testStrength() {
Board board = Board.createEmpty();
board.placePiece(Piece.createPawn(Black), "a7");
assertEquals(1.0, board.getPieceAtPosition("a7").getStrength());
board.placePiece(Piece.createPawn(Black), "a6");
assertEquals(0.5, board.getPieceAtPosition("a7").getStrength());
}
}
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