2019-10-22 22일차 2048게임 만들기

Console Programming/C# Console 2019. 10. 24. 10:19

Program.cs

namespace Sytanx01
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();
        }
    }
}

App.cs

using System;

namespace Sytanx01
{
    public class App
    {
        enum EDir { Right, Left, Top, Bottom };

        int[,] arrBase = new int[4, 4];
        public App()
        {
            //Random rand = new Random();

            //int num1 = rand.Next(0, 2);
            //int num2 = rand.Next(0, 2);           

            //int row1 = rand.Next(0, arrBase.GetLength(0));
            //int col1 = rand.Next(0, arrBase.GetLength(1));
            //int row2 = rand.Next(0, arrBase.GetLength(0));
            //int col2 = rand.Next(0, arrBase.GetLength(1));
            //while (row1 == row2 && col1 == col2)
            //{
            //    row2 = rand.Next(0, arrBase.GetLength(0));
            //    col2 = rand.Next(0, arrBase.GetLength(1));
            //}

            //if (num1 == 0)
            //{
            //    arrBase[row1, col1] = 2;
            //}
            //else
            //{
            //    arrBase[row1, col1] = 4;
            //}
            //if (num2 == 0)
            //{
            //    arrBase[row2, col2] = 2;
            //}
            //else
            //{
            //    arrBase[row2, col2] = 4;

            //}
            
            //테스트를 위해 첫 행만 2,2,2,2로 놓았다
            arrBase[0, 0] = 2;
            arrBase[0, 1] = 2;
            arrBase[0, 2] = 2;
            arrBase[0, 3] = 2;



            Show();
            Console.WriteLine("========================");
            Move(0);
            Show();
            Console.WriteLine("========================");
            

        }
        //public int[] GetIndex()
        //{
        //    Console.WriteLine($"(row) (col) (direction)\t(0:Right, 1:Left, 2:Top, 3:Bottom)");
        //    string str = Console.ReadLine();
        //    string[] str2 = str.Split(' ');
        //    int[] arr = new int[3];
        //    arr[0] = int.Parse(str2[0]);
        //    arr[1] = int.Parse(str2[1]);
        //    arr[2] = int.Parse(str2[2]);
        //    return arr;
        //}
        public void Move(int dir)
        {
            switch (dir)
            {
                case (int)EDir.Right: //오른쪽으로 움직임
                    {
                        for (int j = 3; j > 0; j--)//자신의 좌측을 비교해 자신이 0이면 좌측의 숫자를 가져오는 부분
                        {
                            for (int i = j; i > 0; i--)
                            {
                                if (i - 1 > -1 && arrBase[0, i] == 0)
                                {
                                    arrBase[0, i] = arrBase[0, i - 1];
                                    arrBase[0, i - 1] = 0;
                                }
                            }
                        }
                        for (int j = 3; j > 0; j--)//자신의 좌측숫자와 비교해서 같으면 더하고 좌측은 0으로만드는 부분
                        {
                            for (int i = j; i > 0; i--)
                            {
                                if (arrBase[0, i] == arrBase[0, i - 1])
                                {
                                    arrBase[0, i] += arrBase[0, i - 1];
                                    arrBase[0, i - 1] = 0;
                                }
                            }
                        }
                    }

                        break;
                    
                
                default:
                    break;
            }



        }
        public void Show()
        {
            for (int i = 0; i < arrBase.GetLength(0); i++)
            {
                for (int j = 0; j < arrBase.GetLength(1); j++)
                {
                    Console.Write($"{arrBase[i, j]}\t");
                }
                Console.WriteLine();
            }
        }

    }
}

 

 

실행 결과 

 

4열과 3열을 보고 2,2가  0,4로 바뀌었고

2열과 1열을 보고 2,2가 0,4로 바뀌었다.

 

그러나 0,0,4,4로 되지는 않았다

: