From 45f754415178494cdafbc6a92089449f89556145 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 18 Feb 2023 20:24:03 -0800 Subject: [PATCH] Add TODO Knight class, make Piece abstract --- src/Board.java | 9 ++++----- src/Knight.java | 14 ++++++++++++++ src/Piece.java | 11 ++--------- 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 src/Knight.java diff --git a/src/Board.java b/src/Board.java index 719a903..c081615 100644 --- a/src/Board.java +++ b/src/Board.java @@ -40,11 +40,10 @@ public class Board { board = new Piece[BOARD_SIZE][BOARD_SIZE]; // Initialize pieces - for (int y = 0; y < 2; y++) - for (int x = 0; x < BOARD_SIZE; x++) { - set(x, y, new Piece(true)); - set(x, y + BOARD_SIZE - 2, new Piece(false)); - } + set(1, 0, new Knight(true)); + set(6, 0, new Knight(true)); + set(1, 7, new Knight(false)); + set(6, 7, new Knight(false)); } public boolean outOfBounds(int x, int y) { diff --git a/src/Knight.java b/src/Knight.java new file mode 100644 index 0000000..511aac3 --- /dev/null +++ b/src/Knight.java @@ -0,0 +1,14 @@ +import java.util.ArrayList; + +public class Knight extends Piece { + public Knight(boolean black) { + super(black); + } + + @Override + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); // to up + return possibleMoves; + } +} diff --git a/src/Piece.java b/src/Piece.java index c991225..d081207 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -1,7 +1,7 @@ import java.awt.*; import java.util.ArrayList; -public class Piece { +public abstract class Piece { // Width and height of placeholder rectangle graphic public static final int DIMENSION = 32; public boolean black; @@ -13,14 +13,7 @@ public class Piece { this.black = black; } - public ArrayList getPossibleMoves(BoardCoordinate position) { - ArrayList 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 abstract ArrayList getPossibleMoves(BoardCoordinate position); public ArrayList getLegalMoves(BoardCoordinate position, Board board) { ArrayList legalMoves = getPossibleMoves(position);