2048
2023. 7. 26.

-2,4,8,16,...2^으로 숫자가 커진다
-4x4꼴에서 빈칸 중 랜덤한 위치에 2가 2칸 생긴다. 
-방향키로 움직인다: (상,하,좌,우) 키입력받기
-칸 움직이기 ->

 

1. 크기가 4인 배열을 만든 후 좌, 우 방향으로 움직이게 하였다.

-방향키 명과 움직인 후를 출력한다.

 

1
2048.exe
0.00MB

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

namespace _2048
{
    internal class Game
    {
        //멤버 변수
        ConsoleKeyInfo info;
        int[] arr = { 0,0,0,0};
        int num;
        //생성자
        public Game()
        {

        }

        public void GameStart()
        {
            Console.WriteLine("게임시작\n");

            PrintBoard();   
            while (true)
            {
                ReadKey();
               
            }
         


        }
        void ReadKey()
        {
           
            this.info = Console.ReadKey();
            Console.WriteLine("{0}", this.info.Key);
            

            if(info.Key == ConsoleKey.RightArrow)
            {
                MoveRight(info);
            }
            if (info.Key == ConsoleKey.LeftArrow)
            {
                MoveLeft(info);
            }


        }
        void PrintBoard()
        {
            //보드 만들기 연습

            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write("{0}", arr[i]);
               
            }
            Console.WriteLine("\n");
        }

        void MoveLeft(ConsoleKeyInfo infoKey)
        {//왼쪽 방향키 입력시 왼쪽으로 이동
            
            if (info.Key == ConsoleKey.LeftArrow)
            {
                if (arr[num] != 0)
                {                
                    arr[num-1] = arr[num];                  
                    arr[num] = 0;
                    num--;
                    PrintBoard();
                }
                else
                {
                    arr[0] = 1;               
                    PrintBoard();
                }
            }

        }
        void MoveRight(ConsoleKeyInfo infoKey)
        {//오른쪽 방향키 입력시 오른쪽으로 이동

        //    if (info.Key == ConsoleKey.RightArrow)
            {
                if (arr[num] != 0)
                {
                    arr[num +1] = arr[num];
                    arr[num] = 0;
                    num++;
                    PrintBoard();
                }
                else
                {
                    arr[0] = 1;                
                    PrintBoard();

                }
               

            }

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

namespace _2048
{
    internal class App
    {
        public App()
        {
            Game game = new Game();
            game.GameStart();
        }
    }
}

 

myoskin