2048.exe
0.01MB

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

namespace _2048
{
    internal class App
    {
        public App()
        {
            Game2048 game = new Game2048();
            game.Start();

            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2048
{
    internal class Game2048
    {
        private int[] board = new int[4];
        private Random random = new Random();
        private bool gameOver = false;

        public Game2048() { }

        public void Start()
        {
            GenerateNumber();
            PrintBoard();

            while (!gameOver)
            {
                var key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.LeftArrow)
                {
                    if (!CanMoveLeft()) continue;
                    MoveLeft();
                }
                else if (key == ConsoleKey.RightArrow)
                {
                    if (!CanMoveRight()) continue;
                    MoveRight();
                }
                else
                {
                    continue;
                }
                GenerateNumber();
                PrintBoard();

                if (!CanMove())
                {
                    Console.WriteLine("게임을 진행할 수 없습니다.");
                    gameOver = true;
                }
            }
        }

        private void MoveLeft()
        {
            List<int> newBoard = new List<int>();
            bool shouldMerge = false;
            for (int i = 0; i < 4; i++)
            {
                if (board[i] != 0)
                {
                    if (newBoard.Count > 0 && newBoard.Last() == board[i] && !shouldMerge)
                    {
                        newBoard[newBoard.Count - 1] *= 2;
                        shouldMerge = true;
                    }
                    else
                    {
                        newBoard.Add(board[i]);
                        shouldMerge = false;
                    }
                }
            }
            while (newBoard.Count < 4)
            {
                newBoard.Add(0);
            }
            board = newBoard.ToArray();
        }

        private void MoveRight()
        {
            List<int> newBoard = new List<int>();
            bool shouldMerge = false;
            for (int i = 3; i >= 0; i--)
            {
                if (board[i] != 0)
                {
                    if (newBoard.Count > 0 && newBoard.First() == board[i] && !shouldMerge)
                    {
                        newBoard[0] *= 2;
                        shouldMerge = true;
                    }
                    else
                    {
                        newBoard.Insert(0, board[i]);
                        shouldMerge = false;
                    }
                }
            }
            while (newBoard.Count < 4)
            {
                newBoard.Insert(0, 0);
            }
            board = newBoard.ToArray();
        }


        private bool CanMoveLeft()
        {
            for (int i = 0; i < 3; i++)
            {
                if (board[i] == 0 && board[i + 1] != 0 || board[i] != 0 && board[i] == board[i + 1])
                    return true;
            }
            return false;
        }

        private bool CanMoveRight()
        {
            for (int i = 3; i > 0; i--)
            {
                if (board[i] == 0 && board[i - 1] != 0 || board[i] != 0 && board[i] == board[i - 1])
                    return true;
            }
            return false;
        }


        private void GenerateNumber()
        {
            List<int> emptyIndices = new List<int>();

            for (int i = 0; i < board.Length; i++)
            {
                if (board[i] == 0)
                {
                    emptyIndices.Add(i);
                }
            }

            if (emptyIndices.Count > 0)
            {
                int value = (random.Next(10) < 9) ? 2 : 4;
                int index = emptyIndices[random.Next(emptyIndices.Count)];
                board[index] = value;
            }
        }

        private void PrintBoard()
        {
            Console.Clear();
            foreach (int number in board)
            {
                Console.Write("[{0}]", number);
            }
            Console.WriteLine();
        }

        private bool CanMove()
        {
            for (int i = 0; i < board.Length - 1; i++)
            {
                if (board[i] == 0 || board[i] == board[i + 1])
                    return true;
            }
            if (board[board.Length - 1] == 0) 
                return true;
            return false;
        }
    }

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

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

반응형

'KDT > C# 프로그래밍' 카테고리의 다른 글

싱글톤 패턴  (0) 2023.07.26
디자인 패턴  (0) 2023.07.26
2차원 배열 맵에서 플레이어 키보드 이동  (0) 2023.07.26
2차원 배열 이동  (0) 2023.07.25
아이템 순차 출력  (0) 2023.07.25

+ Recent posts