Add TODO Knight class, make Piece abstract

main
Elnu 2 years ago
parent 1f578b0770
commit 45f7544151

@ -40,11 +40,10 @@ public class Board {
board = new Piece[BOARD_SIZE][BOARD_SIZE]; board = new Piece[BOARD_SIZE][BOARD_SIZE];
// Initialize pieces // Initialize pieces
for (int y = 0; y < 2; y++) set(1, 0, new Knight(true));
for (int x = 0; x < BOARD_SIZE; x++) { set(6, 0, new Knight(true));
set(x, y, new Piece(true)); set(1, 7, new Knight(false));
set(x, y + BOARD_SIZE - 2, new Piece(false)); set(6, 7, new Knight(false));
}
} }
public boolean outOfBounds(int x, int y) { public boolean outOfBounds(int x, int y) {

@ -0,0 +1,14 @@
import java.util.ArrayList;
public class Knight extends Piece {
public Knight(boolean black) {
super(black);
}
@Override
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); // to up
return possibleMoves;
}
}

@ -1,7 +1,7 @@
import java.awt.*; import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
public class Piece { public abstract class Piece {
// Width and height of placeholder rectangle graphic // Width and height of placeholder rectangle graphic
public static final int DIMENSION = 32; public static final int DIMENSION = 32;
public boolean black; public boolean black;
@ -13,14 +13,7 @@ public class Piece {
this.black = black; this.black = black;
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public abstract ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position);
ArrayList<BoardCoordinate> possibleMoves = new ArrayList<>();
possibleMoves.add(new BoardCoordinate(position.x - 1, position.y)); // to left
possibleMoves.add(new BoardCoordinate(position.x + 1, position.y)); // to right
possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); // to up
possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); // to down
return possibleMoves;
}
public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board) { public ArrayList<BoardCoordinate> getLegalMoves(BoardCoordinate position, Board board) {
ArrayList<BoardCoordinate> legalMoves = getPossibleMoves(position); ArrayList<BoardCoordinate> legalMoves = getPossibleMoves(position);