Commit 445de99b by Paktalin

Lesson 4 done

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