Commit 94b90657 by Paktalin

Refactoring in Lesson 5

parent a00c86ad
package com.example.paktalin.agilejava_exercises;
import java.util.ArrayList;
import java.util.List;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/**
......@@ -10,78 +7,88 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
class Board {
private List<Piece> pieces = new ArrayList<>();
static final int ROW_COUNT = 8;
static final int COLUMN_COUNT = 8;
private Piece[][] positionState = new Piece[ROW_COUNT][COLUMN_COUNT];
void initialize() {
fillFirstRank();
for (int i = 0; i < 8; i++)
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Pawn));
for (int i = 0; i < 8; i++)
pieces.add(new Piece(Piece.Color.White, Piece.Type.Pawn));
fillEightRank();
initializeKingRank(Piece.Color.Black, 0);
initializePawnRank(Piece.Color.Black, 1);
initializePawnRank(Piece.Color.White, 6);
initializeKingRank(Piece.Color.White, 7);
}
private void initializeKingRank(Piece.Color color, int rank) {
setPosition(Piece.createRook(color), rank, 'a');
setPosition(Piece.createKnight(color), rank, 'b');
setPosition(Piece.createBishop(color), rank, 'c');
setPosition(Piece.createQueen(color), rank, 'd');
setPosition(Piece.createKing(color), rank, 'e');
setPosition(Piece.createBishop(color), rank, 'f');
setPosition(Piece.createKnight(color), rank, 'g');
setPosition(Piece.createRook(color), rank, 'h');
}
private void fillFirstRank() {
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Rook));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Queen));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.King));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.Black, Piece.Type.Rook));
private void initializePawnRank(Piece.Color color, int rank) {
for (int i = 0; i < COLUMN_COUNT; i++)
setPosition(Piece.createPawn(color), rank, i);
}
private void fillEightRank() {
pieces.add(new Piece(Piece.Color.White, Piece.Type.Rook));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Queen));
pieces.add(new Piece(Piece.Color.White, Piece.Type.King));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Bishop));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Knight));
pieces.add(new Piece(Piece.Color.White, Piece.Type.Rook));
private void setPosition(Piece piece, int rank, char file) {
char firstColumnLetter = 'a';
int column = (int)file - (int)firstColumnLetter;
setPosition(piece, rank, column);
}
private void setPosition(Piece piece, int rank, int column) {
positionState[rank][column] = piece;
}
int pieceCount() {
return pieces.size();
int count = 0;
for (int i = 0; i < ROW_COUNT; i++)
for (int j = 0; j < COLUMN_COUNT; j++)
if (positionState[i][j] != null)
count++;
return count;
}
int colorCount(Piece.Color color) {
int pieceCount(Piece.Color color) {
int counter = 0;
for (Piece piece : pieces)
if (piece.getColor() == color)
for (int i = 0; i < ROW_COUNT; i++)
for (int j = 0; j < COLUMN_COUNT; j++) {
if (positionState[i][j] != null)
if (positionState[i][j].getColor() == color)
counter++;
}
return counter;
}
String printRank(int from) {
StringBuilder buffer = new StringBuilder();
for (int i = from; i < from + 8; i++)
buffer.append(pieces.get(i).getRepresentation());
return buffer.toString();
int pieceCount(Piece.Type type, Piece.Color color) {
int count = 0;
for (int i = 0; i < ROW_COUNT; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) {
if (positionState[i][j] != null)
if (positionState[i][j].isType(type) && positionState[i][j].getColor() == color)
count++;
}
}
return count;
}
String print() {
StringBuilder buffer = new StringBuilder();
buffer.append(printRank(0) + NEW_LINE);
buffer.append(printRank(8) + NEW_LINE);
for (int i = 3; i < 7; i++)
buffer.append("........" + NEW_LINE);
buffer.append(printRank(16) + NEW_LINE);
buffer.append(printRank(24) + NEW_LINE);
return buffer.toString();
for (int i = 0; i < ROW_COUNT; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) {
if (positionState[i][j] == null)
buffer.append(".");
else
buffer.append(positionState[i][j].getRepresentation());
}
int getCount(Piece.Type type, Piece.Color color) {
int count = 0;
for (Piece piece : pieces)
if (piece.getType() == type && piece.getColor() == color)
count++;
return count;
buffer.append(NEW_LINE);
}
return buffer.toString();
}
}
\ No newline at end of file
......@@ -54,45 +54,26 @@ public class Piece {
return this.color == Color.Black;
}
static Piece createWhitePawn() {
return new Piece(Color.White, Type.Pawn);
}
static Piece createBlackPawn() {
return new Piece(Color.Black, Type.Pawn);
boolean isType(Piece.Type type) {
return this.getType() == type;
}
static Piece createWhiteKing() {
return new Piece(Color.White, Type.King);
}
static Piece createBlackKing() {
return new Piece(Color.Black, Type.King);
static Piece createPawn(Color color) {
return new Piece(color, Type.Pawn);
}
static Piece createWhiteBishop() {
return new Piece(Color.White, Type.Bishop);
static Piece createKing(Color color) {
return new Piece(color, Type.King);
}
static Piece createBlackBishop() {
return new Piece(Color.Black, Type.Bishop);
}
static Piece createWhiteRook() {
return new Piece(Color.White, Type.Rook);
static Piece createBishop(Color color) {
return new Piece(color, Type.Bishop);
}
static Piece createBlackRook() {
return new Piece(Color.Black, Type.Rook);
static Piece createRook(Color color) {
return new Piece(color, Type.Rook);
}
static Piece createWhiteKnight() {
return new Piece(Color.White, Type.Knight);
}
static Piece createBlackKnight() {
return new Piece(Color.Black, Type.Knight);
}
static Piece createWhiteQueen() {
return new Piece(Color.White, Type.Queen);
static Piece createKnight(Color color) {
return new Piece(color, Type.Knight);
}
static Piece createBlackQueen() {
return new Piece(Color.Black, Type.Queen);
static Piece createQueen(Color color) {
return new Piece(color, Type.Queen);
}
}
......@@ -28,18 +28,23 @@ public class BoardTest extends TestCase {
StringUtil.appendNewLine("rnbqkbnr"),
board.print());
assertEquals(16, board.colorCount(Piece.Color.Black));
assertEquals(16, board.colorCount(Piece.Color.White));
assertEquals(16, board.pieceCount(Piece.Color.Black));
assertEquals(16, board.pieceCount(Piece.Color.White));
}
public void testCountPieces() {
board.initialize();
assertEquals(8, board.getCount(Piece.Type.Pawn, Piece.Color.White));
assertEquals(1, board.getCount(Piece.Type.King, Piece.Color.Black));
assertEquals(1, board.getCount(Piece.Type.Queen, Piece.Color.White));
assertEquals(2, board.getCount(Piece.Type.Bishop, Piece.Color.Black));
assertEquals(2, board.getCount(Piece.Type.Knight, Piece.Color.White));
assertEquals(2, board.getCount(Piece.Type.Rook, Piece.Color.Black));
assertEquals(8, board.pieceCount(Piece.Type.Pawn, Piece.Color.White));
assertEquals(1, board.pieceCount(Piece.Type.King, Piece.Color.Black));
assertEquals(1, board.pieceCount(Piece.Type.Queen, Piece.Color.White));
assertEquals(2, board.pieceCount(Piece.Type.Bishop, Piece.Color.Black));
assertEquals(2, board.pieceCount(Piece.Type.Knight, Piece.Color.White));
assertEquals(2, board.pieceCount(Piece.Type.Rook, Piece.Color.Black));
}
public void testBoardDimensions() {
assertEquals(8, board.ROW_COUNT);
assertEquals(8, board.COLUMN_COUNT);
}
}
......@@ -9,12 +9,12 @@ import junit.framework.TestCase;
public class PieceTest extends TestCase {
public void testCreate() {
verifyCreation(Piece.createWhitePawn(), Piece.createBlackPawn(), Piece.Type.Pawn, 'p');
verifyCreation(Piece.createWhiteKing(), Piece.createBlackKing(), Piece.Type.King, 'k');
verifyCreation(Piece.createWhiteBishop(), Piece.createBlackBishop(), Piece.Type.Bishop, 'b');
verifyCreation(Piece.createWhiteRook(), Piece.createBlackRook(), Piece.Type.Rook, 'r');
verifyCreation(Piece.createWhiteKnight(), Piece.createBlackKnight(), Piece.Type.Knight, 'n');
verifyCreation(Piece.createWhiteQueen(), Piece.createBlackQueen(), Piece.Type.Queen, 'q');
verifyCreation(Piece.createPawn(Piece.Color.White), Piece.createPawn(Piece.Color.Black), Piece.Type.Pawn, 'p');
verifyCreation(Piece.createKing(Piece.Color.White), Piece.createKing(Piece.Color.Black), Piece.Type.King, 'k');
verifyCreation(Piece.createBishop(Piece.Color.White), Piece.createBishop(Piece.Color.Black), Piece.Type.Bishop, 'b');
verifyCreation(Piece.createRook(Piece.Color.White), Piece.createRook(Piece.Color.Black), Piece.Type.Rook, 'r');
verifyCreation(Piece.createKnight(Piece.Color.White), Piece.createKnight(Piece.Color.Black), Piece.Type.Knight, 'n');
verifyCreation(Piece.createQueen(Piece.Color.White), Piece.createQueen(Piece.Color.Black), Piece.Type.Queen, 'q');
}
private void verifyCreation(Piece whitePiece, Piece blackPiece, Piece.Type type, char representation) {
......
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