diff --git a/Bishop.java b/Bishop.java new file mode 100644 index 0000000..f78ce0c --- /dev/null +++ b/Bishop.java @@ -0,0 +1,72 @@ +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 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; + } +} diff --git a/Board.class b/Board.class index 14395b1..6328222 100644 Binary files a/Board.class and b/Board.class differ diff --git a/Board.java b/Board.java index b95f324..4355039 100644 --- a/Board.java +++ b/Board.java @@ -6,10 +6,10 @@ public class Board { public Board() { board = new Piece[8][8]; for (int i = 0; i <= 7; i++) { - board[i][2] = new Pawn(i + 1, 2, "Black"); + board[i][1] = new Pawn(i, 1, "Black"); } for (int i = 0; i <= 7; i++) { - board[i][7] = new Pawn(i + 1, 7, "White"); + board[i][6] = new Pawn(i, 6, "White"); } } diff --git a/Knight.class b/Knight.class index 5ee2504..07712ee 100644 Binary files a/Knight.class and b/Knight.class differ diff --git a/Pawn.class b/Pawn.class index ea3353c..132432e 100644 Binary files a/Pawn.class and b/Pawn.class differ diff --git a/Piece.class b/Piece.class index 472ecfc..8f66b71 100644 Binary files a/Piece.class and b/Piece.class differ diff --git a/Piece.java b/Piece.java index 4465faf..6b99592 100644 --- a/Piece.java +++ b/Piece.java @@ -1,8 +1,7 @@ import java.awt.*; import java.util.*; -public class Piece { - ArrayList legalMoves; +public abstract class Piece { Position pos; Image sprite; String color; @@ -17,10 +16,7 @@ public class Piece { g.drawImage(sprite, pos.x * 40, pos.y * 40, null); } - public ArrayList getLegalMoves(Board board) { - System.out.println("parent method not overriden"); - return null; - } + public abstract ArrayList getLegalMoves(Board board); public boolean colorMatches(Piece p) { return this.color.equals(p.color); diff --git a/Rook.class b/Rook.class new file mode 100644 index 0000000..639094a Binary files /dev/null and b/Rook.class differ diff --git a/Rook.java b/Rook.java new file mode 100644 index 0000000..f73e7f3 --- /dev/null +++ b/Rook.java @@ -0,0 +1,72 @@ +import java.util.*; +import javax.swing.ImageIcon; + +public class Rook extends Piece { + boolean hasMoved; + + public Rook(int x, int y, String color) { + super(x, y, color, new ImageIcon("sprites/" + color + "/rook.png").getImage()); + hasMoved = false; + } + + 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; + } +}