Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
All work done so far on Model is deprecated :(
  • Loading branch information
Aaron committed Apr 20, 2016
1 parent 5913738 commit 6f43281
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
/bin/
118 changes: 0 additions & 118 deletions src/model/Board.java
Expand Up @@ -28,124 +28,6 @@ public class Board {
movesSinceCapture = 0;
init();
}

public boolean isValidSquare(int i, int j) {
return 0 <= i && i < BOARD_SIZE &&
0 <= j && j < BOARD_SIZE;
}

/**
* Checks if a piece can attack another in a given direction.
* Up is negative y direction, down is positive y direction.
* Left is negative x direction, right is positive x direction.
* @param p The piece.
* @param X The direction along the x-coordinate (LEFT or RIGHT).
* @param Y The direction along the y-coordinate (UP or DOWN).
* @return
*/
public boolean checkAttackDirection(Piece p, Piece[][] image, Direction X, Direction Y) {
Piece candidate = null;
int i = p.getX(), j = p.getY(); // y is row, x is column
int enemy_i = X.equals(Direction.LEFT) ? i-1 : i+1;
int enemy_j = Y.equals(Direction.UP) ? j-1 : j+1;
int end_position_i = X.equals(Direction.LEFT) ? i-2 : i+2;
int end_position_j = Y.equals(Direction.UP) ? j-2: j+2;
if (isValidSquare(enemy_j, enemy_i)) {
candidate = image[enemy_j][enemy_i];
if (null != candidate && // there exists a piece we can take)
candidate.color.equals(p.opposite()) && // it is an enemy piece
isValidSquare(end_position_j, end_position_i) && // there is a free space
null == image[end_position_j][end_position_i]) // that we can end up in
return true;
}
return false;
}

/**
* Used for validation of moves.
* If returns true after a player chooses a move,
* that move is invalid because the player can keep on attacking.
* We go by the convention that black starts out at the "bottom", and
* red starts out at the "top". Smoke moves before fire.
*
* Up is negative y direction, down is positive y direction.
* Left is negative x direction, right is positive x direction.
*
* @param p
* @return
*/
public boolean hasAttackVector(Piece p, Piece[][] image) {
boolean can_attack = false;
if (p.color.equals(Color.BLACK) || p.getType().equals(Type.KING)) {
can_attack |= checkAttackDirection(p, image, Direction.UP, Direction.LEFT);
can_attack |= checkAttackDirection(p, image, Direction.UP, Direction.RIGHT);
}
if (p.color.equals(Color.RED) || p.getType().equals(Type.KING)) {
can_attack |= checkAttackDirection(p, image, Direction.DOWN, Direction.LEFT);
can_attack |= checkAttackDirection(p, image, Direction.DOWN, Direction.RIGHT);
}
return can_attack;
}

/**
* Used for validation of moves.
* We go by the convention that black starts out at the "bottom", and
* red starts out at the "top". Smoke moves before fire.
* Up is negative y direction, down is positive y direction.
* Left is negative x direction, right is positive x direction.
* A piece can move if the spot is available AND the move matches the color.
* (Black can only go up and red can only go down, unless they are kinged.)
* @param p
* @return true if the piece can move to the specified coordinates.
*/
public boolean canMove(Piece p, int x, int y) {
if(!this.isValidSquare(y, x)) return false;
boolean spot_available = (null == this.representation[y][x]);
boolean is_moving_up = (y == p.getY() - 1);
boolean is_moving_down = (y == p.getY() + 1);
boolean is_moving_left = (x == p.getX() - 1);
boolean is_moving_right = (x == p.getX() + 1);

// Spot must be available and must be moving exactly one to the left or right
if (spot_available && (is_moving_left || is_moving_right)) {
if (p.color.equals(Color.BLACK) || p.getType().equals(Type.KING)) {
if (is_moving_up) return true;
}
if (p.color.equals(Color.RED) || p.getType().equals(Type.KING)) {
if (is_moving_down) return true;
}
}
return false;
}

/**
* Tests whether we can move a piece at (src_x, src_y)
* to a spot on the board at (dest_x, dest_y).
* Delegates to canMove(Piece, int, int).
* If a piece does not exist at the source, returns false.
* @param src_x
* @param src_y
* @param dest_x
* @param dest_y
* @return
*/
public boolean canMove(int src_x, int src_y, int dest_x, int dest_y) {
if (!(this.isValidSquare(src_y, src_x) && this.isValidSquare(dest_y, dest_x))) return false;
Piece p = this.representation[src_y][src_x];
if (null == p) return false;
return this.canMove(p, dest_x, dest_y);
}

public boolean tryToMove(int src_x, int src_y, int dest_x, int dest_y) {
if (canMove(src_x, src_y, dest_x, dest_y)) {
Piece piece = this.representation[src_y][src_x];
this.representation[src_y][src_x] = null;
this.representation[dest_y][dest_x] = piece;
return true;
}
else if (canJump)

}

/**
* Initialize the board putting checker pieces in their starting locations
Expand Down

0 comments on commit 6f43281

Please sign in to comment.