diff --git a/King.java b/King.java index 8309a2e..d3b630c 100644 --- a/King.java +++ b/King.java @@ -1,66 +1,66 @@ -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) { - ArrayList positions = getPseudoLegalMoves(board); - for (Position p : new ArrayList<>(positions)) { - Piece tempPiece = board.getPiece(p); - board.setPiece(pos, tempPiece); - board.setPiece(p, this); - if (inCheck(board, p)) { - positions.remove(p); - } - board.setPiece(p, tempPiece); - board.setPiece(pos, this); - } - return positions; - } - - public boolean inCheck(Board board, Position pos) { - 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 (pos.equals(posi)) return true; - } - } - } - } - return false; - } -} +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) { + ArrayList positions = getPseudoLegalMoves(board); + for (Position p : new ArrayList<>(positions)) { + Piece tempPiece = board.getPiece(p); + board.setPiece(pos, null); + board.setPiece(p, this); + if (inCheck(board, p)) { + positions.remove(p); + } + board.setPiece(p, tempPiece); + board.setPiece(pos, this); + } + return positions; + } + + public boolean inCheck(Board board, Position pos) { + for (Piece[] pieces : board.board) { + for (Piece p : pieces) { + if (p == null) continue; + if (!p.colorMatches(this) && !(p instanceof King)) { + ArrayList ar = p.getPseudoLegalMoves(board); + for (Position posi : ar) { + if (pos.equals(posi)) return true; + } + } + } + } + return false; + } +}