-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
120 lines (102 loc) · 2.97 KB
/
Square.java
File metadata and controls
120 lines (102 loc) · 2.97 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package chess;
import java.awt.*;
public class Square {
private String name;
private int row, col; // (x,y) coordinate in squaresBoard
private Color defaultColor;
private Color highlightColor;
private Piece piece;
private boolean attackedByWhite, attackedByBlack;
private boolean enPassantMove;
public Square (String name) {
this.name = name;
switch (name.charAt(0)) {
case 'a': col = 1; break;
case 'b': col = 2; break;
case 'c': col = 3; break;
case 'd': col = 4; break;
case 'e': col = 5; break;
case 'f': col = 6; break;
case 'g': col = 7; break;
case 'h': col = 8; break;
default:
System.out.println("Error: Square.Square()");
}
switch (name.charAt(1)) {
case '1': row = 8; break;
case '2': row = 7; break;
case '3': row = 6; break;
case '4': row = 5; break;
case '5': row = 4; break;
case '6': row = 3; break;
case '7': row = 2; break;
case '8': row = 1; break;
default:
System.out.println("Error: Square.Square()");
}
if ((row+col)%2 == 1) {
defaultColor = new Color(139, 101, 8); // yellow
} else {
defaultColor = new Color(255, 255, 0); // brown
}
highlightColor = defaultColor;
piece = null;
attackedByWhite = attackedByBlack = false;
enPassantMove = false;
}
public String getName() {
return name;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public Piece getPiece() {
return piece;
}
public void setPiece(Piece piece) {
if (piece != null) {
this.piece = piece;
this.piece.setInPlay(true);
this.piece.setSquare(this);
} else {
this.piece = null;
}
}
public boolean hasPiece() {
if (piece != null) {
return true;
} else {
return false;
}
}
public boolean isAttackedByWhite() {
return attackedByWhite;
}
public void setAttackedByWhite(boolean attackedByWhite) {
this.attackedByWhite = attackedByWhite;
}
public boolean isAttackedByBlack() {
return attackedByBlack;
}
public void setAttackedByBlack(boolean attackedByBlack) {
this.attackedByBlack = attackedByBlack;
}
public Color getDefaultColor() {
return defaultColor;
}
public Color getHighlightColor() {
return highlightColor;
}
public void setHighlightColor(Color highlightColor) {
this.highlightColor = highlightColor;
}
public boolean isEnPassantMove() {
return enPassantMove;
}
public void setEnPassantMove(boolean enPassantMove) {
this.enPassantMove = enPassantMove;
}
}