아카이브
2차원 배열 본문
internal class App
{
int[,] playerMap;
int rowIdx;
int colIdx;
public App()
{
int[,] map = {
{1,1,1},
{1,1,2}
};
this.playerMap = new int[map.GetLength(0), map.GetLength(1)];
this.PrintMap(map);
this.PrintSpace();
this.PrintMap(playerMap);
//초기 위치 설정
//------------------------------------------------
this.rowIdx = 1;
this.colIdx = 2;
playerMap[this.rowIdx, this.colIdx] = 100;
//------------------------------------------------
this.PrintSpace();
this.PrintMap(playerMap);
this.MoveLeft();
PrintSpace();
this.PrintMap(playerMap);
}
void MoveLeft()
{
playerMap[this.rowIdx, this.colIdx - 1] = 100;
playerMap[this.rowIdx, this.colIdx] = 0;
this.colIdx -= 1;
Console.WriteLine("왼쪽으로 이동했습니다.[{0},{1}], {2}", this.rowIdx, this.colIdx, this.playerMap[this.rowIdx, this.colIdx]);
}
void PrintSpace()
{
Console.WriteLine();
}
void PrintMap(int[,] arr)
{
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
int element = arr[i, j];
Console.Write("[{0},{1}] : {2}\t", i, j, element);
}
Console.WriteLine();
}
}
}
}