I have a grid a matrix of tile.
enum Tile
{
Nothing = 0,
Forest = 1,
River = 2,
OB = 3
}
class Grid
{
public const int X = 6;
public const int Y = 6;
private Tile[,] Board { get; set; }
public Grid()
{
Board = new Tile[X, Y];
}
}
I want to have a Ctor that take int[,] board
.
Edge case that are not :
- Different size. can be checked with
.GetLength(0)
to match const int X . Throw. - Value that that are unknow
(Tile)12
, Set to default Value int =0.
So I have
public Grid(int[,] board)
{
if (board.GetLength(0) != Y || board.GetLength(1) != X) {
throw new ArgumentException();
}
Board = new Tile[Y, X];
for (int i = 0; i < Y; i++)
for (int j = 0; j < X; j++) {
var enumValue = (Enum.IsDefined(typeof(Tile),board[i, j]))? (Tile)board[i, j]: default(Tile) ;
Board[i, j] = enumValue;
}
}
Is there a straight Foward way to cast int 2d array int[,]
to enum 2d array enum[,]
?
I try various combinaison of Cast, as, (). Without sucess.