public override void write(Object obj1, BOutput bout1, long version) { BOutputBin bout = (BOutputBin)bout1; BBufferBin bbuf = bout.bbuf; char[,,,] arr = (char[, , , ])obj1; // lengths int n3 = arr.GetLength(0); int n2 = arr.GetLength(1); int n1 = arr.GetLength(2); int n0 = arr.GetLength(3); bbuf.putLength(n3); bbuf.putLength(n2); bbuf.putLength(n1); bbuf.putLength(n0); // write for (int i3 = 0; i3 < n3; i3++) { for (int i2 = 0; i2 < n2; i2++) { for (int i1 = 0; i1 < n1; i1++) { for (int i0 = 0; i0 < n0; i0++) { bbuf.putChar(arr[i3, i2, i1, i0]); } } } } }
public static int CountActiveNeighbors(char[,,,] map, int x, int y, int z, int w) { int count = 0; for (int i = x - 1; i < x + 2; i++) { for (int j = y - 1; j < y + 2; j++) { for (int k = z - 1; k < z + 2; k++) { for (int n = w - 1; n < w + 2; n++) { if (i == x && j == y && k == z && n == w) { } else if ( (i >= 0 && j >= 0 && k >= 0 && n >= 0) && (i < map.GetLength(0) && j < map.GetLength(1) && k < map.GetLength(2) && n < map.GetLength(3)) ) { if (map[i, j, k, n] == '#') { count++; } } } } } } return(count); }
static char[,,,] TransformCube(char[,,,] curCube) { int cubeLength = curCube.GetLength(0); char[,,,] newCube = new char[cubeLength, cubeLength, cubeLength, cubeLength]; for (int x = 0; x < cubeLength; x++) { for (int y = 0; y < cubeLength; y++) { for (int z = 0; z < cubeLength; z++) { for (int w = 0; w < cubeLength; w++) { int numActiveNeighbours = CountActiveNeighbours(curCube, x, y, z, w); if (curCube[x, y, z, w] == '#' && (numActiveNeighbours == 2 || numActiveNeighbours == 3)) { newCube[x, y, z, w] = '#'; } else if (curCube[x, y, z, w] == '.' && numActiveNeighbours == 3) { newCube[x, y, z, w] = '#'; } else { newCube[x, y, z, w] = '.'; } } } } } return(newCube); }
public static char[,,,] EnlargeMap(char[,,,] map) { char[,,,] enlarged = new char[map.GetLength(0) + 2, map.GetLength(1) + 2, map.GetLength(2) + 2, map.GetLength(3) + 2]; for (int i = 0; i < enlarged.GetLength(0); i++) { for (int j = 0; j < enlarged.GetLength(1); j++) { for (int k = 0; k < enlarged.GetLength(2); k++) { for (int n = 0; n < enlarged.GetLength(3); n++) { enlarged[i, j, k, n] = '.'; } } } } for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { for (int k = 0; k < map.GetLength(2); k++) { for (int n = 0; n < map.GetLength(3); n++) { enlarged[i + 1, j + 1, k + 1, n + 1] = map[i, j, k, n]; } } } } return(enlarged); }
public static int CountActive(char[,,,] map) { int count = 0; for (int i = 0; i < map.GetLength(0); i++) { for (int j = 0; j < map.GetLength(1); j++) { for (int k = 0; k < map.GetLength(2); k++) { for (int n = 0; n < map.GetLength(3); n++) { if (map[i, j, k, n] == '#') { count++; } } } } } return(count); }
public static char[,,,] UpdateMap(char[,,,] map) { char[,,,] updated = EnlargeMap(map); int activeNeighbors = 0; for (int i = 0; i < updated.GetLength(0); i++) { for (int j = 0; j < updated.GetLength(1); j++) { for (int k = 0; k < updated.GetLength(2); k++) { for (int n = 0; n < updated.GetLength(3); n++) { activeNeighbors = CountActiveNeighbors(map, i - 1, j - 1, k - 1, n - 1); if (IsActive(updated, i, j, k, n)) { if (activeNeighbors != 2 && activeNeighbors != 3) { updated[i, j, k, n] = '.'; } } else { if (activeNeighbors == 3) { updated[i, j, k, n] = '#'; } } } } } } return(updated); }
private static char GetNew2(char[,,,] grid, int w, int x, int y, int z) { int count = 0; char th = '.'; if (!(w < 0 || x < 0 || y < 0 || z < 0 || w >= grid.GetLength(0) || x >= grid.GetLength(1) || y >= grid.GetLength(2) || z >= grid.GetLength(3))) { th = grid[w, x, y, z]; } for (int ow = -1; ow <= 1; ow++) { for (int ox = -1; ox <= 1; ox++) { for (int oy = -1; oy <= 1; oy++) { for (int oz = -1; oz <= 1; oz++) { if (ow + w < 0 || ow + w >= grid.GetLength(0)) { continue; } if (ox + x < 0 || ox + x >= grid.GetLength(1)) { continue; } if (oy + y < 0 || oy + y >= grid.GetLength(2)) { continue; } if (oz + z < 0 || oz + z >= grid.GetLength(3)) { continue; } if (ow == 0 && ox == 0 && oy == 0 && oz == 0) { continue; } if (grid[ow + w, ox + x, oy + y, oz + z] == '#') { count++; } } } } } if (th == '.' && count == 3) { return('#'); } if (th == '#' && (count == 2 || count == 3)) { return('#'); } return('.'); }
private IEnumerable <(int w, int z, int y, int x)> Loop4D(char[,,,] cube) { int size = cube.GetLength(0); for (int w = 0; w < size; w++) { for (int z = 0; z < size; z++) { for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { yield return(w, z, y, x); } } } } }
static int CountActiveNeighbours(char[,,,] curCube, int x, int y, int z, int w) { int cubeLength = curCube.GetLength(0); int numActiveNeighbours = curCube[x, y, z, w] == '#' ? -1 : 0; for (int i = x - 1; i <= x + 1; i++) { if (i == -1 || i == cubeLength) { continue; } for (int j = y - 1; j <= y + 1; j++) { if (j == -1 || j == cubeLength) { continue; } for (int k = z - 1; k <= z + 1; k++) { if (k == -1 || k == cubeLength) { continue; } for (int l = w - 1; l <= w + 1; l++) { if (l == -1 || l == cubeLength) { continue; } if (curCube[i, j, k, l] == '#') { numActiveNeighbours++; } } } } } return(numActiveNeighbours); }
private static int CountNeighborsCube4Dim(char[,,,] input, int indX, int indY, int indZ, int indW) { int rows = input.GetLength(0); int countActif = 0; for (int xIndex = -1; xIndex < 2; xIndex++) { int nX = indX + xIndex; if (nX >= 0 && nX < rows) { for (int yIndex = -1; yIndex < 2; yIndex++) { int nY = indY + yIndex; if (nY >= 0 && nY < rows) { for (int zIndex = -1; zIndex < 2; zIndex++) { int nZ = indZ + zIndex; if (nZ >= 0 && nZ < rows) { for (int wIndex = -1; wIndex < 2; wIndex++) { int nW = indW + wIndex; if (nW >= 0 && nW < rows) { if (input[nX, nY, nZ, nW].Equals('#')) { countActif++; } } } } } } } } } return(countActif); }
private char[,,,] CopyBoard(char[,,,] source, char[,,,] dest) { char[,,,] newBoard = new char[dest.GetLength(0), dest.GetLength(1), dest.GetLength(2), dest.GetLength(3)]; for (int w = 0; w < dest.GetLength(0); w++) { for (int z = 0; z < dest.GetLength(1); z++) { for (int y = 0; y < dest.GetLength(2); y++) { for (int x = 0; x < dest.GetLength(3); x++) { newBoard[w, z, y, x] = dest[w, z, y, x]; } } } } int wOffset = (dest.GetLength(0) - source.GetLength(0)) / 2; int zOffset = (dest.GetLength(1) - source.GetLength(1)) / 2; int yOffset = (dest.GetLength(2) - source.GetLength(2)) / 2; int xOffset = (dest.GetLength(3) - source.GetLength(3)) / 2; for (int w = 0; w < source.GetLength(0); w++) { for (int z = 0; z < source.GetLength(1); z++) { for (int y = 0; y < source.GetLength(2); y++) { for (int x = 0; x < source.GetLength(3); x++) { newBoard[w + wOffset, z + zOffset, y + yOffset, x + xOffset] = source[w, z, y, x]; } } } } return(newBoard); }
static void PrintLayout(char[,,,] curCube) { int cubeLength = curCube.GetLength(0); for (int w = 0; w < cubeLength; w++) { for (int z = 0; z < cubeLength; z++) { Console.Write($"z = {z}, w = {w} {Environment.NewLine}"); for (int x = 0; x < cubeLength; x++) { for (int y = 0; y < cubeLength; y++) { Console.Write(curCube[x, y, z, w]); } Console.Write(Environment.NewLine); } Console.Write(Environment.NewLine); } } }
static int CountActiveCubes(char[,,,] curCube) { int cubeLength = curCube.GetLength(0); int numActiveCubes = 0; for (int x = 0; x < cubeLength; x++) { for (int y = 0; y < cubeLength; y++) { for (int z = 0; z < cubeLength; z++) { for (int w = 0; w < cubeLength; w++) { if (curCube[x, y, z, w] == '#') { numActiveCubes++; } } } } } return(numActiveCubes); }