Skip to content

Instantly share code, notes, and snippets.

@dandohotaru
Created January 16, 2018 22:13
Show Gist options
  • Save dandohotaru/77668db7b3c85ff7fb2239789e3e13e0 to your computer and use it in GitHub Desktop.
Save dandohotaru/77668db7b3c85ff7fb2239789e3e13e0 to your computer and use it in GitHub Desktop.
// 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