Advertisement

c#控制台小游戏——合成2048源码

阅读量:

为了丰富日常生活的兴趣爱好而自主学习的小青年,在观看Unity教学课程后开启了自学C#编程的道路,在持续深入地进行基础学习之后,掌握了二维数组知识的他深知自己的游戏开发大门已悄然打开。哈哈....(欢欣鼓舞中...),于是他经过不懈努力终于成功实现了自己制作的第一个C#控制台小游戏——经典的合成2048游戏。

以下为游戏截图:

图一为游戏说明:

图二为游戏设置:

图三为棋盘大小是(4*4)的游戏截图:

图四为游戏结束后的结算:

图五为棋盘大小是(5*5)的游戏截图:

图六为设置自定义棋盘的截图:

图七为自定义或随机生成棋盘大小是(3*10)的游戏截图

以下为游戏源码:

开发中使用的是基于Linux系统的代码,在Windows环境下运行可能会遇到一些小问题,需要进行轻微的调整。

复制代码
 class Game2048

    
 {
    
     static Random random = new Random();
    
     static int randomNum = 2, row, column;
    
  
    
     private static int GetNumber(int result)
    
     {
    
     int[] array = { 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 };
    
     int a;
    
     do
    
     {
    
         a = random.Next(0, randomNum);
    
     } while (array[a] > result);
    
  
    
     return array[a];
    
     }
    
  
    
     private static int GetMaximumInArray(int[,] array)
    
     {
    
     int maxinum = array[0, 0];
    
     for (int c = 0; c < array.GetLength(0); c++)
    
     {
    
         for (int r = 0; r < array.GetLength(1); r++)
    
         {
    
             if (maxinum < array[c, r]) maxinum = array[c, r];
    
         }
    
     }
    
     return maxinum;
    
     }
    
  
    
     private static int[] RetropositionZero(int[] array)
    
     {
    
     int[] newArray = new int[array.Length];
    
     int indexes = 0;
    
     for (int i = 0; i < array.Length; i++)
    
     {
    
         if (array[i] != 0) newArray[indexes++] = array[i];
    
     }
    
     return newArray;
    
     }
    
  
    
     private static int[] Merge(int[] array)
    
     {
    
     array = RetropositionZero(array);
    
     for (int i = 0; i < array.Length - 1; i++)
    
     {
    
         if (array[i] != 0 && array[i] == array[i + 1])
    
         {
    
             array[i] += array[i + 1];
    
             array[i + 1] = 0;
    
         }
    
     }
    
     array = RetropositionZero(array);
    
     return array;
    
     }
    
  
    
     private static int[,] ArrayCalculateRight(int[,] array)
    
     {
    
     int[] newArray = new int[array.GetLength(1)];
    
     for (int r = 0; r < array.GetLength(0); r++)
    
     {
    
         for (int c = 0; c < newArray.Length; c++)
    
         {
    
             newArray[c] = array[r, newArray.Length - 1 - c];
    
         }
    
         newArray = Merge(newArray);
    
         for (int c = 0; c < newArray.Length; c++)
    
         {
    
             array[r, newArray.Length - 1 - c] = newArray[c];
    
         }
    
     }
    
     return array;
    
     }
    
  
    
     private static int[,] ArrayCalculateLeft(int[,] array)
    
     {
    
     int[] newArray = new int[array.GetLength(1)];
    
     for (int r = 0; r < array.GetLength(0); r++)
    
     {
    
         for (int c = 0; c < newArray.Length; c++)
    
         {
    
             newArray[c] = array[r, c];
    
         }
    
         newArray = Merge(newArray);
    
         for (int c = 0; c < newArray.Length; c++)
    
         {
    
             array[r, c] = newArray[c];
    
         }
    
     }
    
     return array;
    
     }
    
  
    
     private static int[,] ArrayCalculateUp(int[,] array)
    
     {
    
     int[] newArray = new int[array.GetLength(0)];
    
     for (int c = 0; c < array.GetLength(1); c++)
    
     {
    
         for (int r = 0; r < newArray.Length; r++)
    
         {
    
             newArray[r] = array[r, c];
    
         }
    
         newArray = Merge(newArray);
    
         for (int r = 0; r < newArray.Length; r++)
    
         {
    
             array[r, c] = newArray[r];
    
         }
    
     }
    
     return array;
    
     }
    
  
    
     private static int[,] ArrayCalculateDown(int[,] array)
    
     {
    
     int[] newArray = new int[array.GetLength(0)];
    
     for (int c = 0; c < array.GetLength(1); c++)
    
     {
    
         for (int r = 0; r < newArray.Length; r++)
    
         {
    
             newArray[r] = array[newArray.Length - 1 - r, c];
    
         }
    
         newArray = Merge(newArray);
    
         for (int r = 0; r < newArray.Length; r++)
    
         {
    
             array[newArray.Length - 1 - r, c] = newArray[r];
    
         }
    
     }
    
     return array;
    
     }
    
  
    
     private static bool IsBlank(int[,] array)
    
     {
    
     for (int c = 0; c < array.GetLength(0); c++)
    
     {
    
         for (int r = 0; r < array.GetLength(1); r++)
    
         {
    
             if (array[c, r] == 0) return true;
    
         }
    
     }
    
     return false;
    
     }
    
  
    
     private static int SumByArray(int[,] array)
    
     {
    
     int result = 0;
    
     foreach (var item in array)
    
     {
    
         result += item;
    
     }
    
     return result;
    
     }
    
  
    
     private static bool IsSameAdjacentElementInArray(int[,] array)
    
     {
    
     for (int c = 0; c < array.GetLength(0); c++)
    
     {
    
         for (int r = 0; r < array.GetLength(1) - 1; r++)
    
         {
    
             if (array[c, r] == array[c, r + 1]) return true;
    
         }
    
     }
    
     for (int c = 0; c < array.GetLength(1); c++)
    
     {
    
         for (int r = 0; r < array.GetLength(0) - 1; r++)
    
         {
    
             if (array[r, c] == array[r + 1, c]) return true;
    
         }
    
     }
    
     return false;
    
     }
    
  
    
     private static bool IsSameToTwoArray(int[,] array1, int[,] array2)
    
     {
    
     for (int c = 0; c < array1.GetLength(0); c++)
    
     {
    
         for (int r = 0; r < array1.GetLength(1); r++)
    
         {
    
             if (array1[c, r] != array2[c, r]) return false;
    
         }
    
     }
    
  
    
     return true;
    
     }
    
  
    
     private static void PrintArray(int[,] array)
    
     {
    
     System.Console.Write(" ");
    
     for (int i = 0; i < array.GetLength(1); i++)
    
     {
    
         System.Console.Write("———— ");
    
     }
    
     System.Console.WriteLine();
    
  
    
     for (int c = 0; c < array.GetLength(0); c++)
    
     {
    
         System.Console.Write("|");
    
         for (int r = 0; r < array.GetLength(1); r++)
    
         {
    
             if (array[c, r] == 0)
    
                 System.Console.Write("    |");
    
             else
    
             {
    
                 switch (array[c, r].ToString().Length)
    
                 {
    
                     case (1):
    
                         System.Console.Write("  {0} |", array[c, r]);
    
                         break;
    
                     case (2):
    
                         System.Console.Write(" {0} |", array[c, r]);
    
                         break;
    
                     case (3):
    
                         System.Console.Write(" {0}|", array[c, r]);
    
                         break;
    
                     case (4):
    
                         System.Console.Write("{0}|", array[c, r]);
    
                         break;
    
                 }
    
                 //System.Console.Write("{0:d4}|", array[c, r]);
    
             }
    
         }
    
         System.Console.WriteLine();
    
         System.Console.Write(" ");
    
         for (int i = 0; i < array.GetLength(1); i++)
    
         {
    
             System.Console.Write("———— ");
    
         }
    
         System.Console.WriteLine();
    
     }
    
  
    
     }
    
  
    
     private static int[] GameSet(int row, int column)
    
     {
    
     sign1: System.Console.WriteLine("请输入对应字母设置棋盘尺寸:");
    
     System.Console.WriteLine("A、4×4\tB、5×5\tC、自定义尺寸\tD、随机尺寸");
    
     string option = Console.ReadLine();
    
  
    
  
    
     switch (option)
    
     {
    
         case ("A"):
    
         case ("a"):
    
             row = 4; column = 4;
    
             break;
    
         case ("B"):
    
         case ("b"):
    
             row = 5; column = 5;
    
             break;
    
         case ("C"):
    
         case ("c"):
    
             while (row == 0)
    
             {
    
                 System.Console.WriteLine("请输入棋盘行数(2~10):");
    
                 string strRow = Console.ReadLine();
    
                 switch (strRow)
    
                 {
    
                     case ("2"):
    
                     case ("3"):
    
                     case ("4"):
    
                     case ("5"):
    
                     case ("6"):
    
                     case ("7"):
    
                     case ("8"):
    
                     case ("9"):
    
                     case ("10"):
    
                         row = int.Parse(strRow);
    
                         break;
    
                     default:
    
                         System.Console.WriteLine("输入有误,请输入2到10的数字!!!");
    
                         break;
    
                 }
    
             }
    
             while (column == 0)
    
             {
    
                 System.Console.WriteLine("请输入棋盘列数(2~10):");
    
                 string strColumn = Console.ReadLine();
    
                 switch (strColumn)
    
                 {
    
                     case ("2"):
    
                     case ("3"):
    
                     case ("4"):
    
                     case ("5"):
    
                     case ("6"):
    
                     case ("7"):
    
                     case ("8"):
    
                     case ("9"):
    
                     case ("10"):
    
                         column = int.Parse(strColumn);
    
                         break;
    
                     default:
    
                         System.Console.WriteLine("输入有误,请输入2到10的数字!!!");
    
                         break;
    
                 }
    
             }
    
             break;
    
         case ("D"):
    
         case ("d"):
    
             row = random.Next(2, 11); column = random.Next(2, 11);
    
             break;
    
         default:
    
             System.Console.WriteLine("输入有误!!!");
    
             goto sign1;
    
     }
    
     System.Console.WriteLine("请按任意键开始游戏...");
    
     Console.ReadKey();
    
     Console.Clear();
    
     return new int[] { row, column };
    
     }
    
  
    
     static void Main()
    
     {
    
     Console.Title = "合成2048";
    
  
    
     System.Console.WriteLine("游戏说明:");
    
     System.Console.WriteLine("1、按方向键(↑、↓、←、→)对棋盘中的数字进行操作,\r\n使相同数字相加,尽可能合成更大的数字。");
    
     System.Console.WriteLine("2、棋盘大小可选择游戏提供的尺寸或系统随机尺寸或自定义尺寸,\r\n自定义尺寸的行数和列数最小为2,最大为10。");
    
     System.Console.WriteLine("请按任意键继续...");
    
     Console.ReadKey();
    
     Console.Clear();
    
  
    
     while (true)
    
     {
    
         int[] rowAndColumn = GameSet(row, column);
    
         row = rowAndColumn[0];
    
         column = rowAndColumn[1];
    
  
    
         int generatedNum = 1;
    
         if (column * row >= 25) generatedNum = 2;
    
  
    
         if (column * row < 16) randomNum = 1;
    
  
    
         bool flag = true;
    
         int a, b, maxinum = 2;
    
         int[,] array = new int[row, column];
    
         while (flag)
    
         {
    
  
    
             for (int i = 0; i < generatedNum; i++)
    
             {
    
                 if (!IsBlank(array)) break;
    
                 sign2: a = random.Next(0, array.GetLength(0));
    
                 b = random.Next(0, array.GetLength(1));
    
                 if (array[a, b] == 0)
    
                     array[a, b] = GetNumber(maxinum);
    
                 else goto sign2;
    
             }
    
  
    
             PrintArray(array);
    
  
    
             if (!IsBlank(array))
    
             {
    
                 if (!IsSameAdjacentElementInArray(array))
    
                     break;
    
             }
    
  
    
             int[,] oldArray = (int[,])array.Clone();
    
  
    
  
    
             do
    
             {
    
                 ConsoleKey key1 = Console.ReadKey(true).Key;
    
                 if (key1 == ConsoleKey.Escape) {flag = false; break;}
    
                 switch (key1)
    
                 {
    
                     case (ConsoleKey.RightArrow):
    
                         array = ArrayCalculateRight(array);
    
                         break;
    
                     case (ConsoleKey.LeftArrow):
    
                         array = ArrayCalculateLeft(array);
    
                         break;
    
                     case (ConsoleKey.DownArrow):
    
                         array = ArrayCalculateDown(array);
    
                         break;
    
                     case (ConsoleKey.UpArrow):
    
                         array = ArrayCalculateUp(array);
    
                         break;
    
                 }
    
             } while (IsSameToTwoArray(array, oldArray));
    
             maxinum = GetMaximumInArray(array);
    
             Console.Clear();
    
         }
    
         row = column = 0;
    
         System.Console.WriteLine("你合成的最大数为{0},获得的总分数为{1}分。", GetMaximumInArray(array), SumByArray(array));
    
         System.Console.WriteLine("请按【ESC】键退出游戏或按其他键继续游戏...");
    
         ConsoleKey key2 = Console.ReadKey(true).Key;
    
         if (key2 == ConsoleKey.Escape) break;
    
     }
    
  
    
     }
    
 }

全部评论 (0)

还没有任何评论哟~