-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
56 lines (47 loc) · 1.85 KB
/
Copy pathPawn.java
File metadata and controls
56 lines (47 loc) · 1.85 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
package edu.unlv.mis768.finalproject;
/**
* This class designs the pawn piece
* @author William Brasic and Sergio Torres
*
*/
public class Pawn extends Piece{
/**
* Constuctor for pawn class
* @param whitePiece
*/
public Pawn(boolean whitePiece){
super(whitePiece);
}
/**
* This method determines if the pawn can legally make the move.
* @param board
* @param start
* @param end
* @return boolean value if legal move; false otherwise
*/
public boolean legalMove(ChessBoard board, Square start, Square end){
boolean flag = true;
if (end.hasPiece()) {
// if the piece at Square end is the same color as the pawn trying to move, return false, i.e., cannot move
if (end.getPiece().isWhitePiece() == this.isWhitePiece()) {
flag = false;
}
// white pawn can move one space diagonally if it is trying to take black piece
else if ((end.getPiece().isWhitePiece()) && (this.isWhitePiece()) &&
(Math.abs(start.getX() - end.getX()) == 1) &&
(Math.abs(start.getY() - end.getY()) == 1))
flag = true;
// black pawn can move one space diagonally if it is trying to take white piece
else if ((end.getPiece().isWhitePiece()) && (!this.isWhitePiece()) &&
(Math.abs(start.getX() - end.getX()) == 1) &&
(Math.abs(start.getY() - end.getY()) == 1))
flag = true;
}
// if pawn is just trying to move regularly, it can do so if and only if it moves one space up vertically
else if ((Math.abs(start.getX() - end.getX()) == 0) && (Math.abs(start.getY() - end.getY()) == 1)) {
flag = true;
}
// otherwise, pawn can only move
return flag;
}
}