refactored pawn movement + implemented knight movement
This commit is contained in:
@@ -26,6 +26,10 @@ public class Board {
|
||||
}
|
||||
|
||||
public boolean isOpen(Position pos) {
|
||||
return this.board[pos.x][pos.y] == null;
|
||||
return this.getPiece(pos) == null;
|
||||
}
|
||||
|
||||
public Piece getPiece(Position pos) {
|
||||
return this.board[pos.x][pos.y];
|
||||
}
|
||||
}
|
||||
|
||||
13
Knight.java
13
Knight.java
@@ -12,10 +12,17 @@ public class Knight extends Piece {
|
||||
public ArrayList<Position> getLegalMoves(Board board) {
|
||||
ArrayList<Position> positions = new ArrayList<Position>();
|
||||
|
||||
for (int i : xDir) {
|
||||
for (int j : yDir) {
|
||||
Position test = new Position(pos.x + i, pos.y + j);
|
||||
for (int i = 0; i < xDir.length; i++) {
|
||||
int xPlus = xDir[i];
|
||||
int yPlus = yDir[i];
|
||||
Position test = new Position(pos.x + xPlus, pos.y + yPlus);
|
||||
if (!Board.inBounds(test)) continue;
|
||||
if (board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
continue;
|
||||
}
|
||||
if (!board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
27
Pawn.java
27
Pawn.java
@@ -13,27 +13,28 @@ public class Pawn extends Piece {
|
||||
|
||||
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.equals(this.color)) {
|
||||
positions.add(new Position(pos.x + 1, pos.y + colorDir));
|
||||
Position test = new Position(pos.x + 1, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
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.equals(this.color)) {
|
||||
positions.add(new Position(pos.x - 1, pos.y + colorDir));
|
||||
|
||||
test = new Position(pos.x - 1, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && !board.isOpen(test) && !board.getPiece(test).colorMatches(this)) {
|
||||
positions.add(test);
|
||||
}
|
||||
|
||||
// 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));
|
||||
test = new Position(pos.x, pos.y + colorDir);
|
||||
if (Board.inBounds(test) && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
} 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));
|
||||
test = new Position(pos.x, pos.y + 2 * colorDir);
|
||||
if (!hasMoved && board.isOpen(test)) {
|
||||
positions.add(test);
|
||||
}
|
||||
return positions;
|
||||
}
|
||||
|
||||
@@ -21,4 +21,8 @@ public class Piece {
|
||||
System.out.println("parent method not overriden");
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean colorMatches(Piece p) {
|
||||
return this.color.equals(p.color);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user