Commit 502537f3 by Paktalin

Separated the classes Board and BoardLayout. Exercise 9 complete

parent e27c92e5
......@@ -3,17 +3,13 @@ package com.example.paktalin.agilejava_exercises;
import java.util.List;
import static com.example.paktalin.agilejava_exercises.Piece.Color.*;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/**
* Created by Paktalin on 23/05/2018.
*/
class Board {
private static final int ROW_COUNT = 8;
private static final int COLUMN_COUNT = 8;
private Piece[][] pieces = new Piece[ROW_COUNT][COLUMN_COUNT];
private BoardLayout layout;
private Side whiteSide = new Side();
private Side blackSide = new Side();
......@@ -21,123 +17,31 @@ class Board {
private Board() {}
static Board createEmpty() {
return new Board();
}
static Board createInitialized() {
Board board = new Board();
board.initialize();
board.layout = BoardLayout.create(board);
return board;
}
private void initialize() {
initializeKingRank(Black, 0);
initializePawnRank(Black, 1);
initializePawnRank(White, 6);
initializeKingRank(White, 7);
}
private void initializeKingRank(Piece.Color color, int row) {
placePiece(Piece.createRook(color), row, 'a');
placePiece(Piece.createKnight(color), row, 'b');
placePiece(Piece.createBishop(color), row, 'c');
placePiece(Piece.createQueen(color), row, 'd');
placePiece(Piece.createKing(color), row, 'e');
placePiece(Piece.createBishop(color), row, 'f');
placePiece(Piece.createKnight(color), row, 'g');
placePiece(Piece.createRook(color), row, 'h');
}
private void initializePawnRank(Piece.Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; column++)
placePiece(Piece.createPawn(color), row, column);
static Board createInitialized() {
Board board = Board.createEmpty();
board.layout.initialize();
return board;
}
void placePiece(Piece piece, String position) {
placePiece(piece, retrieveRow(position), retrieveColumn(position));
layout.placePiece(piece, position);
}
private void placePiece(Piece piece, int row, char file) {
placePiece(piece, row, fileToColumn(file));
Piece getPieceAtPosition(String position) {
return layout.getPieceAtPosition(position);
}
private void placePiece(Piece piece, int row, int column) {
pieces[row][column] = piece;
setPieceStrength(piece, column);
addToCollection(piece);
String print() {
return layout.print();
}
void addToCollection(Piece piece) {
getSide(piece.getColor()).addPiece(piece);
}
private void setPieceStrength(Piece piece, int column) {
if (piece.isType(Piece.Type.Pawn))
updatePawnsStrength(piece.getColor(), column);
else piece.setStrength();
}
private void updatePawnsStrength(Piece.Color color, int column) {
double strength;
if (pawnsPerColumn(color, column) > 1)
strength = 0.5;
else strength = 1.0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
currentPiece.setStrength(strength);
}
}
private int pawnsPerColumn(Piece.Color color, int column) {
int count = 0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
count++;
}
return count;
}
private int retrieveRow(String position) {
return ROW_COUNT - Integer.parseInt(position.split("")[1]);
}
private int retrieveColumn(String position) {
return fileToColumn(position.toCharArray()[0]);
}
String print() {
StringBuilder buffer = new StringBuilder();
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
if (pieces[row][column] == null)
buffer.append(".");
else
buffer.append(pieces[row][column].getRepresentation());
}
buffer.append(" " + (ROW_COUNT - row) + NEW_LINE);
}
buffer.append("abcdefgh");
return buffer.toString();
}
private int fileToColumn(char file) {
char firstColumnLetter = 'a';
return (int)file - (int)firstColumnLetter;
}
private Piece getPieceAtPosition(int row, int column) {
return pieces[row][column];
}
Piece getPieceAtPosition(String position) {
return getPieceAtPosition(retrieveRow(position), retrieveColumn(position));
}
Side getSide(Piece.Color color){
private Side getSide(Piece.Color color){
if (color == White)
return whiteSide;
return blackSide;
......
package com.example.paktalin.agilejava_exercises;
import static com.example.paktalin.agilejava_exercises.Piece.Color.Black;
import static com.example.paktalin.agilejava_exercises.Piece.Color.White;
import static com.example.paktalin.agilejava_exercises.util.StringUtil.NEW_LINE;
/**
* Created by Paktalin on 27/05/2018.
*/
class BoardLayout {
private static final int ROW_COUNT = 8;
private static final int COLUMN_COUNT = 8;
private Piece[][] pieces = new Piece[ROW_COUNT][COLUMN_COUNT];
private Board board;
private BoardLayout() {}
void initialize() {
initializeKingRank(Black, 0);
initializePawnRank(Black, 1);
initializePawnRank(White, 6);
initializeKingRank(White, 7);
}
static BoardLayout create(Board board) {
BoardLayout layout = new BoardLayout();
layout.board = board;
return layout;
}
private void initializeKingRank(Piece.Color color, int row) {
placePiece(Piece.createRook(color), row, 0);
placePiece(Piece.createKnight(color), row, 1);
placePiece(Piece.createBishop(color), row, 2);
placePiece(Piece.createQueen(color), row, 3);
placePiece(Piece.createKing(color), row, 4);
placePiece(Piece.createBishop(color), row, 5);
placePiece(Piece.createKnight(color), row, 6);
placePiece(Piece.createRook(color), row, 7);
}
private void initializePawnRank(Piece.Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; 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, int column) {
pieces[row][column] = piece;
setPieceStrength(piece, column);
board.addToCollection(piece);
}
private void setPieceStrength(Piece piece, int column) {
if (piece.isType(Piece.Type.Pawn))
updatePawnsStrength(piece.getColor(), column);
else piece.setStrength();
}
private void updatePawnsStrength(Piece.Color color, int column) {
double strength;
if (pawnsPerColumn(color, column) > 1)
strength = 0.5;
else strength = 1.0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
currentPiece.setStrength(strength);
}
}
private int pawnsPerColumn(Piece.Color color, int column) {
int count = 0;
for (int row = 0; row < ROW_COUNT; row++) {
Piece currentPiece = getPieceAtPosition(row, column);
if (currentPiece != null)
if (currentPiece.isType(Piece.Type.Pawn) && currentPiece.isColor(color))
count++;
}
return count;
}
private Piece getPieceAtPosition(int row, int column) {
return pieces[row][column];
}
Piece getPieceAtPosition(String position) {
return getPieceAtPosition(retrieveRow(position), retrieveColumn(position));
}
String print() {
StringBuilder buffer = new StringBuilder();
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COLUMN_COUNT; column++) {
if (pieces[row][column] == null)
buffer.append(".");
else
buffer.append(pieces[row][column].getRepresentation());
}
buffer.append(" " + (ROW_COUNT - row) + NEW_LINE);
}
buffer.append("abcdefgh");
return buffer.toString();
}
private int retrieveRow(String position) {
return ROW_COUNT - Integer.parseInt(position.split("")[1]);
}
private int retrieveColumn(String position) {
char file = position.toCharArray()[0];
char firstColumnLetter = 'a';
return (int)file - (int)firstColumnLetter;
}
}
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