-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
75 lines (64 loc) · 2.63 KB
/
Copy pathPawn.java
File metadata and controls
75 lines (64 loc) · 2.63 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
import java.util.ArrayList;
/**
* Defines data members and methods of each pawn piece.
*/
public class Pawn extends Piece
{
boolean isFirstMove;
/**
* Constructor for objects of class Pawn
*/
public Pawn(String color)
{
super(color);
isFirstMove = true;
}
public ArrayList<Square> getAvailableMoves(Square s, GameBoard b)
{
ArrayList<Square> moves = new ArrayList<Square>();
//white
if(this.color == "white" && b.getSquareAbove(s) != null && !b.getSquareAbove(s).containsPiece()) {
moves.add(b.getSquareAbove(s));
//up two (if hasn't moved)
if(this.isFirstMove && !b.getSquareAbove(moves.get(0)).containsPiece()) {
moves.add(b.getSquareAbove(moves.get(0)));
}
}
//black
else if(this.color == "black" && b.getSquareBelow(s) != null && !b.getSquareBelow(s).containsPiece()) {
moves.add(b.getSquareBelow(s));
//down two (if hasn't moved)
if(this.isFirstMove && !b.getSquareBelow(moves.get(0)).containsPiece()) {
moves.add(b.getSquareBelow(moves.get(0)));
}
}
return moves;
}
public ArrayList<Square> getAvailableCaptures(Square s, GameBoard b)
{
ArrayList<Square> captures = new ArrayList<Square>();
//regular capture
//white
if(this.color == "white" && b.getSquareAbove(s) != null) {
Square above = b.getSquareAbove(s);
if(b.getSquareLeft(above) != null && b.getSquareLeft(above).containsPiece() && b.getSquareLeft(above).getPiece().getColor() == "black") {
captures.add(b.getSquareLeft(above));
}
if(b.getSquareRight(above) != null && b.getSquareRight(above).containsPiece() && b.getSquareRight(above).getPiece().getColor() == "black") {
captures.add(b.getSquareRight(above));
}
}
//black
else if(this.color == "black" && b.getSquareBelow(s) != null) {
Square below = b.getSquareBelow(s);
if(b.getSquareLeft(below) != null && b.getSquareLeft(below).containsPiece() && b.getSquareLeft(below).getPiece().getColor() == "white") {
captures.add(b.getSquareLeft(below));
}
if(b.getSquareRight(below) != null && b.getSquareRight(below).containsPiece() && b.getSquareRight(below).getPiece().getColor() == "white") {
captures.add(b.getSquareRight(below));
}
}
//TODO: en passant
return captures;
}
}