Commit 1263e172 by Paktalin

Refactoring for Lesson 5 Exercise 9

parent f0607e45
package com.example.paktalin.agilejava_exercises;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Paktalin on 26/05/2018.
*/
class BlackSide implements Side {
private List<Piece> pieces = new ArrayList<>();
@Override
public List<Piece> getPieces() {
return pieces;
}
@Override
public double getStrength() {
double strength = 0.0;
for (Piece piece : pieces)
strength += piece.getStrength();
return strength;
}
@Override
public int getPiecesCount() {
return pieces.size();
}
@Override
public int getPiecesCount(Piece.Type type) {
int count = 0;
for (Piece piece : pieces)
if (piece.isType(type))
count++;
return count;
}
@Override
public void addPiece(Piece piece) {
pieces.add(piece);
Collections.sort(pieces);
}
}
package com.example.paktalin.agilejava_exercises;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/**
......@@ -16,8 +15,8 @@ class Board {
private Piece[][] pieces = new Piece[ROW_COUNT][COLUMN_COUNT];
private List<Piece> blackPieces = new ArrayList<>();
private List<Piece> whitePieces = new ArrayList<>();
private WhiteSide whiteSide = new WhiteSide();
private BlackSide blackSide = new BlackSide();
private Board() {}
......@@ -32,10 +31,10 @@ class Board {
}
private void initialize() {
initializeKingRank(Piece.Color.Black, 0);
initializePawnRank(Piece.Color.Black, 1);
initializePawnRank(Piece.Color.White, 6);
initializeKingRank(Piece.Color.White, 7);
initializeKingRank(Black, 0);
initializePawnRank(Black, 1);
initializePawnRank(White, 6);
initializeKingRank(White, 7);
}
private void initializeKingRank(Piece.Color color, int row) {
......@@ -54,6 +53,9 @@ class Board {
placePiece(Piece.createPawn(color), row, column);
}
void placePiece(Piece piece, String position) {
placePiece(piece, retrieveRow(position), retrieveColumn(position));
}
private void placePiece(Piece piece, int row, char file) {
placePiece(piece, row, fileToColumn(file));
}
......@@ -63,18 +65,8 @@ class Board {
addToCollection(piece);
}
void placePiece(Piece piece, String position) {
placePiece(piece, retrieveRow(position), retrieveColumn(position));
}
void addToCollection(Piece piece) {
if (piece.isWhite()) {
whitePieces.add(piece);
Collections.sort(whitePieces);
} else {
blackPieces.add(piece);
Collections.sort(blackPieces);
}
getSide(piece.getColor()).addPiece(piece);
}
private void setPieceStrength(Piece piece, int column) {
......@@ -113,32 +105,10 @@ class Board {
private int retrieveRow(String position) {
return ROW_COUNT - Integer.parseInt(position.split("")[1]);
}
private int retrieveColumn(String position) {
return fileToColumn(position.toCharArray()[0]);
}
int getPiecesCount() {
return getBlackPiecesCount() + getWhitePiecesCount();
}
int getPiecesCount(Piece.Type type, List<Piece> pieces) {
int count = 0;
for (Piece piece : pieces)
if (piece.isType(type))
count++;
return count;
}
int getWhitePiecesCount(Piece.Type type) {
return getPiecesCount(type, whitePieces);
}
int getBlackPiecesCount(Piece.Type type) {
return getPiecesCount(type, blackPieces);
}
String print() {
StringBuilder buffer = new StringBuilder();
for (int row = 0; row < ROW_COUNT; row++) {
......@@ -167,38 +137,25 @@ class Board {
return getPieceAtPosition(retrieveRow(position), retrieveColumn(position));
}
private double getStrength(Piece.Color color) {
double strength = 0;
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
Piece piece = getPieceAtPosition(row, column);
if (piece != null && piece.getColor() == color) {
strength += piece.getStrength();
}
}
}
return strength;
Side getSide(Piece.Color color){
if (color == White)
return whiteSide;
return blackSide;
}
List<Piece> getBlackPieces() {
return blackPieces;
}
List<Piece> getWhitePieces() {
return whitePieces;
int getPiecesCount() {
return getPiecesCount(Black) + getPiecesCount(White);
}
double getBlackStrength() {
return getStrength(Piece.Color.Black);
double getStrength(Piece.Color color) {
return getSide(color).getStrength();
}
double getWhiteStrength() {
return getStrength(Piece.Color.White);
List<Piece> getPieces(Piece.Color color) {
return getSide(color).getPieces();
}
int getBlackPiecesCount() {
return blackPieces.size();
int getPiecesCount(Piece.Color color){
return getSide(color).getPiecesCount();
}
int getWhitePiecesCount() {
return whitePieces.size();
int getPiecesCount(Piece.Color color, Piece.Type type){
return getSide(color).getPiecesCount(type);
}
}
\ No newline at end of file
package com.example.paktalin.agilejava_exercises;
import java.util.List;
/**
* Created by Paktalin on 26/05/2018.
*/
interface Side {
List<Piece> getPieces();
double getStrength();
int getPiecesCount();
int getPiecesCount(Piece.Type type);
void addPiece(Piece piece);
}
package com.example.paktalin.agilejava_exercises;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Paktalin on 26/05/2018.
*/
public class WhiteSide implements Side {
private List<Piece> pieces = new ArrayList<>();
@Override
public List<Piece> getPieces() {
return pieces;
}
@Override
public double getStrength() {
double strength = 0.0;
for (Piece piece : pieces)
strength += piece.getStrength();
return strength;
}
@Override
public int getPiecesCount() {
return pieces.size();
}
@Override
public int getPiecesCount(Piece.Type type) {
int count = 0;
for (Piece piece : pieces)
if (piece.isType(type))
count++;
return count;
}
@Override
public void addPiece(Piece piece) {
pieces.add(piece);
Collections.sort(pieces);
}
}
......@@ -18,8 +18,8 @@ public class BoardTest extends TestCase {
board = Board.createInitialized();
assertEquals(32, board.getPiecesCount());
assertEquals(16, board.getBlackPiecesCount());
assertEquals(16, board.getWhitePiecesCount());
assertEquals(16, board.getPiecesCount(Black));
assertEquals(16, board.getPiecesCount(White));
}
public void testPlacePieces() {
......@@ -77,15 +77,15 @@ public class BoardTest extends TestCase {
private void verifyStrength(Piece piece, String position,
double blackStrength, double whiteStrength, double pieceStrength) {
board.placePiece(piece, position);
assertEquals(blackStrength, board.getBlackStrength());
assertEquals(whiteStrength, board.getWhiteStrength());
assertEquals(blackStrength, board.getStrength(Black));
assertEquals(whiteStrength, board.getStrength(White));
assertEquals(pieceStrength, piece.getStrength());
}
public void testColorCollections() {
board = Board.createInitialized();
List<Piece> blackPieces = board.getBlackPieces();
List<Piece> blackPieces = board.getPieces(Black);
assertEquals(9.0, blackPieces.get(0).getStrength());
assertEquals(5.0, blackPieces.get(1).getStrength());
assertEquals(5.0, blackPieces.get(2).getStrength());
......
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