Commit 94b90657 by Paktalin

Refactoring in Lesson 5

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