Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Mar 31, 2016
2 parents ff660f3 + 8e29251 commit ad79f6e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
71 changes: 49 additions & 22 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

import java.util.ArrayList;

/**
* The representation is a 8x8 grid where
* A[y][x] denotes the (x, y) indexed piece.
* It is swapped because we dereference by "row" first,
* which is "y".
* @author Ayamin
*
*/
public class Board {
// Board properties and representation
private final int BOARD_SIZE = 8;
Expand All @@ -22,30 +30,32 @@ public Board() {
}

public boolean isValidSquare(int i, int j) {
return i < BOARD_SIZE && j < BOARD_SIZE;
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, Direction X, Direction Y) {
public boolean checkAttackDirection(Piece p, Piece[][] image, Direction X, Direction Y) {
Piece candidate = null;
int i = p.getX(), j = p.getY();
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 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_i, enemy_j)) {
candidate = representation[enemy_i][enemy_j];
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_i, end_position_j) && // there is a free space
null == representation[end_position_i][end_position_j]) // that we can end up in
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;
Expand All @@ -58,18 +68,21 @@ public boolean checkAttackDirection(Piece p, Direction X, Direction Y) {
* 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) {
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, Direction.UP, Direction.LEFT);
can_attack |= checkAttackDirection(p, Direction.UP, Direction.RIGHT);
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, Direction.DOWN, Direction.LEFT);
can_attack |= checkAttackDirection(p, Direction.DOWN, Direction.RIGHT);
can_attack |= checkAttackDirection(p, image, Direction.DOWN, Direction.LEFT);
can_attack |= checkAttackDirection(p, image, Direction.DOWN, Direction.RIGHT);
}
return can_attack;
}
Expand All @@ -78,15 +91,18 @@ public boolean hasAttackVector(Piece p) {
* 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) {
boolean spot_available = this.isValidSquare(x, y) && (null != this.representation[x][y]);
boolean is_moving_up = (y == p.getY() + 1);
boolean is_moving_down = (y == p.getY() - 1);
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);

Expand Down Expand Up @@ -114,10 +130,21 @@ public boolean canMove(Piece p, int x, int y) {
* @return
*/
public boolean canMove(int src_x, int src_y, int dest_x, int dest_y) {
if (!(this.isValidSquare(src_x, src_y) && this.isValidSquare(dest_x, dest_y))) return false;
Piece p = this.representation[src_x][src_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)

}

/**
Expand All @@ -128,8 +155,8 @@ private void init()
for(int row = 0; row < 3; row++){
for (int col = 0; col < 4; col++)
{
Piece red_piece = new Piece(Color.RED, row, 2*col + (row % 2));
Piece black_piece = new Piece(Color.BLACK, BOARD_SIZE - 1 - row, 2*col + (BOARD_SIZE - 1 - row) %2);
Piece red_piece = new Piece(Color.RED, 2*col + (row % 2), row);
Piece black_piece = new Piece(Color.BLACK, 2*col + (BOARD_SIZE - 1 - row) %2, BOARD_SIZE - 1 - row);
representation[row][2*col+ (row % 2)] = red_piece;
representation[BOARD_SIZE - 1 - row][2*col + (BOARD_SIZE - 1 - row) %2] = black_piece;
}
Expand Down
33 changes: 31 additions & 2 deletions src/test/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,39 @@
import model.Board;

public class BoardTest {

public static void assertCanMove(Board b, int i, int j, int dest_i, int dest_j) {
assert(b.canMove(i, j, dest_i,dest_j)) :
"Can move from (" + i + "," + j +") to (" + dest_i + "," + dest_j + ").";
}

public static void assertCannotMove(Board b, int i, int j, int dest_i, int dest_j) {
assert(!b.canMove(i, j, dest_i,dest_j)) :
"Cannot move from (" + i + "," + j +") to (" + dest_i + "," + dest_j + ").";
}

public static void movementTest() {
Board b = new Board();
assertCannotMove(b, 0, 6, 1, 5); // move black onto another
assertCannotMove(b, 6, 2, 7, 1); // move red up
assertCannotMove(b, 0, 2, -1, 3); // move red out of bounds
assertCannotMove(b, 7, 5, 8, 4); // move black out of bounds
assertCanMove(b, 3, 5, 4, 4); // move black up right
assertCanMove(b, 3, 5, 2, 4); // move black up left
assertCanMove(b, 1, 5, 2, 4); // move black up right
assertCanMove(b, 0, 2, 1, 3); // move red down right
assertCanMove(b, 4, 2, 3, 3); // move red down left
assertCanMove(b, 4, 2, 5, 3); // move red down right
}

public static void printTest() {
Board b = new Board();
b.print();
}

public static void main(String[]args) {
Board checkers = new Board();
checkers.print();
printTest();
movementTest();
}
}

0 comments on commit ad79f6e

Please sign in to comment.