Commit 125aa959 by Paktalin

Refactoring in Piece and Pawn

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