본문 바로가기

자료구조

배열연습 빈공간이면 (0이면) 계속 이동

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]);
                    }
                }
            }
        }
    }
}

HelloWorld.exe
0.01MB