Commit f5fa298f by Paktalin

Refactoring: created Position class

parent 2af1b9c5
...@@ -29,11 +29,11 @@ class Board { ...@@ -29,11 +29,11 @@ class Board {
} }
void placePiece(Piece piece, String position) { void placePiece(Piece piece, String position) {
layout.placePiece(piece, position); layout.placePiece(piece, Position.create(position));
} }
Piece getPieceAtPosition(String position) { Piece getPieceAtPosition(String position) {
return layout.getPieceAtPosition(position); return layout.getPieceAtPosition(Position.create(position));
} }
String print() { String print() {
......
...@@ -31,69 +31,30 @@ class BoardLayout { ...@@ -31,69 +31,30 @@ class BoardLayout {
} }
private void initializeKingRank(Piece.Color color, int row) { private void initializeKingRank(Piece.Color color, int row) {
placePiece(Piece.createRook(color), row, 0); placePiece(Piece.createRook(color), Position.create(row, 0));
placePiece(Piece.createKnight(color), row, 1); placePiece(Piece.createKnight(color), Position.create(row, 1));
placePiece(Piece.createBishop(color), row, 2); placePiece(Piece.createBishop(color), Position.create(row, 2));
placePiece(Piece.createQueen(color), row, 3); placePiece(Piece.createQueen(color), Position.create(row, 3));
placePiece(Piece.createKing(color), row, 4); placePiece(Piece.createKing(color), Position.create(row, 4));
placePiece(Piece.createBishop(color), row, 5); placePiece(Piece.createBishop(color), Position.create(row, 5));
placePiece(Piece.createKnight(color), row, 6); placePiece(Piece.createKnight(color), Position.create(row, 6));
placePiece(Piece.createRook(color), row, 7); placePiece(Piece.createRook(color), Position.create(row, 7));
} }
private void initializePawnRank(Piece.Color color, int row) { private void initializePawnRank(Piece.Color color, int row) {
for (int column = 0; column < COLUMN_COUNT; column++) for (int column = 0; column < COLUMN_COUNT; column++)
placePiece(Piece.createPawn(color), row, column); placePiece(Piece.createPawn(color), Position.create(row, column));
} }
void placePiece(Piece piece, String position) { void placePiece(Piece piece, Position position) {
placePiece(piece, retrieveRow(position), retrieveColumn(position)); piece.setPosition(position);
} piece.setStrength(board.getPieces(piece.getColor()));
private void placePiece(Piece piece, int row, int column) {
pieces[row][column] = piece;
setPieceStrength(piece, column);
board.addToCollection(piece); board.addToCollection(piece);
pieces[position.getRow()][position.getIntColumn()] = piece;
} }
private void setPieceStrength(Piece piece, int column) { Piece getPieceAtPosition(Position position) {
if (piece.isType(Piece.Type.Pawn)) return pieces[position.getRow()][position.getIntColumn()];
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() { String print() {
...@@ -110,13 +71,4 @@ class BoardLayout { ...@@ -110,13 +71,4 @@ class BoardLayout {
buffer.append("abcdefgh"); buffer.append("abcdefgh");
return buffer.toString(); 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;
}
} }
...@@ -2,6 +2,8 @@ package com.example.paktalin.agilejava_exercises; ...@@ -2,6 +2,8 @@ package com.example.paktalin.agilejava_exercises;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import java.util.List;
/** /**
* Created by Paktalin on 23/05/2018. * Created by Paktalin on 23/05/2018.
*/ */
...@@ -15,6 +17,7 @@ public class Piece implements Comparable<Piece>{ ...@@ -15,6 +17,7 @@ public class Piece implements Comparable<Piece>{
private Color color; private Color color;
private double strength; private double strength;
Position position;
private Piece() {} private Piece() {}
...@@ -23,6 +26,18 @@ public class Piece implements Comparable<Piece>{ ...@@ -23,6 +26,18 @@ public class Piece implements Comparable<Piece>{
this.type = type; this.type = type;
} }
void setPosition(Position position) {
this.position = position;
}
String getStringPosition() {
return position.toString();
}
Position getPosition() {
return position;
}
char getRepresentation() { char getRepresentation() {
char representation = getRepresentationLetter(); char representation = getRepresentationLetter();
if (this.isBlack()) if (this.isBlack())
...@@ -43,17 +58,25 @@ public class Piece implements Comparable<Piece>{ ...@@ -43,17 +58,25 @@ public class Piece implements Comparable<Piece>{
void setStrength(double strength) { void setStrength(double strength) {
this.strength = strength; this.strength = strength;
} }
void setStrength() {
strength = calculateStrength();
}
private double calculateStrength() { void setStrength(List<Piece> pieces) {
switch (this.type) { switch (this.type) {
case Queen: return 9; case Queen:
case Rook: return 5; strength = 9;
case Bishop: return 3; break;
case Knight: return 2.5; case Rook:
default: return 0; strength = 5;
break;
case Bishop:
strength = 3;
break;
case Knight:
strength = 2.5;
break;
case King:
strength = 0;
break;
default: Position.setPawnStrength(this, pieces);
} }
} }
......
package com.example.paktalin.agilejava_exercises;
import java.util.List;
/**
* Created by Paktalin on 31/05/2018.
*/
class Position {
private int row;
private char column;
Position() {}
Position(int row, char column) {
this.row = row;
this.column = column;
}
static Position create(String positionString) {
return create(retrieveRow(positionString), retrieveColumn(positionString));
}
static Position create(int row, int column) {
return new Position(row, intToChar(column));
}
private static int retrieveRow(String position) {
return Integer.parseInt(position.split("")[1]) - 1;
}
private static int retrieveColumn(String position) {
char file = position.toCharArray()[0];
return charToInt(file);
}
@Override
public String toString() {
return column + "" + (row + 1);
}
private static char intToChar(int i) {
return (char)(i + 'a');
}
private static int charToInt(char c) {
char firstColumnLetter = 'a';
return (int)c - (int)firstColumnLetter;
}
int getRow() {
return row;
}
char getColumn() {
return column;
}
int getIntColumn() {
return charToInt(column);
}
static void setPawnStrength(Piece currentPiece, List<Piece> pieces) {
currentPiece.setStrength(1.0);
for (Piece piece : pieces) {
if (pawnsOnSameColumn(currentPiece, piece)) {
piece.setStrength(0.5);
currentPiece.setStrength(0.5);
}
}
}
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return piece.getPosition().getColumn() == currentPiece.getPosition().getColumn() &&
piece.getPosition().getRow() != currentPiece.getPosition().getRow() &&
piece.getType() == currentPiece.getType();
}
}
...@@ -14,6 +14,7 @@ public class AllTests extends TestSuite { ...@@ -14,6 +14,7 @@ public class AllTests extends TestSuite {
suite.addTestSuite(PieceTest.class); suite.addTestSuite(PieceTest.class);
suite.addTestSuite(BoardTest.class); suite.addTestSuite(BoardTest.class);
suite.addTestSuite(CharacterTest.class); suite.addTestSuite(CharacterTest.class);
suite.addTestSuite(PositionTest.class);
return suite; return suite;
} }
......
...@@ -36,7 +36,7 @@ public class PieceTest extends TestCase { ...@@ -36,7 +36,7 @@ public class PieceTest extends TestCase {
assertEquals(1.0, board.getPieceAtPosition("a7").getStrength()); assertEquals(1.0, board.getPieceAtPosition("a7").getStrength());
board.placePiece(Piece.createPawn(Black), "a6"); board.placePiece(Piece.createPawn(Black), "a6");
assertEquals(0.5, board.getPieceAtPosition("a7").getStrength()); assertEquals(0.5, board.getPieceAtPosition("a6").getStrength());
} }
} }
package com.example.paktalin.agilejava_exercises;
import junit.framework.TestCase;
import java.util.List;
/**
* Created by Paktalin on 31/05/2018.
*/
public class PositionTest extends TestCase {
private Board board;
@Override
protected void setUp() throws Exception {
board = Board.createEmpty();
}
public void testCreate() {
Position position = Position.create("a8");
assertEquals("a8", position.toString());
assertEquals(0, position.getIntColumn());
assertEquals(7, position.getRow());
}
public void testStrengthByPosition() {
verifyStrength(Piece.createBishop(Piece.Color.Black), "b5", 3.0);
verifyStrength(Piece.createPawn(Piece.Color.White), "a2", 1.0);
verifyStrength(Piece.createPawn(Piece.Color.White), "a3", 0.5);
assertEquals(0.5, board.getPieceAtPosition("a2").getStrength());
}
private void verifyStrength(Piece piece, String position, double strength) {
board.placePiece(piece, position);
piece = board.getPieceAtPosition(position);
assertEquals(strength, piece.getStrength());
}
public void testSetPawnStrength() {
board.placePiece(Piece.createPawn(Piece.Color.Black), "a6");
board.placePiece(Piece.createKnight(Piece.Color.Black), "a5");
board.placePiece(Piece.createPawn(Piece.Color.Black), "a4");
Piece currentPiece = board.getPieceAtPosition("a4");
char column = currentPiece.getPosition().getColumn();
assertEquals('a', column);
int row = currentPiece.getPosition().getRow();
assertEquals(3, row);
List<Piece> pieces = board.getPieces(Piece.Color.Black);
Piece secondPiece = pieces.get(1);
//assertTrue((secondPiece.getPosition().getColumn() == column) && (secondPiece.getPosition().getRow() != row));
for (Piece piece : board.getPieces(Piece.Color.Black)) {
Position position = piece.getPosition();
if ((position.getColumn() == column) && (position.getRow() != row)) {
piece.setStrength(0.5);
currentPiece.setStrength(0.5);
}
}
}
}
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