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