プリミティブ配列を使用していると仮定すると、固定数のセルで行列を拡張すると、次のようになります。
boolean[][] gameBoard = new boolean[3][3];
public boolean[][] expandMatrixBy(boolean[][] matrix, int number) {
int oldSize = matrix.length;
int newSize = oldSize + 2 * number;
boolean[][] result = new boolean[newSize][newSize];
// Assume new cells should be dead, i.e. false..
for (int row = number; row < oldSize + number; row++) {
for (int col = number; col < oldSize + number; col++) {
// ..copy only the existing cells into new locations.
result[row][col] = matrix[row - number][col - number];
}
}
return result;
}
// Calling this on a 3x3 matrix will produce 5x5 matrix, expanded by 1 on each side.
gameBoard = expandMatrixBy(gameBoard, 1);
そしてジョン・コンウェイのライフゲームです:-)
オプション:
このソリューションは、次のようにカスタマイズして、選択した側で拡張を有効にすることができます。
enum Side { Left, Right, Top, Bottom };
public boolean[][] expandMatrixBy(boolean[][] matrix, int number, Set<Side> sides) {
int oldSize = matrix.length;
int newSize = oldSize + number * sides.size();
boolean[][] result = new boolean[newSize][newSize];
for (Side side : sides) {
switch(side) {
case Left:
// Add number of columns on the left.
case Right:
// Add "number" of columns on the right.
}
}
return result;
}
Set<Side> expandOnTheseSides = EnumSet.of(Side.Left, Side.Top);
gameBoard = expandMatrixBy(gameBoard, 1, expandOnTheseSides);
幸運を。