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