Commit a774fec3 by Paktalin

Refactoring for exercise 7

parent 611a32a1
......@@ -7,32 +7,44 @@ import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
*/
class Board {
static final int ROW_COUNT = 8;
static final int COLUMN_COUNT = 8;
private static final int ROW_COUNT = 8;
private static final int COLUMN_COUNT = 8;
private Piece[][] board = new Piece[ROW_COUNT][COLUMN_COUNT];
private Piece[][] pieces = new Piece[ROW_COUNT][COLUMN_COUNT];
void initialize() {
private Board() {}
static Board createEmpty() {
return new Board();
}
static Board createInitialized() {
Board board = new Board();
board.initialize();
return board;
}
private void initialize() {
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) {
placePiece(Piece.createRook(color), rank, 'a');
placePiece(Piece.createKnight(color), rank, 'b');
placePiece(Piece.createBishop(color), rank, 'c');
placePiece(Piece.createQueen(color), rank, 'd');
placePiece(Piece.createKing(color), rank, 'e');
placePiece(Piece.createBishop(color), rank, 'f');
placePiece(Piece.createKnight(color), rank, 'g');
placePiece(Piece.createRook(color), rank, 'h');
private void initializeKingRank(Piece.Color color, int row) {
placePiece(Piece.createRook(color), row, 'a');
placePiece(Piece.createKnight(color), row, 'b');
placePiece(Piece.createBishop(color), row, 'c');
placePiece(Piece.createQueen(color), row, 'd');
placePiece(Piece.createKing(color), row, 'e');
placePiece(Piece.createBishop(color), row, 'f');
placePiece(Piece.createKnight(color), row, 'g');
placePiece(Piece.createRook(color), row, 'h');
}
private void initializePawnRank(Piece.Color color, int rank) {
for (int i = 0; i < COLUMN_COUNT; i++)
placePiece(Piece.createPawn(color), rank, i);
private void initializePawnRank(Piece.Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; column++)
placePiece(Piece.createPawn(color), row, column);
}
private void placePiece(Piece piece, int row, char file) {
......@@ -40,7 +52,7 @@ class Board {
}
private void placePiece(Piece piece, int row, int column) {
board[row][column] = piece;
pieces[row][column] = piece;
}
void placePiece(Piece piece, String position) {
......@@ -57,9 +69,9 @@ class Board {
int pieceCount() {
int count = 0;
for (int i = 0; i < ROW_COUNT; i++)
for (int j = 0; j < COLUMN_COUNT; j++)
if (board[i][j] != null)
for (int row = 0; row < ROW_COUNT; row++)
for (int column = 0; column < COLUMN_COUNT; column++)
if (pieces[row][column] != null)
count++;
return count;
......@@ -67,10 +79,10 @@ class Board {
int pieceCount(Piece.Color color) {
int counter = 0;
for (int i = 0; i < ROW_COUNT; i++)
for (int j = 0; j < COLUMN_COUNT; j++) {
if (board[i][j] != null)
if (board[i][j].getColor() == color)
for (int row = 0; row < ROW_COUNT; row++)
for (int column = 0; column < COLUMN_COUNT; column++) {
if (pieces[row][column] != null)
if (pieces[row][column].getColor() == color)
counter++;
}
return counter;
......@@ -78,10 +90,10 @@ class Board {
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 (board[i][j] != null)
if (board[i][j].isType(type) && board[i][j].getColor() == color)
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
if (pieces[row][column] != null)
if (pieces[row][column].isType(type) && pieces[row][column].getColor() == color)
count++;
}
}
......@@ -90,14 +102,14 @@ class Board {
String print() {
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < ROW_COUNT; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) {
if (board[i][j] == null)
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
if (pieces[row][column] == null)
buffer.append(".");
else
buffer.append(board[i][j].getRepresentation());
buffer.append(pieces[row][column].getRepresentation());
}
buffer.append(" " + (ROW_COUNT - i) + NEW_LINE);
buffer.append(" " + (ROW_COUNT - row) + NEW_LINE);
}
buffer.append("abcdefgh");
return buffer.toString();
......@@ -108,15 +120,15 @@ class Board {
return (int)file - (int)firstColumnLetter;
}
Piece getPieceAtPosition(int row, int column) {
return board[row][column];
private Piece getPieceAtPosition(int row, int column) {
return pieces[row][column];
}
Piece getPieceAtPosition(String position) {
return getPieceAtPosition(retrieveRow(position), retrieveColumn(position));
}
double getStrength(Piece.Color color) {
private double getStrength(Piece.Color color) {
double strength = 0;
for (int row = 0; row < ROW_COUNT; row++) {
......
......@@ -13,7 +13,7 @@ public class Piece {
private Piece() {}
Piece(final Color color, final Type type) {
private Piece(final Color color, final Type type) {
this.color = color;
this.type = type;
}
......@@ -21,7 +21,7 @@ public class Piece {
public Color getColor() {
return color;
}
public Type getType() {
Type getType() {
return type;
}
......
......@@ -8,11 +8,4 @@ public class StringUtil {
public static final String NEW_LINE = "\n";
private StringUtil() {}
public static String appendNewLine(String string) {
return string + NEW_LINE;
}
}
package com.example.paktalin.agilejava_exercises;
import com.example.paktalin.agilejava_exercises.util.StringUtil;
import junit.framework.TestCase;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
......@@ -14,12 +12,8 @@ import static com.example.paktalin.agilejava_exercises.Piece.Type.*;
public class BoardTest extends TestCase {
private Board board;
protected void setUp() {
board = new Board();
}
public void testCreate() {
board.initialize();
board = Board.createInitialized();
assertEquals(32, board.pieceCount());
......@@ -28,7 +22,7 @@ public class BoardTest extends TestCase {
}
public void testCountPieces() {
board.initialize();
board = Board.createInitialized();
assertEquals(8, board.pieceCount(Pawn, White));
assertEquals(1, board.pieceCount(King, Black));
......@@ -39,7 +33,7 @@ public class BoardTest extends TestCase {
}
public void testPieceLocation() {
board.initialize();
board = Board.createInitialized();
Piece piece = board.getPieceAtPosition("a8");
......@@ -52,7 +46,7 @@ public class BoardTest extends TestCase {
}
public void testPlacePieces() {
board.initialize();
board = Board.createInitialized();
board.placePiece(Piece.createBishop(Black), "a8");
Piece bishop = board.getPieceAtPosition("a8");
......@@ -61,6 +55,8 @@ public class BoardTest extends TestCase {
}
public void testOverallStrength() {
board = Board.createEmpty();
verifyStrength(Piece.createQueen(Black), "e6", 9.0, 0.0);
verifyStrength(Piece.createQueen(White), "g4", 9.0, 9.0);
......
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