From c66a2664d01a5f4f475909a46b742ea14af52f5d Mon Sep 17 00:00:00 2001 From: Brett Date: Fri, 16 Dec 2022 22:48:05 -0500 Subject: [PATCH] Board Setup, All pieces and their moves are in --- src/chess/Bishop.java | 15 +++++++++++++ src/chess/Board.java | 44 ++++++++++++++++++++++++++++++++++++ src/chess/ChessPiece.java | 47 ++++++++++++++++++++++++++++++++++++++- src/chess/King.java | 18 +++++++++++++++ src/chess/Knight.java | 24 ++++++++++++++++++++ src/chess/Move.java | 32 ++++++++++++++++++++++++++ src/chess/Pawn.java | 32 ++++++++++++++++++++++++++ src/chess/Queen.java | 18 +++++++++++++++ src/chess/Rook.java | 15 +++++++++++++ 9 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 src/chess/Bishop.java create mode 100644 src/chess/King.java create mode 100644 src/chess/Knight.java create mode 100644 src/chess/Move.java create mode 100644 src/chess/Pawn.java create mode 100644 src/chess/Queen.java create mode 100644 src/chess/Rook.java diff --git a/src/chess/Bishop.java b/src/chess/Bishop.java new file mode 100644 index 0000000..838eea1 --- /dev/null +++ b/src/chess/Bishop.java @@ -0,0 +1,15 @@ +package chess; + +import java.util.ArrayList; + +public class Bishop extends ChessPiece { + + public Bishop(Board b, boolean isWhite, int x, int y) { + super(b,isWhite,x,y); + } + + @Override + public ArrayList getMoves() { + return new ArrayList(super.getDiagonalMoves(b.size())); + } +} diff --git a/src/chess/Board.java b/src/chess/Board.java index 69bd1f3..284ed4e 100644 --- a/src/chess/Board.java +++ b/src/chess/Board.java @@ -1,4 +1,48 @@ package chess; public class Board { + + private final ChessPiece[][] board = new ChessPiece[8][8]; + + /** + * create a basic chess board in default configuration + */ + public Board(){ + for (int i = 0; i < size(); i++) { + board[i][1] = new Pawn(this, true, i, 1); + board[i][size() - 2] = new Pawn(this, false, i, size() - 2); + } + // white + board[0][0] = new Rook(this, true,0, 0); + board[size()-1][0] = new Rook(this, true, size() - 1, 0); + board[1][0] = new Knight(this, true, 1, 0); + board[size()-2][0] = new Knight(this, true, size() - 2, 0); + board[2][0] = new Bishop(this, true, 2, 0); + board[size()-3][0] = new Bishop(this, true, size() - 3, 0); + board[3][0] = new Queen(this, true, 3, 0); + board[size()-4][0] = new King(this, true, size() - 4, 0); + + // black + board[0][0] = new Rook(this, false,0, 0); + board[size()-1][0] = new Rook(this, false, size() - 1, 0); + board[1][0] = new Knight(this, false, 1, 0); + board[size()-2][0] = new Knight(this, false, size() - 2, 0); + board[2][0] = new Bishop(this, false, 2, 0); + board[size()-3][0] = new Bishop(this, false, size() - 3, 0); + board[3][0] = new Queen(this, false, 3, 0); + board[size()-4][0] = new King(this, false, size() - 4, 0); + } + + public ChessPiece get(int x, int y){ + if (x < 0 || x > board.length) + return null; + if (y < 0 || y > board.length) + return null; + return board[x][y]; + } + + public int size(){ + return board.length; + } + } diff --git a/src/chess/ChessPiece.java b/src/chess/ChessPiece.java index 6046b0c..ee31582 100644 --- a/src/chess/ChessPiece.java +++ b/src/chess/ChessPiece.java @@ -1,7 +1,52 @@ package chess; +import java.util.ArrayList; + public abstract class ChessPiece { - public abstract void move(); + protected Board b; + protected int x, y; + protected boolean isInDanger, isWhite; + + public ChessPiece(Board b, boolean isWhite, int x, int y) { + this.b = b; + this.x = x; + this.y = y; + this.isWhite = isWhite; + } + + public void setInDanger(boolean isInDanger){ + this.isInDanger = isInDanger; + } + + public boolean isWhite(){ + return isWhite; + } + + public abstract ArrayList getMoves(); + + protected ArrayList getCardinalMoves(int length){ + ArrayList moves = new ArrayList(); + for (int i = 0; i < length; i++){ + // cardinals + moves.add(new Move(x - i, 0)); + moves.add(new Move(x + i, 0)); + moves.add(new Move(0, y - i)); + moves.add(new Move(0, y + i)); + } + return moves; + } + + protected ArrayList getDiagonalMoves(int length){ + ArrayList moves = new ArrayList(); + for (int i = 0; i < length; i++){ + // cardinals + moves.add(new Move(x - i, y - i)); + moves.add(new Move(x + i, y + i)); + moves.add(new Move(x + i, y - i)); + moves.add(new Move(x - i, y + i)); + } + return moves; + } } diff --git a/src/chess/King.java b/src/chess/King.java new file mode 100644 index 0000000..8e99b38 --- /dev/null +++ b/src/chess/King.java @@ -0,0 +1,18 @@ +package chess; + +import java.util.ArrayList; + +public class King extends ChessPiece { + + public King(Board b, boolean isWhite, int x, int y) { + super(b,isWhite,x,y); + } + + @Override + public ArrayList getMoves() { + ArrayList moves = new ArrayList(); + moves.addAll(super.getCardinalMoves(1)); + moves.addAll(super.getDiagonalMoves(1)); + return moves; + } +} diff --git a/src/chess/Knight.java b/src/chess/Knight.java new file mode 100644 index 0000000..4e4e673 --- /dev/null +++ b/src/chess/Knight.java @@ -0,0 +1,24 @@ +package chess; + +import java.util.ArrayList; + +public class Knight extends ChessPiece { + + public Knight(Board b, boolean isWhite, int x, int y) { + super(b,isWhite,x,y); + } + + @Override + public ArrayList getMoves() { + ArrayList moves = new ArrayList(); + moves.add(new Move(x + 2, y + 1)); + moves.add(new Move(x + 2, y - 1)); + moves.add(new Move(x - 2, y + 1)); + moves.add(new Move(x - 2, y - 1)); + moves.add(new Move(x - 1, y - 2)); + moves.add(new Move(x + 1, y - 2)); + moves.add(new Move(x - 1, y + 2)); + moves.add(new Move(x + 1, y + 2)); + return moves; + } +} diff --git a/src/chess/Move.java b/src/chess/Move.java new file mode 100644 index 0000000..1522e3b --- /dev/null +++ b/src/chess/Move.java @@ -0,0 +1,32 @@ +package chess; + +import java.util.ArrayList; + +public class Move { + + private final int x,y; + private final boolean enPassant; + + public Move(int x, int y){ + this.x = x; + this.y = y; + enPassant = false; + } + public Move(int x, int y, boolean enPassant){ + this.x = x; + this.y = y; + this.enPassant = enPassant; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public boolean isEnPassant(){ + return enPassant; + } +} diff --git a/src/chess/Pawn.java b/src/chess/Pawn.java new file mode 100644 index 0000000..317e165 --- /dev/null +++ b/src/chess/Pawn.java @@ -0,0 +1,32 @@ +package chess; + +import java.util.ArrayList; + +public class Pawn extends ChessPiece { + + private final boolean isFirstMove = true; + + public Pawn(Board b, boolean isWhite, int x, int y) { + super(b,isWhite,x,y); + } + + public boolean isFirstMove(){ + return isFirstMove; + } + + @Override + public ArrayList getMoves() { + ArrayList moves = new ArrayList(); + moves.add(new Move(x, y + 1)); + if (isFirstMove) + moves.add(new Move(x, y + 2)); + ChessPiece neighbour = null; + // En passant + if ((neighbour = b.get(x-1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove()) + moves.add(new Move(x-1, 1, true)); + // En passant + if ((neighbour = b.get(x+1, y)) != null && neighbour instanceof Pawn && ((Pawn) neighbour).isFirstMove()) + moves.add(new Move(x+1, 1, true)); + return moves; + } +} diff --git a/src/chess/Queen.java b/src/chess/Queen.java new file mode 100644 index 0000000..9165322 --- /dev/null +++ b/src/chess/Queen.java @@ -0,0 +1,18 @@ +package chess; + +import java.util.ArrayList; + +public class Queen extends ChessPiece { + + public Queen(Board b, boolean isWhite, int x, int y) { + super(b,isWhite,x,y); + } + + @Override + public ArrayList getMoves() { + ArrayList moves = new ArrayList(); + moves.addAll(super.getCardinalMoves(b.size())); + moves.addAll(super.getDiagonalMoves(b.size())); + return moves; + } +} diff --git a/src/chess/Rook.java b/src/chess/Rook.java new file mode 100644 index 0000000..784c2ac --- /dev/null +++ b/src/chess/Rook.java @@ -0,0 +1,15 @@ +package chess; + +import java.util.ArrayList; + +public class Rook extends ChessPiece { + + public Rook(Board b, boolean isWhite, int x, int y) { + super(b,isWhite,x,y); + } + + @Override + public ArrayList getMoves() { + return new ArrayList(super.getCardinalMoves(b.size())); + } +}