-
-
Save anonymous/d9de46638ac02c151bf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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