-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBishop.java
40 lines (36 loc) · 1.06 KB
/
Bishop.java
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
import java.awt.Color;
import javax.swing.ImageIcon;
/** Represents a Bishop for chess. */
public class Bishop extends Piece {
/**
* Initializes a Bishop.
*
* @param color is a chess piece color.
*/
public Bishop(Color color) {
super(Name.B, color);
}
/** Checks if the piece can move to the position.
*
* @param position is a tile coordinate.
* @return true if valid move, else false.
*/
@Override
public boolean checkMove(Position start, Position end) {
int rowDiff = Math.abs(end.getX() - start.getX());
int colDiff = Math.abs(end.getY() - start.getY());
int lvlDiff = Math.abs(end.getZ() - start.getZ());
int maxTravel = Math.max(rowDiff, colDiff);
// Check for diagonal move > 0
return rowDiff == colDiff && rowDiff != 0 && lvlDiff <= maxTravel;
}
/** Gets the piece icon.
*
* @return ImageIcon is a chess piece icon.
*/
@Override
public ImageIcon getIcon() {
String fileName = this.getColor().equals(Color.WHITE) ? "bw" : "bb";
return new ImageIcon(getClass().getResource("/resources/images/" + fileName + ".png"));
}
}