From 17e81feac071d43625f7e46b4731a9037d2920bd Mon Sep 17 00:00:00 2001 From: JoffreTS Date: Tue, 21 Feb 2023 18:13:47 -0800 Subject: [PATCH] added classes for each piece, with possible moves. Also updated board initialization to include all pieces. --- src/Bishop.java | 18 ++++++++++++++++++ src/Board.java | 26 ++++++++++++++++++++++---- src/King.java | 20 ++++++++++++++++++++ src/Pawn.java | 17 +++++++++++++++++ src/Queen.java | 22 ++++++++++++++++++++++ src/Rook.java | 18 ++++++++++++++++++ 6 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/Bishop.java create mode 100644 src/King.java create mode 100644 src/Pawn.java create mode 100644 src/Queen.java create mode 100644 src/Rook.java diff --git a/src/Bishop.java b/src/Bishop.java new file mode 100644 index 0000000..75e96e7 --- /dev/null +++ b/src/Bishop.java @@ -0,0 +1,18 @@ +import java.util.ArrayList; + +public class Bishop extends Piece { + public Bishop(boolean black) { + super(black); + } + + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + for(int i = 1; i <= 7; i++) { + possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i)); + } + return possibleMoves; + } +} diff --git a/src/Board.java b/src/Board.java index c081615..30f05c2 100644 --- a/src/Board.java +++ b/src/Board.java @@ -40,10 +40,28 @@ public class Board { board = new Piece[BOARD_SIZE][BOARD_SIZE]; // Initialize pieces - set(1, 0, new Knight(true)); - set(6, 0, new Knight(true)); - set(1, 7, new Knight(false)); - set(6, 7, new Knight(false)); + for(int i = 0; i <= 7; i++){ + if(i == 0 || i == 7){ + for(int j = 0; j <= 7; j++){ + if(j == 0 || j == 7){ + set(j, i, new Rook(i==0)); + } else if(j == 1 || j == 6){ + set(j, i, new Knight(i==0)); + } else if(j == 2 || j == 5){ + set(j, i, new Bishop(i==0)); + } else if(j == 4){ + set(j, i, new Queen(i==0)); + } else { + set(j, i, new King(i==0)); + } + } + } + if(i == 1 || i == 6){ + for(int j = 0; j <= 7; j++){ + set(j, i, new Pawn(i==1)); + } + } + } } public boolean outOfBounds(int x, int y) { diff --git a/src/King.java b/src/King.java new file mode 100644 index 0000000..44419f8 --- /dev/null +++ b/src/King.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; + +public class King extends Piece { + public King(boolean black) { + super(black); + } + + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1)); + possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1)); + possibleMoves.add(new BoardCoordinate(position.x - 1, position.y)); + possibleMoves.add(new BoardCoordinate(position.x + 1, position.y - 1)); + possibleMoves.add(new BoardCoordinate(position.x + 1, position.y + 1)); + possibleMoves.add(new BoardCoordinate(position.x + 1, position.y)); + possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); + possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); + return possibleMoves; + } +} \ No newline at end of file diff --git a/src/Pawn.java b/src/Pawn.java new file mode 100644 index 0000000..2563766 --- /dev/null +++ b/src/Pawn.java @@ -0,0 +1,17 @@ +import java.util.ArrayList; + +public class Pawn extends Piece { + public Pawn(boolean black) { + super(black); + } + + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + if (this.black) { + possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); + } else { + possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); + } + return possibleMoves; + } +} diff --git a/src/Queen.java b/src/Queen.java new file mode 100644 index 0000000..44f36db --- /dev/null +++ b/src/Queen.java @@ -0,0 +1,22 @@ +import java.util.ArrayList; + +public class Queen extends Piece { + public Queen(boolean black) { + super(black); + } + + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + for(int i = 1; i <= 7; i++) { + possibleMoves.add(new BoardCoordinate(position.x - i, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x - i, position.y + i)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y + i)); + possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x, position.y + i)); + } + return possibleMoves; + } +} diff --git a/src/Rook.java b/src/Rook.java new file mode 100644 index 0000000..d94ecf8 --- /dev/null +++ b/src/Rook.java @@ -0,0 +1,18 @@ +import java.util.ArrayList; + +public class Rook extends Piece { + public Rook(boolean black) { + super(black); + } + + public ArrayList getPossibleMoves(BoardCoordinate position) { + ArrayList possibleMoves = new ArrayList<>(); + for(int i = 1; i <= 7; i++) { + possibleMoves.add(new BoardCoordinate(position.x - i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x + i, position.y)); + possibleMoves.add(new BoardCoordinate(position.x, position.y - i)); + possibleMoves.add(new BoardCoordinate(position.x, position.y + i)); + } + return possibleMoves; + } +} \ No newline at end of file