Commit 445de99b by Paktalin

Lesson 4 done

parent 830017af
...@@ -10,80 +10,69 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE; ...@@ -10,80 +10,69 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/ */
class Board { class Board {
private List<Piece> pieces = new ArrayList<>();
private List<Piece> firstRank = new ArrayList<>();
private List<Piece> secondRank = new ArrayList<>();
private List<Piece> seventhRank = new ArrayList<>();
private List<Piece> eightRank = new ArrayList<>();
private final String blankRank = "........" + NEW_LINE;
void initialize() { void initialize() {
fillFirstRank(); fillFirstRank();
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++)
secondRank.add(new Piece(Piece.Color.Black, Piece.Name.Pawn)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Pawn));
seventhRank.add(new Piece(Piece.Color.White, Piece.Name.Pawn)); for (int i = 0; i < 8; i++)
} pieces.add(new Piece(Piece.Color.White, Piece.Name.Pawn));
fillEightRank(); fillEightRank();
} }
private void fillFirstRank() { private void fillFirstRank() {
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Rook)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Rook));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Knight)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Knight));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Bishop)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Bishop));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Queen)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Queen));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.King)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.King));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Bishop)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Bishop));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Knight)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Knight));
firstRank.add(new Piece(Piece.Color.Black, Piece.Name.Rook)); pieces.add(new Piece(Piece.Color.Black, Piece.Name.Rook));
} }
private void fillEightRank() { private void fillEightRank() {
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Rook)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Rook));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Knight)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Knight));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Bishop)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Bishop));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Queen)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Queen));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.King)); pieces.add(new Piece(Piece.Color.White, Piece.Name.King));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Bishop)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Bishop));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Knight)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Knight));
eightRank.add(new Piece(Piece.Color.White, Piece.Name.Rook)); pieces.add(new Piece(Piece.Color.White, Piece.Name.Rook));
} }
int pieceCount() { int pieceCount() {
return firstRank.size() + secondRank.size() + seventhRank.size() + eightRank.size(); return pieces.size();
} }
List<Piece> getRank(int rankNumber) { int colorCount(Piece.Color color) {
switch (rankNumber) { int counter = 0;
case 1: for (Piece piece : pieces)
return firstRank; if (piece.getColor() == color)
case 2: counter++;
return secondRank; return counter;
case 7:
return seventhRank;
default:
return eightRank;
}
} }
String printRank(int rankNumber) { String printRank(int from) {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
for (Piece piece : getRank(rankNumber)) for (int i = from; i < from + 8; i++)
buffer.append(piece.print()); buffer.append(pieces.get(i).print());
return buffer.toString(); return buffer.toString();
} }
String print() { String print() {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
buffer.append(printRank(1) + NEW_LINE); buffer.append(printRank(0) + NEW_LINE);
buffer.append(printRank(2) + NEW_LINE); buffer.append(printRank(8) + NEW_LINE);
for (int i = 3; i < 7; i++) for (int i = 3; i < 7; i++)
buffer.append(blankRank); buffer.append("........" + NEW_LINE);
buffer.append(printRank(7) + NEW_LINE); buffer.append(printRank(16) + NEW_LINE);
buffer.append(printRank(8) + NEW_LINE); buffer.append(printRank(24) + NEW_LINE);
return buffer.toString(); return buffer.toString();
} }
......
...@@ -53,4 +53,12 @@ public class Piece { ...@@ -53,4 +53,12 @@ public class Piece {
return letter.toUpperCase(); return letter.toUpperCase();
return letter; return letter;
} }
boolean isWhite() {
return this.color == Color.White;
}
boolean isBlack() {
return this.color == Color.Black;
}
} }
...@@ -27,5 +27,8 @@ public class BoardTest extends TestCase { ...@@ -27,5 +27,8 @@ public class BoardTest extends TestCase {
StringUtil.appendNewLine("pppppppp") + StringUtil.appendNewLine("pppppppp") +
StringUtil.appendNewLine("rnbqkbnr"), StringUtil.appendNewLine("rnbqkbnr"),
board.print()); board.print());
assertEquals(16, board.colorCount(Piece.Color.Black));
assertEquals(16, board.colorCount(Piece.Color.White));
} }
} }
...@@ -18,6 +18,9 @@ public class PieceTest extends TestCase { ...@@ -18,6 +18,9 @@ public class PieceTest extends TestCase {
public void testPawnColor() { public void testPawnColor() {
assertEquals(Piece.Color.White, whitePawn.getColor()); assertEquals(Piece.Color.White, whitePawn.getColor());
assertEquals(Piece.Color.Black, blackPawn.getColor()); assertEquals(Piece.Color.Black, blackPawn.getColor());
assertTrue(whitePawn.isWhite());
assertFalse(blackPawn.isWhite());
} }
public void testPawnRepresentation() { public void testPawnRepresentation() {
......
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