pawn moves check
This commit is contained in:
35
Pawn.java
35
Pawn.java
@@ -1,9 +1,40 @@
|
||||
import java.awt.*;
|
||||
import java.util.*;
|
||||
import javax.swing.ImageIcon;
|
||||
|
||||
public class Pawn extends Piece {
|
||||
boolean hasMoved;
|
||||
int colorDir;
|
||||
|
||||
public Pawn(int x, int y, String color) {
|
||||
super(x, y, new ImageIcon("Sprites/" + color + "/pawn.png").getImage());
|
||||
super(x, y, color, new ImageIcon("sprites/" + color + "/pawn.png").getImage());
|
||||
hasMoved = false;
|
||||
colorDir = color.equals("White") ? -1 : 1;
|
||||
}
|
||||
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
// diagonal moves (captures)
|
||||
if (Board.inBounds(new Position(pos.x + 1, pos.y + colorDir))
|
||||
&& !board.isOpen(new Position(pos.x + 1, pos.y + colorDir))
|
||||
&& board.board[pos.x + 1][pos.y + colorDir].color != this.color) {
|
||||
positions.add(new Position(pos.x + 1, pos.y + colorDir));
|
||||
}
|
||||
if (Board.inBounds(new Position(pos.x - 1, pos.y + colorDir))
|
||||
&& !board.isOpen(new Position(pos.x - 1, pos.y + colorDir))
|
||||
&& board.board[pos.x + 1][pos.y + colorDir].color != this.color) {
|
||||
positions.add(new Position(pos.x - 1, pos.y + colorDir));
|
||||
}
|
||||
|
||||
// one square in front: if blocked return early
|
||||
if (Board.inBounds(new Position(pos.x, pos.y + colorDir))
|
||||
&& board.isOpen(new Position(pos.x, pos.y + colorDir))) {
|
||||
positions.add(new Position(pos.x, pos.y + colorDir));
|
||||
} else return positions;
|
||||
|
||||
// two squares in front
|
||||
if (!hasMoved && board.isOpen(new Position(pos.x, pos.y + 2 * colorDir))) {
|
||||
positions.add(new Position(pos.x, pos.y + 2 * colorDir));
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user