Commit a86e112e by Paktalin

Exercise 6.5 done

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