C#/游戏2048的核心代码

    //类1
    ///游戏类核心算法,与平台无关
    class GameCore
    {
        private int[,] map;    //地图的字段
        private int[] mergeArray;       //合并数组
        private int[] removeZeroArray;  //用来进行移动的辅助数组
        private int[,] originalMap;     //原地图,用于比较移动后地图是否变化
        public bool IsChange { get; set; }   // 用于判断地图是否发生改变
        private List emptyLOC;//用于统计空位置的List(长度可变数组)
        private Random random;          //用于生成随机位置和随机数

        public int[,] Map      //地图的属性
        {
            get { return map; }
        }

        public GameCore()      //构造函数
        {
            map = new int[4, 4];
            mergeArray = new int[map.GetLength(0)];
            removeZeroArray = new int[4];
            emptyLOC = new List(16); 
            random = new Random();
            originalMap = new int[4, 4];
        }
      
        private void RemovwZero()  //把零调整到后边,非零数按顺序调整到左边
        {
            Array.Clear(removeZeroArray, 0, 4);

            int index = 0;
            for (int i = 0; i < mergeArray.Length; i++)
            {
                if (mergeArray[i] != 0)
                    removeZeroArray[index++] = mergeArray[i];
            }
            removeZeroArray.CopyTo(mergeArray, 0);
        }
        
        private void Merge()  //合并,即相同的数字相加
        {
            RemovwZero();
            for(int i=0;i= 0; r--)
                    mergeArray[3 - r] = map[r, c];

                Merge();
                for (int r = map.GetLength(0) - 1; r >= 0; r--)
                    map[r, c] = mergeArray[3 - r];
            }
        }

        private void MoveLeft()
        {
            for(int r=0;r<4;r++)
            {
                for (int c = 0; c < 4; c++)
                    mergeArray[c] = map[r, c];

                Merge();
                for (int c = 0; c < 4; c++)
                    map[r, c] = mergeArray[c];
            }
        }

        private void MoveRight()
        {
            for(int r=0;r<4;r++)
            {
                for (int c = 3; c >= 0; c--)
                    mergeArray[3-c] = map[r, c];
                Merge();
                for(int c=3;c>=0;c--)
                {
                    map[r, c] = mergeArray[3 - c];
                }
            }
        }

        //将移动的调用简化的函数
        public void Move(MoveDirection direction)
        {
            Array.Copy(map, originalMap, map.Length);
            IsChange = false;

            switch(direction)
            {
                case MoveDirection.Up:  MoveUp();break;
                case MoveDirection.Down: MoveDown(); break;
                case MoveDirection.Left: MoveLeft(); break;
                case MoveDirection.Right: MoveRight(); break;
            }
            //移动后对比
            CheckMapChange();
        }

        
        private void CheckMapChange()
        {
            for(int r=0;r 0) return false;
            for(int r=0;r<4;r++)
                for(int c=0;c<3;c++)
                {
                    if (map[r,c]==map[r,c+1]||map[c,r]==map[c+1,r])
                        return false;
                }
            return true;
        }
    }


    //类2 
    //用于计算空白位置时的辅助类
    class Location
    {
        public int RIndex { get; set; }

        public int CIndex { get; set; }

        public Location(int r,int c)
        {
            RIndex = r;
            CIndex = c;
        }

    }


    //枚举,用于表示方向
    enum MoveDirection
    {
        Up,
        Down,
        Left,
        Right
    }


    //主函数
    class Program
    {
        private static GameCore core; //全局静态变量
        static void Main(string[] args)
        {
            core = new GameCore();

            core.GenerateNumber();
            core.GenerateNumber();
            PrintMap();
            while (true)
            {
                KeyDown();   //输入“w”、“s”、“a”、“d”,并发生移动
                if(core.IsChange) //如果地图改变
                {
                    core.GenerateNumber();  //生成随机数
                    PrintMap();             //打印地图
                    if(core.IsOver())
                    {
                        Console.WriteLine("游戏结束");
                    }
                }
            }
            
        }

        private static void PrintMap() //打印地图
        {
            Console.Clear();   //清空控制台
            for(int r=0;r<4;r++)
            {
                for(int c=0;c<4;c++)
                {
                    Console.Write(core.Map[r, c] + "\t");
                }
                Console.WriteLine();
            }
        }

        private static void KeyDown()
        {
            switch(Console.ReadLine())
            {
                case"w":
                    core.Move(MoveDirection.Up);break;
                case"s":
                    core.Move(MoveDirection.Down); break;
                case"a":
                    core.Move(MoveDirection.Left); break;
                case"d":
                    core.Move(MoveDirection.Right); break;
            }
        }
    }

你可能感兴趣的:(C#/游戏2048的核心代码)