Commit a86e112e by Paktalin

Exercise 6.5 done

parent d2093f3a
......@@ -9,15 +9,13 @@ import java.util.List;
*/
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;
private double strength;
Position position;
private Position position;
private Piece() {}
......@@ -26,12 +24,29 @@ public class Piece implements Comparable<Piece>{
this.type = type;
}
void setPosition(Position position) {
this.position = position;
public enum Type {
Pawn(1, 'p'), Knight(2.5, 'n'), Rook(5, 'r'), Bishop(3, 'b'), Queen(9, 'q'), King(0, 'k');
private double strength;
private char representation;
Type(double strength, char representation) {
this.strength = strength;
this.representation = representation;
}
public char getRepresentation() {
return representation;
}
public double getStrength() {
return strength;
}
}
String getStringPosition() {
return position.toString();
void setPosition(Position position) {
this.position = position;
}
Position getPosition() {
......@@ -39,45 +54,20 @@ public class Piece implements Comparable<Piece>{
}
char getRepresentation() {
char representation = getRepresentationLetter();
char representation = this.type.getRepresentation();
if (this.isBlack())
return Character.toUpperCase(representation);
return representation;
}
private char getRepresentationLetter() {
switch (this.type) {
case Knight: return 'n';
case King: return 'k';
case Queen: return 'q';
case Rook: return 'r';
case Bishop: return 'b';
default: return 'p';
}
}
void setStrength(double strength) {
this.strength = strength;
}
void setStrength(List<Piece> pieces) {
switch (this.type) {
case Queen:
strength = 9;
break;
case Rook:
strength = 5;
break;
case Bishop:
strength = 3;
break;
case Knight:
strength = 2.5;
break;
case King:
strength = 0;
break;
default: Position.setPawnStrength(this, pieces);
}
if (this.type != Type.Pawn)
strength = this.type.getStrength();
else Position.setPawnStrength(this, pieces);
}
@Override
......
......@@ -11,9 +11,9 @@ class Position {
private int row;
private char column;
Position() {}
private Position() {}
Position(int row, char column) {
private Position(int row, char column) {
this.row = row;
this.column = column;
}
......
......@@ -11,22 +11,22 @@ import java.util.List;
class Side {
private List<Piece> pieces = new ArrayList<>();
public List<Piece> getPieces() {
List<Piece> getPieces() {
return pieces;
}
public double getStrength() {
double getStrength() {
double strength = 0.0;
for (Piece piece : pieces)
strength += piece.getStrength();
return strength;
}
public int getPiecesCount() {
int getPiecesCount() {
return pieces.size();
}
public int getPiecesCount(Piece.Type type) {
int getPiecesCount(Piece.Type type) {
int count = 0;
for (Piece piece : pieces)
......@@ -36,7 +36,7 @@ class Side {
return count;
}
public void addPiece(Piece piece) {
void addPiece(Piece piece) {
pieces.add(piece);
Collections.sort(pieces);
}
......
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