diff --git a/Bishop.class b/Bishop.class new file mode 100644 index 0000000..da99627 Binary files /dev/null and b/Bishop.class differ diff --git a/Bishop.java b/Bishop.java index f78ce0c..c102748 100644 --- a/Bishop.java +++ b/Bishop.java @@ -2,71 +2,28 @@ import java.util.*; import javax.swing.ImageIcon; public class Bishop extends Piece { - boolean hasMoved; public Bishop(int x, int y, String color) { super(x, y, color, new ImageIcon("sprites/" + color + "/bishop.png").getImage()); - hasMoved = false; + } + + public Piece copy() { + Piece newP = new Bishop(this.pos.x, this.pos.y, this.color); + return newP; + } + + public ArrayList getPseudoLegalMoves(Board board) { + ArrayList positions = new ArrayList(); + + positions.addAll(slide(board, -1, -1)); + positions.addAll(slide(board, 1, -1)); + positions.addAll(slide(board, -1, 1)); + positions.addAll(slide(board, 1, 1)); + + return positions; } public ArrayList getLegalMoves(Board board) { - ArrayList positions = new ArrayList(); - - // check left - for (int i = pos.x - 1; i >= 0; i--) { - Position test = new Position(i, pos.y); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } - - // check right - for (int i = pos.x + 1; i <= 7; i++) { - Position test = new Position(i, pos.y); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } - - // check up - for (int i = pos.y + 1; i <= 7; i++) { - Position test = new Position(pos.x, i); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } - - // check down - for (int i = pos.y - 1; i >= 0; i--) { - Position test = new Position(pos.x, i); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } - return positions; + return null; } } diff --git a/Board.class b/Board.class index 6328222..ace75e4 100644 Binary files a/Board.class and b/Board.class differ diff --git a/Board.java b/Board.java index 4355039..d8a55ca 100644 --- a/Board.java +++ b/Board.java @@ -13,6 +13,10 @@ public class Board { } } + public Board(boolean isCopy) { + board = new Piece[8][8]; + } + public void draw(Graphics g) { for (Piece[] row : board) { for (Piece p : row) { @@ -32,4 +36,22 @@ public class Board { public Piece getPiece(Position pos) { return this.board[pos.x][pos.y]; } + + public Piece getPiece(int x, int y) { + return this.board[x][y]; + } + + public static Board copy(Board b) { + Board newBoard = new Board(); + + for (int i = 0; i <= 7; i++) { + for (int j = 0; j <= 7; j++) { + Piece p = b.getPiece(i, j); + if (p == null) continue; + newBoard.board[i][j] = p.copy(); + } + } + + return newBoard; + } } diff --git a/King.class b/King.class new file mode 100644 index 0000000..67b67f0 Binary files /dev/null and b/King.class differ diff --git a/King.java b/King.java new file mode 100644 index 0000000..6c23197 --- /dev/null +++ b/King.java @@ -0,0 +1,55 @@ +import java.util.*; +import javax.swing.ImageIcon; + +public class King extends Piece { + boolean hasMoved; + static int[] xDir = {-1, 0, 1, -1, 1, -1, 0, 1}; + static int[] yDir = {-1, -1, -1, 0, 0, 1, 1, 1}; + + public King(int x, int y, String color) { + super(x, y, color, new ImageIcon("sprites/" + color + "/king.png").getImage()); + hasMoved = false; + } + + public Piece copy() { + Piece newP = new King(this.pos.x, this.pos.y, this.color); + return newP; + } + + public ArrayList getPseudoLegalMoves(Board board) { + ArrayList positions = new ArrayList(); + + for (int i = 0; i < xDir.length; i++) { + Position test = new Position(pos.x + xDir[i], pos.y + yDir[i]); + if (!Board.inBounds(test)) continue; + if (board.isOpen(test)) { + positions.add(test); + continue; + } + if (!board.getPiece(test).colorMatches(this)) { + positions.add(test); + } + } + + return positions; + } + + public ArrayList getLegalMoves(Board board) { + return null; + } + + public boolean inCheck(Board board) { + for (Piece[] pieces : board.board) { + for (Piece p : pieces) { + if (p == null) continue; + if (!p.colorMatches(this) && !(p instanceof King)) { + ArrayList ar = p.getLegalMoves(board); + for (Position posi : ar) { + if (this.pos.equals(posi)) return true; + } + } + } + } + return false; + } +} diff --git a/Knight.class b/Knight.class index 07712ee..7831c1c 100644 Binary files a/Knight.class and b/Knight.class differ diff --git a/Knight.java b/Knight.java index a691ee0..d093531 100644 --- a/Knight.java +++ b/Knight.java @@ -9,13 +9,16 @@ public class Knight extends Piece { super(x, y, color, new ImageIcon("sprites/" + color + "/knight.png").getImage()); } - public ArrayList getLegalMoves(Board board) { + public Piece copy() { + Piece newP = new Knight(this.pos.x, this.pos.y, this.color); + return newP; + } + + public ArrayList getPseudoLegalMoves(Board board) { ArrayList positions = new ArrayList(); for (int i = 0; i < xDir.length; i++) { - int xPlus = xDir[i]; - int yPlus = yDir[i]; - Position test = new Position(pos.x + xPlus, pos.y + yPlus); + Position test = new Position(pos.x + xDir[i], pos.y + yDir[i]); if (!Board.inBounds(test)) continue; if (board.isOpen(test)) { positions.add(test); @@ -28,4 +31,8 @@ public class Knight extends Piece { return positions; } + + public ArrayList getLegalMoves(Board board) { + return null; + } } diff --git a/Pawn.class b/Pawn.class index 132432e..5bbcd8b 100644 Binary files a/Pawn.class and b/Pawn.class differ diff --git a/Pawn.java b/Pawn.java index 43ca65a..7d73ff5 100644 --- a/Pawn.java +++ b/Pawn.java @@ -11,7 +11,12 @@ public class Pawn extends Piece { colorDir = color.equals("White") ? -1 : 1; } - public ArrayList getLegalMoves(Board board) { + public Piece copy() { + Piece newP = new Pawn(this.pos.x, this.pos.y, this.color); + return newP; + } + + public ArrayList getPseudoLegalMoves(Board board) { ArrayList positions = new ArrayList(); // diagonal moves (captures) @@ -38,4 +43,8 @@ public class Pawn extends Piece { } return positions; } + + public ArrayList getLegalMoves(Board board) { + return null; + } } diff --git a/Piece.class b/Piece.class index 8f66b71..f7fa974 100644 Binary files a/Piece.class and b/Piece.class differ diff --git a/Piece.java b/Piece.java index 6b99592..d4da0d4 100644 --- a/Piece.java +++ b/Piece.java @@ -18,7 +18,30 @@ public abstract class Piece { public abstract ArrayList getLegalMoves(Board board); + public abstract ArrayList getPseudoLegalMoves(Board board); + + public abstract Piece copy(); + public boolean colorMatches(Piece p) { return this.color.equals(p.color); } + + public ArrayList slide(Board board, int dx, int dy) { + ArrayList positions = new ArrayList<>(); + int step = 1; + while (true) { + Position test = new Position(pos.x + step * dx, pos.y + step * dy); + if (!Board.inBounds(test)) break; + if (board.isOpen(test)) { + positions.add(test); + } else if (board.getPiece(test).colorMatches(this)) { + break; + } else { + positions.add(test); + break; + } + step++; + } + return positions; + } } diff --git a/Queen.class b/Queen.class new file mode 100644 index 0000000..012f6fa Binary files /dev/null and b/Queen.class differ diff --git a/Queen.java b/Queen.java new file mode 100644 index 0000000..fef89eb --- /dev/null +++ b/Queen.java @@ -0,0 +1,33 @@ +import java.util.*; +import javax.swing.ImageIcon; + +public class Queen extends Piece { + + public Queen(int x, int y, String color) { + super(x, y, color, new ImageIcon("sprites/" + color + "/queen.png").getImage()); + } + + public Piece copy() { + Piece newP = new Queen(this.pos.x, this.pos.y, this.color); + return newP; + } + + public ArrayList getPseudoLegalMoves(Board board) { + ArrayList positions = new ArrayList(); + + positions.addAll(slide(board, -1, -1)); + positions.addAll(slide(board, 1, -1)); + positions.addAll(slide(board, -1, 1)); + positions.addAll(slide(board, 1, 1)); + positions.addAll(slide(board, 1, 0)); + positions.addAll(slide(board, -1, 0)); + positions.addAll(slide(board, 0, 1)); + positions.addAll(slide(board, 0, -1)); + + return positions; + } + + public ArrayList getLegalMoves(Board board) { + return null; + } +} diff --git a/Rook.class b/Rook.class index 639094a..dad9fa5 100644 Binary files a/Rook.class and b/Rook.class differ diff --git a/Rook.java b/Rook.java index f73e7f3..5c00914 100644 --- a/Rook.java +++ b/Rook.java @@ -9,64 +9,23 @@ public class Rook extends Piece { hasMoved = false; } - public ArrayList getLegalMoves(Board board) { + public Piece copy() { + Piece newP = new Rook(this.pos.x, this.pos.y, this.color); + return newP; + } + + public ArrayList getPseudoLegalMoves(Board board) { ArrayList positions = new ArrayList(); - // check left - for (int i = pos.x - 1; i >= 0; i--) { - Position test = new Position(i, pos.y); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } + positions.addAll(slide(board, 1, 0)); + positions.addAll(slide(board, -1, 0)); + positions.addAll(slide(board, 0, 1)); + positions.addAll(slide(board, 0, -1)); - // check right - for (int i = pos.x + 1; i <= 7; i++) { - Position test = new Position(i, pos.y); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } - - // check up - for (int i = pos.y + 1; i <= 7; i++) { - Position test = new Position(pos.x, i); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } - - // check down - for (int i = pos.y - 1; i >= 0; i--) { - Position test = new Position(pos.x, i); - if (board.isOpen(test)) { - positions.add(test); - continue; - } else if (board.getPiece(test).colorMatches(this)) { - break; - } else { - positions.add(test); - break; - } - } return positions; } + + public ArrayList getLegalMoves(Board board) { + return null; + } }