Commit f0607e45 by Paktalin

Lesson 5 Exercise 8 complete

parent af649a49
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.util.StringUtil.NEW_LINE;
/**
......@@ -12,6 +16,9 @@ class Board {
private Piece[][] pieces = new Piece[ROW_COUNT][COLUMN_COUNT];
private List<Piece> blackPieces = new ArrayList<>();
private List<Piece> whitePieces = new ArrayList<>();
private Board() {}
static Board createEmpty() {
......@@ -53,12 +60,23 @@ class Board {
private void placePiece(Piece piece, int row, int column) {
pieces[row][column] = piece;
setPieceStrength(piece, column);
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);
}
}
private void setPieceStrength(Piece piece, int column) {
if (piece.isType(Piece.Type.Pawn))
updatePawnsStrength(piece.getColor(), column);
......@@ -100,37 +118,25 @@ class Board {
return fileToColumn(position.toCharArray()[0]);
}
int pieceCount() {
int getPiecesCount() {
return getBlackPiecesCount() + getWhitePiecesCount();
}
int getPiecesCount(Piece.Type type, List<Piece> pieces) {
int count = 0;
for (int row = 0; row < ROW_COUNT; row++)
for (int column = 0; column < COLUMN_COUNT; column++)
if (pieces[row][column] != null)
for (Piece piece : pieces)
if (piece.isType(type))
count++;
return count;
}
int pieceCount(Piece.Color color) {
int counter = 0;
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++;
int getWhitePiecesCount(Piece.Type type) {
return getPiecesCount(type, whitePieces);
}
return counter;
}
int pieceCount(Piece.Type type, Piece.Color color) {
int count = 0;
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++;
}
}
return count;
int getBlackPiecesCount(Piece.Type type) {
return getPiecesCount(type, blackPieces);
}
String print() {
......@@ -175,10 +181,24 @@ class Board {
return strength;
}
List<Piece> getBlackPieces() {
return blackPieces;
}
List<Piece> getWhitePieces() {
return whitePieces;
}
double getBlackStrength() {
return getStrength(Piece.Color.Black);
}
double getWhiteStrength() {
return getStrength(Piece.Color.White);
}
int getBlackPiecesCount() {
return blackPieces.size();
}
int getWhitePiecesCount() {
return whitePieces.size();
}
}
\ No newline at end of file
package com.example.paktalin.agilejava_exercises;
import android.support.annotation.NonNull;
/**
* Created by Paktalin on 23/05/2018.
*/
public class Piece {
enum Type {Pawn, Knight, Rook, Bishop, Queen, King}
enum Color {White, Black}
public class Piece implements Comparable<Piece>{
public enum Type {Pawn, Knight, Rook, Bishop, Queen, King}
public enum Color {White, Black}
private Type type;
private Color color;
......@@ -61,6 +64,15 @@ public class Piece {
strength = 0.0;
}
@Override
public int compareTo(@NonNull Piece that) {
if (this.getStrength() > that.getStrength())
return -1;
if (this.getStrength() < that.getStrength())
return 1;
return 0;
}
void setStrength(double strength) {
this.strength = strength;
}
......
......@@ -2,6 +2,8 @@ package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase;
import java.util.List;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.Piece.Type.*;
......@@ -15,34 +17,9 @@ public class BoardTest extends TestCase {
public void testCreate() {
board = Board.createInitialized();
assertEquals(32, board.pieceCount());
assertEquals(16, board.pieceCount(Black));
assertEquals(16, board.pieceCount(White));
}
public void testCountPieces() {
board = Board.createInitialized();
assertEquals(8, board.pieceCount(Pawn, White));
assertEquals(1, board.pieceCount(King, Black));
assertEquals(1, board.pieceCount(Queen, White));
assertEquals(2, board.pieceCount(Bishop, Black));
assertEquals(2, board.pieceCount(Knight, White));
assertEquals(2, board.pieceCount(Rook, Black));
}
public void testPieceLocation() {
board = Board.createInitialized();
Piece piece = board.getPieceAtPosition("a8");
assertEquals(Rook, piece.getType());
assertEquals(Black, piece.getColor());
piece = board.getPieceAtPosition("e1");
assertEquals(King, piece.getType());
assertEquals(White, piece.getColor());
assertEquals(32, board.getPiecesCount());
assertEquals(16, board.getBlackPiecesCount());
assertEquals(16, board.getWhitePiecesCount());
}
public void testPlacePieces() {
......@@ -80,6 +57,10 @@ public class BoardTest extends TestCase {
verifyStrength(Piece.createKnight(White), "f4", 20.0, 19.5, 2.5);
verifyPrint();
}
private void verifyPrint() {
assertEquals(
".KR..... 8\n" +
"P.PB.... 7\n" +
......@@ -100,4 +81,17 @@ public class BoardTest extends TestCase {
assertEquals(whiteStrength, board.getWhiteStrength());
assertEquals(pieceStrength, piece.getStrength());
}
public void testColorCollections() {
board = Board.createInitialized();
List<Piece> blackPieces = board.getBlackPieces();
assertEquals(9.0, blackPieces.get(0).getStrength());
assertEquals(5.0, blackPieces.get(1).getStrength());
assertEquals(5.0, blackPieces.get(2).getStrength());
assertEquals(3.0, blackPieces.get(3).getStrength());
assertEquals(3.0, blackPieces.get(4).getStrength());
assertEquals(2.5, blackPieces.get(5).getStrength());
assertEquals(2.5, blackPieces.get(5).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