-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
70 lines (57 loc) · 1.93 KB
/
Square.java
File metadata and controls
70 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public class Square implements Comparable<Square> {
private int letter;
private int number;
private Piece piece;
public Square(int letter, int number) {
this.letter = letter;
this.number = number;
this.piece = null;
}
/** Returns this square's letter index on the table */
public int getLetter() {
return this.letter;
}
/** Returns this square's number index on the table */
public int getNumber() {
return this.number;
}
/** Returns the piece that sits on this square */
public Piece getPiece() {
return this.piece;
}
/** Sets a piece on this square */
public void setPiece(Piece piece) {
this.piece = piece;
}
public String getCoords() {
return "" + ((char) (this.letter+96)) + this.number;
}
/** Returns a string interpretation of the piece on this square */
public String toString() {
if ( this.piece == null ) return "❤"; // ❤ â�¤
// Return white pieces
if ( this.piece.getColor() == PieceColor.WHITE) {
if(this.piece instanceof Pawn) return "♙";
else if(this.piece instanceof Bishop) return "♗";
else if(this.piece instanceof Knight) return "♘";
else if(this.piece instanceof Rook) return "♖";
else if(this.piece instanceof King) return "♔";
else if(this.piece instanceof Queen) return "♕";
}
// Return black pieces
if( this.piece instanceof Pawn ) return "♟";
else if( this.piece instanceof Bishop )return "♝";
else if( this.piece instanceof Knight )return "♞";
else if( this.piece instanceof Rook ) return "♜";
else if( this.piece instanceof King ) return "♚";
else if( this.piece instanceof Queen ) return "♛";
// Program never reaches this far.
return "";
}
@Override
public int compareTo(Square square) {
if(square.getLetter() != this.getLetter())
return square.getLetter() - this.getLetter();
return square.getNumber() - this.getNumber();
}
}