c# - Reverse a multidimensional array 4 times -
for simple game, need scoreboard generated dynamically on startup. scoreboard stored multidimensional array in c#. generating base scores isn't hard, problem array should reversed 4 times shown below.
the multidimensional array:
int[,] scoreboard = new int[4, 4];
the base figures:
{ 5, 10, 15, 20 10, 15, 20, 25 15, 20, 25, 30 }
should become:
{ 5, 10, 15, 20, 20, 15, 10, 5 10, 15, 20, 25, 25, 20, 15, 10 15, 20, 25, 30, 30, 25, 20, 15 15, 20, 25, 30, 30, 25, 20, 15 10, 15, 20, 25, 25, 20, 15, 10 5, 10, 15, 20, 20, 15, 10, 5 }
var result = arrayreflection(scoreboard); public static t[,] arrayreflection<t>(t[,] arr) { // number of rows in array int m = arr.getlength(0); // number of columns in array int n = arr.getlength(1); var res = new t[m*2, n*2]; for(int r=0; r<m; r++) for(int c=0; c<n; c++) { res[r,c] = arr[r,c]; res[r,n + c] = arr[r, n - c - 1]; res[r + m,c] = arr[m - r - 1, c]; res[r+m,n+c] = arr[m - r - 1, n - c - 1]; } return res; }
Comments
Post a Comment