Commit e73c7a32 by Paktalin

Lesson 5 exercise 6 done + refactoring

parent 1642774c
...@@ -10,7 +10,7 @@ class Board { ...@@ -10,7 +10,7 @@ class Board {
static final int ROW_COUNT = 8; static final int ROW_COUNT = 8;
static final int COLUMN_COUNT = 8; static final int COLUMN_COUNT = 8;
private Piece[][] positionState = new Piece[ROW_COUNT][COLUMN_COUNT]; private Piece[][] board = new Piece[ROW_COUNT][COLUMN_COUNT];
void initialize() { void initialize() {
initializeKingRank(Piece.Color.Black, 0); initializeKingRank(Piece.Color.Black, 0);
...@@ -20,34 +20,38 @@ class Board { ...@@ -20,34 +20,38 @@ class Board {
} }
private void initializeKingRank(Piece.Color color, int rank) { private void initializeKingRank(Piece.Color color, int rank) {
setPosition(Piece.createRook(color), rank, 'a'); placePiece(Piece.createRook(color), rank, 'a');
setPosition(Piece.createKnight(color), rank, 'b'); placePiece(Piece.createKnight(color), rank, 'b');
setPosition(Piece.createBishop(color), rank, 'c'); placePiece(Piece.createBishop(color), rank, 'c');
setPosition(Piece.createQueen(color), rank, 'd'); placePiece(Piece.createQueen(color), rank, 'd');
setPosition(Piece.createKing(color), rank, 'e'); placePiece(Piece.createKing(color), rank, 'e');
setPosition(Piece.createBishop(color), rank, 'f'); placePiece(Piece.createBishop(color), rank, 'f');
setPosition(Piece.createKnight(color), rank, 'g'); placePiece(Piece.createKnight(color), rank, 'g');
setPosition(Piece.createRook(color), rank, 'h'); placePiece(Piece.createRook(color), rank, 'h');
} }
private void initializePawnRank(Piece.Color color, int rank) { private void initializePawnRank(Piece.Color color, int rank) {
for (int i = 0; i < COLUMN_COUNT; i++) for (int i = 0; i < COLUMN_COUNT; i++)
setPosition(Piece.createPawn(color), rank, i); placePiece(Piece.createPawn(color), rank, i);
} }
private void setPosition(Piece piece, int rank, char file) { private void placePiece(Piece piece, int row, char file) {
setPosition(piece, rank, fileToColumn(file)); placePiece(piece, row, fileToColumn(file));
} }
private void setPosition(Piece piece, int rank, int column) { private void placePiece(Piece piece, int row, int column) {
positionState[rank][column] = piece; board[row][column] = piece;
}
void placePiece(Piece piece, String position) {
placePiece(piece, retrieveRow(position), retrieveColumn(position));
} }
int pieceCount() { int pieceCount() {
int count = 0; int count = 0;
for (int i = 0; i < ROW_COUNT; i++) for (int i = 0; i < ROW_COUNT; i++)
for (int j = 0; j < COLUMN_COUNT; j++) for (int j = 0; j < COLUMN_COUNT; j++)
if (positionState[i][j] != null) if (board[i][j] != null)
count++; count++;
return count; return count;
...@@ -57,8 +61,8 @@ class Board { ...@@ -57,8 +61,8 @@ class Board {
int counter = 0; int counter = 0;
for (int i = 0; i < ROW_COUNT; i++) for (int i = 0; i < ROW_COUNT; i++)
for (int j = 0; j < COLUMN_COUNT; j++) { for (int j = 0; j < COLUMN_COUNT; j++) {
if (positionState[i][j] != null) if (board[i][j] != null)
if (positionState[i][j].getColor() == color) if (board[i][j].getColor() == color)
counter++; counter++;
} }
return counter; return counter;
...@@ -68,8 +72,8 @@ class Board { ...@@ -68,8 +72,8 @@ class Board {
int count = 0; int count = 0;
for (int i = 0; i < ROW_COUNT; i++) { for (int i = 0; i < ROW_COUNT; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) { for (int j = 0; j < COLUMN_COUNT; j++) {
if (positionState[i][j] != null) if (board[i][j] != null)
if (positionState[i][j].isType(type) && positionState[i][j].getColor() == color) if (board[i][j].isType(type) && board[i][j].getColor() == color)
count++; count++;
} }
} }
...@@ -80,10 +84,10 @@ class Board { ...@@ -80,10 +84,10 @@ class Board {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
for (int i = 0; i < ROW_COUNT; i++) { for (int i = 0; i < ROW_COUNT; i++) {
for (int j = 0; j < COLUMN_COUNT; j++) { for (int j = 0; j < COLUMN_COUNT; j++) {
if (positionState[i][j] == null) if (board[i][j] == null)
buffer.append("."); buffer.append(".");
else else
buffer.append(positionState[i][j].getRepresentation()); buffer.append(board[i][j].getRepresentation());
} }
buffer.append(NEW_LINE); buffer.append(NEW_LINE);
} }
...@@ -95,12 +99,18 @@ class Board { ...@@ -95,12 +99,18 @@ class Board {
return (int)file - (int)firstColumnLetter; return (int)file - (int)firstColumnLetter;
} }
Piece getPieceAtLocation(String location) { private int retrieveRow(String position) {
int row = Integer.parseInt(location.split("")[1]) - 1; return Integer.parseInt(position.split("")[1]) - 1;
int column = fileToColumn(location.toCharArray()[0]); }
return positionState[row][column]; private int retrieveColumn(String position) {
return fileToColumn(position.toCharArray()[0]);
} }
Piece getPieceAtPosition(String position) {
int row = retrieveRow(position);
int column = retrieveColumn(position);
return board[row][column];
}
} }
\ No newline at end of file
...@@ -13,4 +13,6 @@ public class StringUtil { ...@@ -13,4 +13,6 @@ public class StringUtil {
public static String appendNewLine(String string) { public static String appendNewLine(String string) {
return string + NEW_LINE; return string + NEW_LINE;
} }
} }
...@@ -4,6 +4,9 @@ import com.example.paktalin.agilejava_exercises.util.StringUtil; ...@@ -4,6 +4,9 @@ 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.Type.*;
/** /**
* Created by Paktalin on 23/05/2018. * Created by Paktalin on 23/05/2018.
*/ */
...@@ -28,31 +31,40 @@ public class BoardTest extends TestCase { ...@@ -28,31 +31,40 @@ public class BoardTest extends TestCase {
StringUtil.appendNewLine("rnbqkbnr"), StringUtil.appendNewLine("rnbqkbnr"),
board.print()); board.print());
assertEquals(16, board.pieceCount(Piece.Color.Black)); assertEquals(16, board.pieceCount(Black));
assertEquals(16, board.pieceCount(Piece.Color.White)); assertEquals(16, board.pieceCount(White));
} }
public void testCountPieces() { public void testCountPieces() {
board.initialize(); board.initialize();
assertEquals(8, board.pieceCount(Piece.Type.Pawn, Piece.Color.White)); assertEquals(8, board.pieceCount(Pawn, White));
assertEquals(1, board.pieceCount(Piece.Type.King, Piece.Color.Black)); assertEquals(1, board.pieceCount(King, Black));
assertEquals(1, board.pieceCount(Piece.Type.Queen, Piece.Color.White)); assertEquals(1, board.pieceCount(Queen, White));
assertEquals(2, board.pieceCount(Piece.Type.Bishop, Piece.Color.Black)); assertEquals(2, board.pieceCount(Bishop, Black));
assertEquals(2, board.pieceCount(Piece.Type.Knight, Piece.Color.White)); assertEquals(2, board.pieceCount(Knight, White));
assertEquals(2, board.pieceCount(Piece.Type.Rook, Piece.Color.Black)); assertEquals(2, board.pieceCount(Rook, Black));
} }
public void testPieceLocation() { public void testPieceLocation() {
board.initialize(); board.initialize();
Piece piece = board.getPieceAtLocation("a8"); Piece piece = board.getPieceAtPosition("a8");
assertEquals(Rook, piece.getType());
assertEquals(White, piece.getColor());
assertEquals(Piece.Type.Rook, piece.getType()); piece = board.getPieceAtPosition("e1");
assertEquals(Piece.Color.White, piece.getColor()); assertEquals(King, piece.getType());
assertEquals(Black, piece.getColor());
}
public void testPlacePieces() {
board.initialize();
piece = board.getPieceAtLocation("e1"); board.placePiece(Piece.createBishop(Black), "a8");
assertEquals(Piece.Type.King, piece.getType()); Piece bishop = board.getPieceAtPosition("a8");
assertEquals(Piece.Color.Black, piece.getColor()); assertEquals(Bishop, bishop.getType());
assertEquals(Black, bishop.getColor());
} }
} }
...@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises; ...@@ -2,6 +2,9 @@ package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase; import junit.framework.TestCase;
import static com.example.paktalin.agilejava_exercises.Piece.Type.*;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
/** /**
* Created by Paktalin on 23/05/2018. * Created by Paktalin on 23/05/2018.
*/ */
...@@ -9,12 +12,12 @@ import junit.framework.TestCase; ...@@ -9,12 +12,12 @@ import junit.framework.TestCase;
public class PieceTest extends TestCase { public class PieceTest extends TestCase {
public void testCreate() { public void testCreate() {
verifyCreation(Piece.createPawn(Piece.Color.White), Piece.createPawn(Piece.Color.Black), Piece.Type.Pawn, 'p'); verifyCreation(Piece.createPawn(White), Piece.createPawn(Black), Pawn, 'p');
verifyCreation(Piece.createKing(Piece.Color.White), Piece.createKing(Piece.Color.Black), Piece.Type.King, 'k'); verifyCreation(Piece.createKing(White), Piece.createKing(Black), King, 'k');
verifyCreation(Piece.createBishop(Piece.Color.White), Piece.createBishop(Piece.Color.Black), Piece.Type.Bishop, 'b'); verifyCreation(Piece.createBishop(White), Piece.createBishop(Black), Bishop, 'b');
verifyCreation(Piece.createRook(Piece.Color.White), Piece.createRook(Piece.Color.Black), Piece.Type.Rook, 'r'); verifyCreation(Piece.createRook(White), Piece.createRook(Black), Rook, 'r');
verifyCreation(Piece.createKnight(Piece.Color.White), Piece.createKnight(Piece.Color.Black), Piece.Type.Knight, 'n'); verifyCreation(Piece.createKnight(White), Piece.createKnight(Black), Knight, 'n');
verifyCreation(Piece.createQueen(Piece.Color.White), Piece.createQueen(Piece.Color.Black), Piece.Type.Queen, 'q'); verifyCreation(Piece.createQueen(White), Piece.createQueen(Black), 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