Commit 125aa959 by Paktalin

Refactoring in Piece and Pawn

parent 63f13924
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
/**
* Created by Paktalin on 01/06/2018.
*/
public class Bishop extends Piece {
public Bishop(Piece.Color color) {
strength = 3;
representation = 'b';
......
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
/**
* Created by Paktalin on 01/06/2018.
*/
......
package com.example.paktalin.agilejava_exercises.pieces;
import com.example.paktalin.agilejava_exercises.Position;
import java.util.List;
/**
......@@ -27,8 +25,16 @@ public class Pawn extends Piece {
}
private static boolean pawnsOnSameColumn(Piece currentPiece, Piece piece) {
return piece.getPosition().getColumn() == currentPiece.getPosition().getColumn() &&
piece.getPosition().getRow() != currentPiece.getPosition().getRow() &&
piece.getClass() == currentPiece.getClass();
return sameColumn(currentPiece, piece) &&
sameRow(currentPiece, piece) &&
piece.getClass() == Pawn.class;
}
private static boolean sameColumn(Piece piece1, Piece piece2) {
return piece2.getPosition().getColumn() == piece1.getPosition().getColumn();
}
private static boolean sameRow(Piece piece1, Piece piece2) {
return piece2.getPosition().getRow() == piece1.getPosition().getRow();
}
}
......@@ -17,22 +17,25 @@ public abstract class Piece implements Comparable<Piece> {
public enum Color {White, Black}
Color color;
private Position position;
double strength;
private Position position;
MoveStrategy moveStrategy;
char representation;
Piece() {}
public void setPosition(Position position) {
this.position = position;
@Override
public int compareTo(@NonNull Piece that) {
if (this.getStrength() > that.getStrength())
return -1;
if (this.getStrength() < that.getStrength())
return 1;
return 0;
}
public char getRepresentation() {
if (isBlack())
return Character.toUpperCase(representation);
return representation;
public void setPosition(Position position) {
this.position = position;
}
public void setStrength(List<Piece> pieces) {}
......@@ -41,39 +44,37 @@ public abstract class Piece implements Comparable<Piece> {
this.strength = strength;
}
@Override
public int compareTo(@NonNull Piece that) {
if (this.getStrength() > that.getStrength())
return -1;
if (this.getStrength() < that.getStrength())
return 1;
return 0;
}
public boolean isMovable(Position from, Position to){
return moveStrategy.isMovable(from, to);
public char getRepresentation() {
if (isBlack())
return Character.toUpperCase(representation);
return representation;
}
public double getStrength() {
return this.strength;
return strength;
}
public Color getColor() {
return color;
}
public Position getPosition() {
return position;
}
boolean isColor(Color color) {
return this.color == color;
}
public boolean isWhite() {
return isColor(Color.White);
return this.color == Color.White;
}
public boolean isBlack() {
return isColor(Color.Black);
return this.color == Color.Black;
}
public boolean isAtPosition(Position position) {
return this.position.equals(position);
}
public boolean isMovable(Position from, Position to){
return moveStrategy.isMovable(from, to);
}
}
\ No newline at end of file
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