Skip to content

Commit

Permalink
Refactored generateMovesFrontier to delegate work to generateMoves
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 21, 2016
1 parent 30fd758 commit 0cea3a7
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,62 +81,39 @@ public boolean equals(Board other) {
* @param p
* @return
*/
public ArrayList<Board> generateMoveFrontierForPiece(Piece p) {
ArrayList<Board> frontier = new ArrayList<Board>();
public ArrayList<Move> generateMoves(Piece p) {
ArrayList<Move> avail_moves = new ArrayList<Move>();
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);
}
Move upLeft = new Move(p.getLocation(), new Location(row - 1, col - 1));
if (isValidMove(upLeft)) {
avail_moves.add(new Move(p.getLocation(), new Location(row - 1, col - 1)));
}

// 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);
}
Move upRight = new Move(p.getLocation(), new Location(row - 1, col + 1));
if (isValidMove(upRight)) {
avail_moves.add(new Move(p.getLocation(), new Location(row - 1, col + 1)));
}
}
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);
}
Move downLeft = new Move(p.getLocation(), new Location(row + 1, col - 1));
if (isValidMove(downLeft)) {
avail_moves.add(new Move(p.getLocation(), new Location(row + 1, col - 1)));
}

// 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);
}
Move downRight = new Move(p.getLocation(), new Location(row + 1, col + 1));
if (isValidMove(downRight)) {
avail_moves.add(new Move(p.getLocation(), new Location(row + 1, col + 1)));
}
}
return frontier;
return avail_moves;
}

/**
Expand All @@ -150,14 +127,26 @@ public ArrayList<Board> generateMoveFrontier(Color color) {
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);
ArrayList<Move> jump_moves = generateMoves(this.representation[i][j]);
for (Move move : jump_moves) {
Board board = new Board(this);
board.move(move);
frontier.add(board);
}
}
}
}
return frontier;
}

public void move(Move move) {
representation[move.destination.row][move.destination.column]
= representation[move.source.row][move.source.column];
representation[move.source.row][move.source.column] = null;
representation[move.destination.row][move.destination.column]
.setLocation(new Location(move.destination.row, move.destination.column));
}

/**
* Generates the frontier.
* @param color The color of pieces to generate the frontier for.
Expand Down Expand Up @@ -205,6 +194,10 @@ public boolean isValidJump(Move move) {
&& monkey.getColor() == toMove.opposite();
}

public boolean isValidMove(Move move) {
return isValidSquare(move.destination) && !isOccupied(move.destination);
}

/**
* return true if square contains a piece
* return false otherwise
Expand Down

0 comments on commit 0cea3a7

Please sign in to comment.