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