Unity3D/C#

2048

일등하이 2021. 3. 19. 14:14
반응형

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class GameLauncher
    {
        private int[] arrPlate; //판 
        //생성자 
        public GameLauncher()
        {

        }

        //초기화 
        public void Init()
        {
            Console.WriteLine("초기화 합니다.");
            this.CreatePlate();
        }

        //새로운 판을 생성
        public void CreatePlate()
        {
            Console.WriteLine("새로운 판을 생성합니다.");
            this.arrPlate = new int[4];
            this.CreateBlock();
        }

        //빈 자리 중 한 칸에 랜덤하게 2 또는 4가 나온다.
        public void CreateBlock()
        {
            // Console.WriteLine("CreateBlock");
            PrintPlate();
            List<int> list = new List<int>();
            int idx = 0;
            for (int i = 0; i < this.arrPlate.Length; i++)
            {
                if (this.arrPlate[i] == 0)
                {
                    list.Add(i);
                }

            }
            foreach (int a in list)
            {
                Console.Write("{0} ", a);
            }
            Console.WriteLine("list.Count: {0}", list.Count);
            if (list.Count == 0)
            {
                Console.WriteLine("판이 꽉참");
                return;
            }
            System.Random random = new System.Random();
            int index = list[random.Next(list.Count)];
            Console.WriteLine("index: {0}", index);
            var num = random.Next(1, 3) * 2;
            this.arrPlate[index] = num;
            PrintPlate();
        }

        private void PrintPlate()
        {
            for (int i = 0; i < this.arrPlate.Length; i++)
            {
                Console.Write(this.arrPlate[i] + " ");
            }
            Console.WriteLine();
        }



        //게임 시작
        public void StartGame()
        {
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                if (keyInfo.Key == ConsoleKey.LeftArrow)
                {
                    Console.WriteLine("<-");
                    CreateBlock();
                }
                else if (keyInfo.Key == ConsoleKey.RightArrow)
                {
                    Console.WriteLine("->");
                    CreateBlock();
                }
                else if (keyInfo.Key == ConsoleKey.Escape)
                {
                    this.EndGame();
                    break;
                }
            }
        }

        //게임 종료 
        public void EndGame()
        {
            Console.WriteLine("게임 종료");
        }
    }
}
반응형