pawnlogic
This commit is contained in:
@@ -104,6 +104,64 @@ class Board {
|
||||
|
||||
return piece.moves(coord, this);
|
||||
}
|
||||
|
||||
// Is the selected space free? (either nothing there, or an enemy piece).
|
||||
boolean isFree(Coord coord, Util.Col col) {
|
||||
return this.get(coord) == null || this.get(coord).col != col;
|
||||
}
|
||||
|
||||
// Is the selected space actually free?
|
||||
boolean isActuallyFree(Coord coord) { return this.get(coord) == null; }
|
||||
|
||||
// Is the selected space occupied by an enemy piece?
|
||||
boolean isOccupiedByEnemy(Coord coord, Util.Col col) {
|
||||
Piece piece = this.get(coord);
|
||||
return piece != null && piece.col != col;
|
||||
}
|
||||
|
||||
// Return the line of free spaces above.
|
||||
ArrayList<Coord> getLineAbove(Coord coord) {
|
||||
ArrayList<Coord> line = new ArrayList<>();
|
||||
for (int y = coord.y - 1; y >= 0; y--) {
|
||||
Coord next = new Coord(coord.x, y);
|
||||
if (!this.isFree(next, Util.Col.WHITE)) break;
|
||||
line.add(next);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
// Return the line of free spaces below.
|
||||
ArrayList<Coord> getLineBelow(Coord coord) {
|
||||
ArrayList<Coord> line = new ArrayList<>();
|
||||
for (int y = coord.y + 1; y < Util.boardW; y++) {
|
||||
Coord next = new Coord(coord.x, y);
|
||||
if (!this.isFree(next, Util.Col.WHITE)) break;
|
||||
line.add(next);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
// Return the line of free spaces to the left.
|
||||
ArrayList<Coord> getLineLeft(Coord coord) {
|
||||
ArrayList<Coord> line = new ArrayList<>();
|
||||
for (int x = coord.x - 1; x >= 0; x--) {
|
||||
Coord next = new Coord(x, coord.y);
|
||||
if (!this.isFree(next, Util.Col.WHITE)) break;
|
||||
line.add(next);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
// Return the line of free spaces to the right.
|
||||
ArrayList<Coord> getLineRight(Coord coord) {
|
||||
ArrayList<Coord> line = new ArrayList<>();
|
||||
for (int x = coord.x + 1; x < Util.boardW; x++) {
|
||||
Coord next = new Coord(x, coord.y);
|
||||
if (!this.isFree(next, Util.Col.WHITE)) break;
|
||||
line.add(next);
|
||||
}
|
||||
return line;
|
||||
}
|
||||
}
|
||||
|
||||
// Information layered over the board.
|
||||
|
@@ -37,19 +37,23 @@ class Pawn extends Piece {
|
||||
ArrayList<Coord> moves(Coord coord, Board board) {
|
||||
ArrayList<Coord> moves = new ArrayList<Coord>();
|
||||
if (coord.rel(this.col).y == 1) {
|
||||
moves.add(new Coord(
|
||||
Coord move = new Coord(
|
||||
coord.x, coord.y + (this.col.equals(Col.BLACK) ? 2 : -2)
|
||||
));
|
||||
);
|
||||
|
||||
if (board.isActuallyFree(move)) moves.add(move);
|
||||
}
|
||||
if (!board
|
||||
.get(new Coord(
|
||||
coord.x + 1,
|
||||
coord.y + (this.col.equals(Col.BLACK) ? 1 : -1)
|
||||
))
|
||||
.col.equals(this.col)) {}
|
||||
moves.add(
|
||||
new Coord(coord.x, coord.y + (this.col.equals(Col.BLACK) ? 1 : -1))
|
||||
);
|
||||
|
||||
Coord move =
|
||||
new Coord(coord.x, coord.y + (this.col.equals(Col.BLACK) ? 1 : -1));
|
||||
if (board.isActuallyFree(move)) moves.add(move);
|
||||
|
||||
Coord attack1 = new Coord(coord.x + 1, move.y);
|
||||
Coord attack2 = new Coord(coord.x - 1, move.y);
|
||||
|
||||
if (board.isOccupiedByEnemy(attack1, this.col)) moves.add(attack1);
|
||||
if (board.isOccupiedByEnemy(attack2, this.col)) moves.add(attack2);
|
||||
|
||||
return moves;
|
||||
}
|
||||
}
|
||||
|
@@ -44,7 +44,7 @@ class Player extends Actor {
|
||||
"clicked on bad space with something selected"
|
||||
);
|
||||
this.board.undisplayMoves(moves);
|
||||
this.board.unselect(coord);
|
||||
this.board.unselect(this.selected);
|
||||
this.selected = null;
|
||||
return;
|
||||
}
|
||||
|
Reference in New Issue
Block a user