Commit 611a32a1 by Paktalin

Lesson 5 Exercise 7 complete

parent 59a9a544
......@@ -118,11 +118,12 @@ class Board {
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);
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();
strength += piece.getStrength(this, column, color);
}
}
return strength;
......@@ -134,4 +135,15 @@ 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
......@@ -46,7 +46,7 @@ public class Piece {
return representation;
}
double getStrength() {
double getStrength(Board board, int column, Color color) {
if (type == Type.Queen)
return 9;
if (type == Type.Rook)
......@@ -55,16 +55,23 @@ public class Piece {
return 3;
if (type == Type.Knight)
return 2.5;
if (type == Type.Pawn)
if (type == Type.Pawn) {
if (board.getPawnsPerColumn(column, color) > 1)
return 0.5;
return 1.0;
}
return 0;
}
boolean isColor(Color color) {
return this.color == color;
}
boolean isWhite() {
return this.color == Color.White;
return isColor(Color.White);
}
boolean isBlack() {
return this.color == Color.Black;
return isColor(Color.Black);
}
boolean isType(Piece.Type type) {
return this.getType() == type;
......
......@@ -71,18 +71,18 @@ public class BoardTest extends TestCase {
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(White), "f2", 15.0, 15.0);
verifyStrength(Piece.createPawn(Black), "c7", 16.0, 14.5);
verifyStrength(Piece.createPawn(White), "g2", 16.0, 15.0);
verifyStrength(Piece.createPawn(Black), "c7", 16.0, 15.0);
verifyStrength(Piece.createPawn(White), "g2", 16.0, 16.0);
verifyStrength(Piece.createBishop(Black), "d7", 19.0, 15.0);
verifyStrength(Piece.createPawn(White), "f3", 19.0, 15.5);
verifyStrength(Piece.createBishop(Black), "d7", 19.0, 16.0);
verifyStrength(Piece.createPawn(White), "f3", 19.0, 16.0);
verifyStrength(Piece.createPawn(Black), "b6", 20.0, 15.5);
verifyStrength(Piece.createPawn(White), "h3", 18.5, 16.0);
verifyStrength(Piece.createPawn(Black), "b6", 20.0, 16.0);
verifyStrength(Piece.createPawn(White), "h3", 20.0, 17.0);
verifyStrength(Piece.createKnight(White), "f4", 18.5, 18.5);
verifyStrength(Piece.createKnight(White), "f4", 20.0, 19.5);
assertEquals(
".KR..... 8\n" +
......
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