using System;
class App
{
int prevNum;
int[] arr;
//생성자
public App()
{
this.arr = new int[5];
//랜덤 숫자 구하기
Random rand = new Random();
int randNum = rand.Next(0, 5); //0에서 4까지
int randNum2 = rand.Next(1, 3) * 2; //2또는 4
this.prevNum = randNum;
this.arr[randNum] = 2;
int[] arrTemp = new int[5];
int tempIdex = 0;
//빈공간 찾기 (값이 0인 인덱스 찾기)
//값이 0인 인덱스들을 새로운 배열에 넣어서 랜덤숫자.
//for (int i = 0; i < this.arr.Length; i++) //0~4
//{
// if (this.arr[i] == 0)
// {
// Console.WriteLine("빈공간 index: {0}", i);
// arrTemp[tempIdex++] = i;
// }
//}
//for (int i = 0; i < arrTemp.Length; i++)
//{
// Console.WriteLine(arrTemp[i]);
//}
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.WriteLine();
while (true)
{
Console.WriteLine("입력대기");
ConsoleKeyInfo info = Console.ReadKey();
Console.WriteLine();
ConsoleKey theKey = info.Key;
if (theKey == ConsoleKey.LeftArrow)
{
if (arr[0] != 0)
{
Console.WriteLine("막혀있습니다.");
}
else
{
while(true)
{
if (arr[randNum] == 2 && arr[randNum - 1] == 0)
{
prevNum = randNum;
randNum -= 1;
arr[randNum] = 2;
arr[prevNum] = 0;
}
if (randNum == 0)
{
break;
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
}
}
else if (theKey == ConsoleKey.RightArrow)
{
if (arr[4] != 0)
{
Console.WriteLine("막혀있습니다.");
}
else
{
while (true)
{
if (arr[randNum] == 2 && arr[randNum + 1] == 0)
{
prevNum = randNum;
randNum += 1;
arr[randNum] = 2;
arr[prevNum] = 0;
}
if (randNum == 4)
{
break;
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write("{0} ", arr[i]);
}
}
}
}
}
}
'자료구조' 카테고리의 다른 글
2차원 배열 (0) | 2021.12.25 |
---|---|
배열 복습 - 아이템 배열, 값 뒤집기 (0) | 2021.12.25 |
배열에서 최소, 최대, 평균 계산하기 (0) | 2021.12.25 |
배열연습 + swap 한칸씩이동. [수정] (0) | 2021.12.25 |
배열연습 인벤토리만들기(차례대로넣기) (0) | 2021.12.25 |