diff --git a/src/model/Board.java b/src/model/Board.java index cdb0982..52402ed 100755 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -5,7 +5,8 @@ /** * The representation is a 8x8 grid where * A[row][col] marks the position of the checker piece. - * Smoke starts at lower row and fire starts at higher row. + * Smoke starts at the "bottom" and Fire starts at the "top". + * The top-left-most square is row = 0, column = 0. * @author Ayamin * */ @@ -37,7 +38,7 @@ public Board(Board other) { Piece[][] other_representation = other.getRepresentation(); for (int i = 0; i < other_representation.length; ++i) { for (int j = 0; j < other_representation[0].length; ++j) { - this.representation[i][j] = other_representation[i][j]; + this.representation[i][j] = new Piece(other_representation[i][j]); } } movesSinceCapture = other.getMovesSinceCapture(); @@ -77,10 +78,86 @@ public boolean equals(Board other) { return true; } + /** + * Generates the frontier for a particular piece. + * @param p + * @return + */ + public ArrayList generateMoveFrontierForPiece(Piece p) { + ArrayList frontier = new ArrayList(); + int row = p.getLocation().row; + int col = p.getLocation().column; + boolean up, down; + up = p.getColor() == Color.BLACK || p.getType() == Type.KING; + down = p.getColor() == Color.RED || p.getType() == Type.KING; + if (up) { + // up left: + if (isValidSquare(row - 1, col - 1)) { + if (null == this.representation[row - 1][col - 1]) { + Board upleft = new Board(this); + Piece[][] rep = upleft.getRepresentation(); + rep[row - 1][col - 1] = rep[row][col]; + rep[row][col] = null; + rep[row - 1][col - 1].setLocation(new Location(row - 1, col - 1)); + frontier.add(upleft); + } + } + // up right + if (isValidSquare(row - 1, col + 1)) { + if (null == this.representation[row - 1][col + 1]) { + Board upright = new Board(this); + Piece[][] rep = upright.getRepresentation(); + rep[row - 1][col + 1] = rep[row][col]; + rep[row][col] = null; + rep[row - 1][col + 1].setLocation(new Location(row - 1, col + 1)); + frontier.add(upright); + } + } + } + if (down) { + // down left + if (isValidSquare(row + 1, col - 1)) { + if (null == this.representation[row + 1][col - 1]) { + Board downleft = new Board(this); + Piece[][] rep = downleft.getRepresentation(); + rep[row + 1][col - 1] = rep[row][col]; + rep[row][col] = null; + rep[row + 1][col - 1].setLocation(new Location(row + 1, col - 1)); + frontier.add(downleft); + } + } + // down right + if (isValidSquare(row + 1, col + 1)) { + if (null == this.representation[row + 1][col + 1]) { + Board downright = new Board(this); + Piece[][] rep = downright.getRepresentation(); + rep[row + 1][col + 1] = rep[row][col]; + rep[row][col] = null; + rep[row + 1][col + 1].setLocation(new Location(row + 1, col + 1)); + frontier.add(downright); + } + } + } + return frontier; + } + + /** + * Generates the frontier for movement for all pieces. + * @param color + * @return + */ public ArrayList generateMoveFrontier(Color color) { ArrayList frontier = new ArrayList(); - - + for (int i = 0; i < BOARD_SIZE; ++i) { + for (int j = 0; j < BOARD_SIZE; ++j) { + Piece p = this.representation[i][j]; + if(null != p && p.getColor() == color) { + ArrayList sub_frontier = generateMoveFrontierForPiece(this.representation[i][j]); + frontier.addAll(sub_frontier); + } + } + } + return frontier; } /** diff --git a/src/model/Location.java b/src/model/Location.java index 4bb4498..70db62a 100644 --- a/src/model/Location.java +++ b/src/model/Location.java @@ -14,6 +14,11 @@ public Location(int row, int column) { this.column = column; } + public Location(Location other) { + this.row = other.row; + this.column = other.column; + } + public boolean equals(Location other) { return this.row == other.row && this.column == other.column; } diff --git a/src/model/Piece.java b/src/model/Piece.java index 909c2e8..8a1b776 100755 --- a/src/model/Piece.java +++ b/src/model/Piece.java @@ -11,6 +11,12 @@ public Piece(Color color, int row, int col) { this.type = Type.NORMAL; } + public Piece(Piece other) { + this.color = other.getColor(); + this.location = new Location(other.getLocation()); + this.type = other.getType(); + } + public void setLocation(Location location) { this.location = location; }