C#/수업과제

03/19 2차원 배열 2048 구현

박준희 2021. 3. 22. 00:58
728x90

 

Program.cs

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

namespace Study06
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main");
            new App();
        }
    }
}

 

App.cs

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

namespace Study06
{
    public class App
    {     
        public App()
        {
            Console.WriteLine("App");

            GameLauncher gameLauncher = new GameLauncher();
            gameLauncher.Init();
            gameLauncher.PrintPlate();
            //gameLauncher.StartGame();


        }        
    }
}

 

GameLauncher.cs

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

namespace Study06
{
    public class GameLauncher
    {
        public enum eDirection
        {
            Left,
            Right,
            Up,
            Down
        }

        private int[,] arrPlate;

        public GameLauncher()
        {

        }

        //초기화
        public void Init()
        {
            this.CreatePlate();
        }

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

        //빈 자리 중 한 칸에 랜점하게 2 또는 4가 나온다.
        public void CreateBlock()
        {
            List<Brock> listTmp = new List<Brock>();
            Random random = new Random();

            for (int y = 0; y < arrPlate.GetLength(0); y++)
            {
                for (int x = 0; x < arrPlate.GetLength(1); x++)
                {
                    if (arrPlate[x, y] == 0)
                    {
                        listTmp.Add(new Brock() { Row = x, Col = y });
                    }
                }
            }
            Brock randomBrock = new Brock();
            randomBrock = listTmp[random.Next(0, listTmp.Count)];
            randomBrock.Value = random.Next() % 2 == 0 ? 2 : 4;

            arrPlate[randomBrock.Row, randomBrock.Col] = randomBrock.Value;
            Console.WriteLine("({0}, {1})에 숫자({2}) 생성\n", randomBrock.Row, randomBrock.Col, randomBrock.Value);

        }

        //게임 시작
        public void StartGame()
        {
            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey();
                switch (keyInfo.Key)
                {
                    case ConsoleKey.LeftArrow:
                        {
                            CalculateBlocks(eDirection.Left);
                            CreateBlock();
                            BlockMovementCheck();
                            PrintPlate();
                            break;
                        }
                    case ConsoleKey.RightArrow:
                        {
                            CalculateBlocks(eDirection.Right);
                            CreateBlock();
                            BlockMovementCheck();
                            PrintPlate();
                            break;
                        }
                    case ConsoleKey.UpArrow:
                        {
                            CalculateBlocks(eDirection.Up);
                            CreateBlock();
                            BlockMovementCheck();
                            PrintPlate();
                            break;
                        }
                    case ConsoleKey.DownArrow:
                        {
                            CalculateBlocks(eDirection.Down);
                            CreateBlock();
                            BlockMovementCheck();
                            PrintPlate();
                            break;
                        }
                    case ConsoleKey.Escape:
                        {
                            EndGame();
                            break;
                        }
                    default:
                        break;
                }
            }
        }
        //게임 종료
        public void EndGame()
        {
            Console.WriteLine("게임이 종료되었습니다.");
            Environment.Exit(0);
        }

        public void GameOver()
        {
            Console.WriteLine("GAMEOVER");
            Environment.Exit(0);
        }

        //판 출력
        public void PrintPlate()
        {
            Console.WriteLine("방향키 : →  ← ↑ ↓ 종료 : ESC\n");

            for (int x = 0; x < arrPlate.GetLength(0); x++)
            {
                for (int y = 0; y < arrPlate.GetLength(1); y++)
                {
                    Console.Write("{0, 5}", arrPlate[x, y]);
                }
                Console.WriteLine("\n");
            }
        }

        //합칠 수 있는 블럭이 있거나, 빈 공간이 있는가
        public void BlockMovementCheck()
        {            
            for (int i = 0; i < arrPlate.GetLength(0); i++)
            {
                for (int j = 0; j < arrPlate.GetLength(1); j++)
                {
                    if (arrPlate[i, j] == 0)
                    {
                        return;
                    }
                }
            }

            for (int i = 0; i < arrPlate.GetLength(0); i++)
            {
                for (int j = 0; j < arrPlate.GetLength(1)-1; j++)
                {
                    if (arrPlate[i, j] == arrPlate[i, j+1])
                    {
                        return;
                    }
                }
            }

            for (int i = 0; i < arrPlate.GetLength(0)-1; i++)
            {
                for (int j = 0; j < arrPlate.GetLength(1); j++)
                {
                    if (arrPlate[i, j] == arrPlate[i + 1, j])
                    {
                        return;
                    }
                }
            }

            GameOver();
        }

        //블럭 계산
        public void CalculateBlocks(eDirection direction)
        {
            int[,] arrTmp = new int[arrPlate.GetLength(0), arrPlate.GetLength(1)];
            int tmp = 0;

            try
            {
                switch (direction)
                {
                    case eDirection.Left:
                        {
                            tmp = 0;
                            for (int i = 0; i < arrPlate.GetLength(0); i++)
                            {
                                for (int j = 0; j < arrPlate.GetLength(1); j++)
                                {
                                    if (arrPlate[i, j] != 0)
                                    {
                                        arrTmp[i, tmp] = arrPlate[i, j];
                                        tmp++;
                                    }
                                }
                                tmp = 0;
                            }

                            for (int i = 0; i < arrTmp.GetLength(0); i++)
                            {
                                for (int j = 0; j < arrTmp.GetLength(1) - 1; j++)
                                {
                                    if (arrTmp[i, j] == arrTmp[i, j + 1])
                                    {
                                        arrTmp[i, j] += arrTmp[i, j + 1];
                                        for (int k = j + 1; k < arrTmp.GetLength(1) - 1; k++)
                                        {
                                            arrTmp[i, k] = arrTmp[i, k + 1];
                                        }
                                        arrTmp[i, arrTmp.GetLength(1) - 1] = 0;
                                    }
                                }
                            }
                            break;
                        }
                    case eDirection.Right:
                        {
                            tmp = arrPlate.GetLength(1) - 1;
                            for (int i = arrPlate.GetLength(0) - 1; i >= 0; i--)
                            {
                                for (int j = arrPlate.GetLength(1) - 1; j >= 0; j--)
                                {
                                    if (arrPlate[i, j] != 0)
                                    {
                                        arrTmp[i, tmp] = arrPlate[i, j];
                                        tmp--;
                                    }
                                }
                                tmp = arrPlate.GetLength(1) - 1;
                            }

                            for (int i = arrTmp.GetLength(0) - 1; i >= 0; i--)
                            {
                                for (int j = arrTmp.GetLength(1) - 1; j > 0; j--)
                                {
                                    if (arrTmp[i, j] == arrTmp[i, j - 1])
                                    {
                                        arrTmp[i, j] += arrTmp[i, j - 1];
                                        for (int k = j - 1; k > 0; k--)
                                        {
                                            arrTmp[i, k] = arrTmp[i, k - 1];
                                        }
                                        arrTmp[i, 0] = 0;
                                    }
                                }
                            }
                            break;
                        }
                    case eDirection.Up:
                        {
                            tmp = 0;
                            for (int i = 0; i < arrPlate.GetLength(0); i++)
                            {
                                for (int j = 0; j < arrPlate.GetLength(1); j++)
                                {
                                    if (arrPlate[j, i] != 0)
                                    {
                                        arrTmp[tmp, i] = arrPlate[j, i];
                                        tmp++;
                                    }
                                }
                                tmp = 0;
                            }

                            for (int i = 0; i < arrTmp.GetLength(0); i++)
                            {
                                for (int j = 0; j < arrTmp.GetLength(1) - 1; j++)
                                {
                                    if (arrTmp[j, i] == arrTmp[j + 1, i])
                                    {
                                        arrTmp[j, i] += arrTmp[j + 1, i];
                                        for (int k = j + 1; k < arrTmp.GetLength(1) - 1; k++)
                                        {
                                            arrTmp[k, i] = arrTmp[k + 1, i];
                                        }
                                        arrTmp[arrTmp.GetLength(1) - 1, i] = 0;
                                    }
                                }
                            }
                            break;
                        }
                    case eDirection.Down:
                        {
                            tmp = arrPlate.GetLength(1) - 1;
                            for (int i = arrPlate.GetLength(0) - 1; i >= 0; i--)
                            {
                                for (int j = arrPlate.GetLength(1) - 1; j >= 0; j--)
                                {
                                    if (arrPlate[j, i] != 0)
                                    {
                                        arrTmp[tmp, i] = arrPlate[j, i];
                                        tmp--;
                                    }
                                }
                                tmp = arrPlate.GetLength(1) - 1;
                            }

                            for (int i = arrTmp.GetLength(0) - 1; i >= 0; i--)
                            {
                                for (int j = arrTmp.GetLength(1) - 1; j > 0; j--)
                                {
                                    if (arrTmp[j, i] == arrTmp[j - 1, i])
                                    {
                                        arrTmp[j, i] += arrTmp[j - 1, i];
                                        for (int k = j - 1; k > 0; k--)
                                        {
                                            arrTmp[k, i] = arrTmp[k - 1, i];
                                        }
                                        arrTmp[0, i] = 0;
                                    }
                                }
                            }
                            break;
                        }
                    default:
                        break;
                }
            }
            catch (ArgumentOutOfRangeException e)
            {
                GameOver();
            }
            
            arrPlate = arrTmp;


        }
    }
}

 

Brock.cs

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

namespace Study06
{
    public class Brock
    {
        public int Row { get; set; }
        public int Col { get; set; }
        public int Value { get; set; }
    }
}

 

프로젝트 파일

Study06.zip
0.04MB

실행파일

 

 

Study06.exe
0.01MB

728x90