Skip to content

Commit

Permalink
Board::generateMoveFrontier() finished. Piece copyconsturctor added. …
Browse files Browse the repository at this point in the history
…Location copy constructor added.
  • Loading branch information
Aaron committed Apr 21, 2016
1 parent 8150181 commit 5662aca
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
85 changes: 81 additions & 4 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
*/
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -77,10 +78,86 @@ public boolean equals(Board other) {
return true;
}

/**
* Generates the frontier for a particular piece.
* @param p
* @return
*/
public ArrayList<Board> generateMoveFrontierForPiece(Piece p) {
ArrayList<Board> frontier = new ArrayList<Board>();
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<Board> generateMoveFrontier(Color color) {
ArrayList<Board> frontier = new ArrayList<Board>();


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<Board> sub_frontier = generateMoveFrontierForPiece(this.representation[i][j]);
frontier.addAll(sub_frontier);
}
}
}
return frontier;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/model/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/model/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5662aca

Please sign in to comment.