-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
131 lines (113 loc) · 5.24 KB
/
Copy pathKing.java
File metadata and controls
131 lines (113 loc) · 5.24 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
121
122
123
124
125
126
127
128
129
130
131
import java.util.ArrayList;
/**
* Defines data members and methods of each king piece.
*/
public class King extends Piece
{
boolean hasMoved;
/**
* Constructor for objects of class King
*/
public King(String color)
{
super(color);
this.hasMoved = false;
}
public ArrayList<Square> getAvailableMoves(Square s, GameBoard b)
{
ArrayList<Square> moves = new ArrayList<Square>();
Square up = b.getSquareAbove(s);
Square down = b.getSquareBelow(s);
//up
if(up != null && !up.containsPiece()){
moves.add(up);
}
//down
if(down != null && !down.containsPiece()){
moves.add(down);
}
//left
if(b.getSquareLeft(s) != null && !b.getSquareLeft(s).containsPiece()){
moves.add(b.getSquareLeft(s));
}
//right
if(b.getSquareRight(s) != null && !b.getSquareRight(s).containsPiece()){
moves.add(b.getSquareRight(s));
}
//upper left
if(b.getSquareLeft(up) != null && !b.getSquareLeft(up).containsPiece()){
moves.add(b.getSquareLeft(up));
}
//upper right
if(b.getSquareRight(up) != null && !b.getSquareRight(up).containsPiece()){
moves.add(b.getSquareRight(up));
}
//bottom left
if(b.getSquareLeft(down) != null && !b.getSquareLeft(down).containsPiece()){
moves.add(b.getSquareLeft(down));
}
//bottom right
if(b.getSquareRight(down) != null && !b.getSquareRight(down).containsPiece()){
moves.add(b.getSquareRight(down));
}
//castling
if(!this.hasMoved)
{
if(!b.getSquareRight(s).containsPiece() &&
!b.getSquareRight(b.getSquareRight(s)).containsPiece() &&
b.getSquareRight(b.getSquareRight(b.getSquareRight(s))).getPiece() instanceof Rook)
{
Rook rook = (Rook)(b.getSquareRight(b.getSquareRight(b.getSquareRight(s))).getPiece());
if(!rook.hasMoved())
{
//Must make sure king is not currently in check,
//moving through check, or moving into check.
moves.add(b.getSquareRight(b.getSquareRight(s)));
}
}
if(!b.getSquareLeft(s).containsPiece() &&
!b.getSquareLeft(b.getSquareLeft(s)).containsPiece() &&
!b.getSquareLeft(b.getSquareLeft(b.getSquareLeft(s))).containsPiece() &&
b.getSquareLeft(b.getSquareLeft(b.getSquareLeft(b.getSquareLeft(s)))).getPiece() instanceof Rook)
{
Rook rook = (Rook)(b.getSquareLeft(b.getSquareLeft(b.getSquareLeft(b.getSquareLeft(s)))).getPiece());
if(!rook.hasMoved())
{
//Must make sure king is not currently in check,
//moving through check, or moving into check.
moves.add(b.getSquareLeft(b.getSquareLeft(s)));
}
}
}
return moves;
}
public ArrayList<Square> getAvailableCaptures(Square s, GameBoard b)
{
ArrayList<Square> captures = new ArrayList<Square>();
if(b.getSquareAbove(s) != null && b.getSquareAbove(s).containsPiece() && !b.getSquareAbove(s).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareAbove(s));
}
if(b.getSquareBelow(s) != null && b.getSquareBelow(s).containsPiece() && !b.getSquareBelow(s).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareBelow(s));
}
if(b.getSquareLeft(s) != null && b.getSquareLeft(s).containsPiece() && !b.getSquareLeft(s).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareLeft(s));
}
if(b.getSquareRight(s) != null && b.getSquareRight(s).containsPiece() && !b.getSquareRight(s).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareRight(s));
}
if(b.getSquareLeft(b.getSquareAbove(s)) != null && b.getSquareLeft(b.getSquareAbove(s)).containsPiece() && !b.getSquareLeft(b.getSquareAbove(s)).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareLeft(b.getSquareAbove(s)));
}
if(b.getSquareRight(b.getSquareAbove(s)) != null && b.getSquareRight(b.getSquareAbove(s)).containsPiece() && !b.getSquareRight(b.getSquareAbove(s)).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareRight(b.getSquareAbove(s)));
}
if(b.getSquareLeft(b.getSquareBelow(s)) != null && b.getSquareLeft(b.getSquareBelow(s)).containsPiece() && !b.getSquareLeft(b.getSquareBelow(s)).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareLeft(b.getSquareBelow(s)));
}
if(b.getSquareLeft(b.getSquareAbove(s)) != null && b.getSquareRight(b.getSquareBelow(s)).containsPiece() && !b.getSquareRight(b.getSquareBelow(s)).getPiece().getColor().equals(this.color)) {
captures.add(b.getSquareRight(b.getSquareBelow(s)));
}
return captures;
}
}