一.Program.cs
1.在主方法中实例化核心控制类GameCore.cs;
GameCore core = new GameCore();
2.调用类方法初始化一个二维数组,在控制台显示输出;
core.InitalArray(); core.Print();//输出
3.获取用户输入,case不同的合并方向;
4.找到空位置,并在随机位置补充新的数字;
5.显示输出。
while (true) { //获取用户输入 var keyInfo = Console.ReadKey(true); //执行 switch (keyInfo.Key) { case ConsoleKey.UpArrow: core.UpArrow(); break; case ConsoleKey.DownArrow: core.DownArrow(); break; case ConsoleKey.LeftArrow: core.LeftArrow(); break; case ConsoleKey.RightArrow: core.RightArrow(); break; } if (core.FindEmpty()==0) { Environment.Exit(0); Console.WriteLine("游戏结束"); }//找空位置 core.CreateNewNumber();//生成新数字 //显示 core.Print(); }
二.GameCore.cs
1.定义位置结构体,包括行坐标和列坐标,用于记录查找到的空位置;
//定义位置结构体 struct Pos { public int r; public int c; public Pos(int r, int c) { this.r = r; this.c = c; } }
2.定义二维数组,并赋初值为0(0即为空);
//初始化二维数组 int[,] Matrix = { {0,0,0,0 }, {0,0,0,0 }, {0,0,0,0 }, {0,0,0,0} };3.定义一个变量用于记录查找到的空位置的个数,以及一个结构体数组记录为空的所有位置;
int emptySum = 0;//空位置的个数 Pos[] empty = new Pos[20];//空位置数组
4.声明随机数生成器,random函数返回一个0~num-1之间的随机数,用于在空位置产生数字;
Random ran = new Random();//随机数生成器
5.向右清除0元素,除0以外的所有数字统统右靠齐;
public void RemoveZeroRight() { int[,] arr = new int[Matrix.GetLength(0), Matrix.GetLength(1)];//创建一个新数组 for(int r = 0; r < Matrix.GetLength(0); r++) { int index = Matrix.GetLength(1) - 1;//列索引初始化为最大 for(int c = Matrix.GetLength(1) - 1; c >= 0; c--) { if (Matrix[r, c] != 0) { arr[r, index--] = Matrix[r, c]; } } } Matrix = arr;//替换Matrix为新数组 }
6.向右合并,从最右列开始,两数相同,合二为一;
public void MergeRight() { for(int r = 0; r < Matrix.GetLength(0); r++) { for(int c = Matrix.GetLength(1) - 1; c >= 1; c--) { if (Matrix[r, c] == Matrix[r, c - 1]) { Matrix[r, c] *= 2;//当前位置的数字乘2 Matrix[r, c - 1] = 0;//把前面的数字变成0 } } } }
7.顺时针旋转90度;
public void Rotate90() { int[,] arr = new int[Matrix.GetLength(1), Matrix.GetLength(0)];//新建数组 int cc = 0;//老数组从最小的列开始 for (int r = 0; r < arr.GetLength(0); r++) { int rr = Matrix.GetLength(0) - 1;//老数组从最大的行开始 for (int c = 0; c < arr.GetLength(1); c++) { arr[r, c] = Matrix[rr--, cc]; } cc++; } Matrix = arr;//将新数组赋值给老数组 }
8.旋转到右朝向,处理;
public void Process() { //清零 RemoveZeroRight(); //合并 MergeRight(); //清零 RemoveZeroRight(); }
9.上下左右(我们只是代码的搬运工......);
//向上 public void UpArrow() { Rotate90();//旋转 Process();//处理 Rotate90();//旋转 Rotate90();//旋转 Rotate90();//旋转 } //向下 public void DownArrow() { Rotate90();//旋转 Rotate90();//旋转 Rotate90();//旋转 Process();//处理 Rotate90();//旋转 } //向左 public void LeftArrow() { Rotate90();//旋转 Rotate90();//旋转 Process();//处理 Rotate90();//旋转 Rotate90();//旋转 } //向右 public void RightArrow() { Process();//处理 }
10.查找空位置;
public int FindEmpty() { int index = 0;//空位置数组索引 int flag = 0; //查找所有空位置放到空位置数组中 for(int r = 0; r < Matrix.GetLength(0); r++) { for(int c = 0; c < Matrix.GetLength(1); c++) { if (Matrix[r, c] == 0) { Pos pos = new Pos(r, c); empty[index++] = pos; emptySum = index + 1;//记录找了多少个空位置 flag = 1; } } } return flag; }
11.随机在某个空位置处生成数字2;
public void CreateNewNumber() { int index = ran.Next(0, emptySum);//产生一个随机索引 Pos pos = empty[index]; Matrix[pos.r, pos.c] = 2; }
12.利用上两个方法初始化数组;
//初始化随机数组,随机两个空位置并修改为2 public void InitalArray() { FindEmpty(); CreateNewNumber(); FindEmpty(); CreateNewNumber(); }
13.打印输出Print;
public void Print() { for(int r = 0; r < Matrix.GetLength(0); r++) { for(int c = 0; c < Matrix.GetLength(1); c++) { Console.Write(Matrix[r, c] + "\t"); } Console.WriteLine(); } Console.WriteLine("_______________________________________________"); }