Skip to content

Instantly share code, notes, and snippets.

Created December 7, 2013 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d9de46638ac02c151bf2 to your computer and use it in GitHub Desktop.
Save anonymous/d9de46638ac02c151bf2 to your computer and use it in GitHub Desktop.
class Board {
final int boardSize = 2;
Square[][] pos;
/* create empty board */
public Board() {
pos = new Square[boardSize][boardSize];
for (int row = 0; row < boardSize; row++)
for (int col = 0; col < boardSize; col++) {
pos[row][col] = new Square(row, col);
}
}
/** A copy constructor */
public Board(Board other) {
pos = new Square[boardSize][boardSize];
for (int row = 0; row < boardSize; row++)
for (int col = 0; col < boardSize; col++) {
// we need to provide a reference to the new outter class - ourself: this
this.pos[row][col] = other.pos[row][col].createAClone(this);
}
}
class Square {
int row;
int col;
public Square(int r, int c) {
row = r;
col = c;
}
/** A clone method of this inner class with an argument of enclosing class */
public Square createAClone(Board referenceToEnclosingClass) {
return referenceToEnclosingClass.new Square(row, col);
}
public int gameHash() {
return Board.this.hashCode();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment