Skip to content

Commit

Permalink
added deep-equality functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Apr 20, 2016
1 parent 7c62a12 commit 753b2b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean isValidSquare(int row, int col) {
* Initialize the board putting checker pieces in their starting locations
*/
private void init() {
for(int row = 0; row < 3; row++){
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 @@ -47,11 +47,26 @@ 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 row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (!isOccupied(row, col))
System.out.print("| ");
Expand All @@ -62,7 +77,7 @@ else if (representation[row][col].getColor() == Color.RED) {
System.out.print("|R");
}
else {
if(representation[row][col].getType() == Type.NORMAL)
if (representation[row][col].getType() == Type.NORMAL)
System.out.print("|b");
else
System.out.print("|B");
Expand Down Expand Up @@ -94,6 +109,10 @@ public ArrayList<Piece> getBlackPieces() {

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;
}
}
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();
}
}

0 comments on commit 753b2b0

Please sign in to comment.