Skip to content

Commit

Permalink
Added isValidJump
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 20, 2016
2 parents da362d9 + 753b2b0 commit 791a755
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 133 deletions.
19 changes: 3 additions & 16 deletions src/controller/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,14 @@

import model.Board;
import model.Color;
import model.Player;

public class Game {
private Board board;
private Player player_one, player_two;
private Color current_turn;

public Game(Player p1, Player p2) {
this.player_one = p1;
this.player_two = p2;
public Game(Color start) {
this.board = new Board();
assignPieces();
}

private void assignPieces() {
if (player_one.getAssignedColor() == Color.RED) {
player_one.setPieces(board.getRedPieces());
player_two.setPieces(board.getBlackPieces());
} else {
player_two.setPieces(board.getRedPieces());
player_one.setPieces(board.getBlackPieces());
}
current_turn = start;
}

}
91 changes: 47 additions & 44 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

/**
* 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".
* A[row][col] marks the position of the checker piece.
* Smoke starts at lower row and fire starts at higher row.
* @author Ayamin
*
*/
Expand All @@ -15,10 +14,6 @@ public class Board {
private final int BOARD_SIZE = 8;
private Piece[][] representation;

// Pieces available to the Players
private ArrayList<Piece> red_pieces;
private ArrayList<Piece> black_pieces;

// Move properties
private int movesSinceCapture;
public enum Direction {UP, DOWN, LEFT, RIGHT};
Expand All @@ -28,15 +23,18 @@ public Board() {
representation = new Piece[BOARD_SIZE][BOARD_SIZE];
movesSinceCapture = 0;
init();
}

public boolean isValidSquare(Location location) {
return 0 <= location.row && location.row < BOARD_SIZE &&
0 <= location.column && location.column < BOARD_SIZE;
}



/**
* Initialize the board putting checker pieces in their starting locations
*/
private void init()
{
for(int row = 0; row < 3; row++) {
private void init() {
for (int row = 0; row < 3; row++){
for (int col = 0; col < 4; col++) {
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);
Expand All @@ -46,61 +44,66 @@ private void init()
}
}

/**
* Tests board equality.
* @param other The other board.
* @return true if the board's pieces are all equal to the other board's pieces.
*/
public boolean equals(Board other) {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if(!(this.representation[i][j]).equals(other.getRepresentation()[i][j]))
return false;
}
}
return true;
}

/**
* Print the current board representation
*/
public void print()
{
for(int row = 0; row < BOARD_SIZE; row++)
{
for (int col = 0; col < BOARD_SIZE; col++)
{
if (!isOccupied(row, col))
public void print() {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (!isOccupied(new Location(row, col)))
System.out.print("| ");
else if (representation[row][col].getColor() == Color.RED)
{
else if (representation[row][col].getColor() == Color.RED) {
if (representation[row][col].getType() == Type.NORMAL)
System.out.print("|r");
else
System.out.print("|R");
}
else
{
if(representation[row][col].getType() == Type.NORMAL)
else {
if (representation[row][col].getType() == Type.NORMAL)
System.out.print("|b");
else
System.out.print("|B");
}

}
System.out.println("|");
}
}


public boolean isValidJump(Move move) {
Piece monkey = representation[(move.destination.row + move.source.row)/2][(move.destination.column + move.source.column)/2];
Piece toMove = representation[move.source.row][move.source.column];
return isValidSquare(move.destination) && !isOccupied(move.destination)
&& monkey != null
&& monkey.getColor() == toMove.opposite();
}

/**
* return true if square contains a piece
* return false otherwise
*/
public boolean isOccupied(int row, int col)
{
return representation[row][col] != null;
}

public ArrayList<Piece> getRedPieces() {
return red_pieces;
}

public void setRedPieces(ArrayList<Piece> red_pieces) {
this.red_pieces = red_pieces;
}

public ArrayList<Piece> getBlackPieces() {
return black_pieces;
public boolean isOccupied(Location location) {
return representation[location.row][location.column] != null;
}

public void setBlackPieces(ArrayList<Piece> black_pieces) {
this.black_pieces = black_pieces;
}


public Piece[][] getRepresentation() {
return this.representation;
}
}

4 changes: 4 additions & 0 deletions src/model/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public Location(int row, int column) {
this.row = row;
this.column = column;
}

public boolean equals(Location other) {
return this.row == other.row && this.column == other.column;
}
}
22 changes: 3 additions & 19 deletions src/model/Move.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
package model;

public class Move {
private Location source;
private Location destination;
public final Location source;
public final Location destination;

public Move(Location source, Location destination) {
this.setSource(source);
this.setDestination(destination);
}

public Location getSource() {
return source;
}

public void setSource(Location source) {
this.source = source;
this.destination = destination;
}

public Location getDestination() {
return destination;
}

public void setDestination(Location destination) {
this.destination = destination;
}


}
6 changes: 6 additions & 0 deletions src/model/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public Type getType() {
public Color getColor() {
return this.color;
}

public boolean equals(Piece other) {
return this.color == other.color &&
this.location.equals(other.getLocation()) &&
this.type == other.getType();
}
}
35 changes: 0 additions & 35 deletions src/model/Player.java

This file was deleted.

20 changes: 1 addition & 19 deletions src/test/BoardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@

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() {
Expand Down

0 comments on commit 791a755

Please sign in to comment.