Created
January 16, 2018 22:13
-
-
Save dandohotaru/77668db7b3c85ff7fb2239789e3e13e0 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
// How could I map my area from a Tile[,] to a int[,], where only the height is saved? | |
void Main() | |
{ | |
var area = new Tile[7, 10]; | |
var rows = area.GetLength(0); | |
var columns = area.GetLength(1); | |
var random = new Random(); | |
for (var i = 0; i < rows; i++) | |
{ | |
for (var j = 0; j < columns; j++) | |
{ | |
area[i, j] = new Tile | |
{ | |
Height = i + j, | |
Terrain = random.Next(1, rows * columns), | |
}; | |
} | |
} | |
area.Select(p => p.Height).Dump(); | |
} | |
public class Tile | |
{ | |
public int Height; | |
public int Terrain; | |
} | |
public static class Extensions | |
{ | |
public static TOutput[,] Select<TInput, TOutput>(this TInput[,] instance, Func<TInput, TOutput> mapper) | |
{ | |
var rows = instance.GetLength(0); | |
var columns = instance.GetLength(1); | |
var result = new TOutput[rows, columns]; | |
for (int i = 0; i < rows; i++) | |
{ | |
for (int j = 0; j < columns; j++) | |
{ | |
var item = instance[i, j]; | |
result[i, j] = mapper(item); | |
} | |
} | |
return result; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment