标签:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace SeqListSort { /// <summary> /// <ather> /// <lihonglin> /// </ather> /// <countent> /// 2048游戏规则: ///开始时棋盘内随机出现两个数字,出现的数字仅可能为2或4 ///玩家可以选择上下左右四个方向,若棋盘内的数字出现位移或合并,视为有效移动 ///玩家选择的方向上若有相同的数字则合并,每次有效移动可以同时合并,但不可以连续合并 ///合并所得的所有新生成数字相加即为该步的有效得分 ///玩家选择的方向行或列前方有空格则出现位移 ///每有效移动一步,棋盘的空位(无数字处)随机出现一个数字(依然可能为2或4) ///棋盘被数字填满,无法进行有效移动,判负,游戏结束 ///棋盘上出现2048,判胜,游戏结束 /// </countent> /// </summary> //public struct Point //{ // public int x; // public int y; // public Point(int _x, int _y) // { // this.x = _x; // this.y = _y; // } //} public enum DIR_TYPE { TOP, LEFT, RIGHT, BOTTOM } class Game2048 { const int n = 4; static int[,] map = new int[n,n];//定义棋盘 static int[] temp = new int[n+1];//一个临时数组,存放十六个位置数据 static int score = 0; static Stack<Point> fullCell = new Stack<Point>();//格子是否已满 public static void GameRandom() { Random r = new Random(); int x = 0; int y = 0; do { x = r.Next(0, n); y = r.Next(0, n); } while ( map[x,y] != 0); Thread.Sleep(10); if (r.Next(0, n + 1) % n == 0) { map[x,y] = 4; } else { map[x, y] = 2; } fullCell.Push(new Point(x, y)); } public static void GameInit() { GameRandom(); GameRandom(); Display(); } public static void Display() { Console.WriteLine("分数 = " + score); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { Console.Write("{0,-6}" , map[i, j]); } Console.WriteLine(); } GameOverOrWine(); } public static void GameContral() { DIR_TYPE type = DIR_TYPE.TOP; while (true) { Console.WriteLine("请按w、a、s、d键上下左右控制‘0‘键退出"); char ch = Convert.ToChar(Console.ReadLine()); if (ch == ‘a‘|| ch == ‘A‘ || ch == ‘W‘|| ch == ‘w‘ || ch == ‘0‘ || ch == ‘S‘|| ch == ‘s‘ ||ch == ‘D‘|| ch == ‘d‘ ) { Console.Clear(); } else { Console.WriteLine("按下非法键,请重新按!"); continue; } switch (ch) { case ‘0‘: { Console.WriteLine("是否退出游戏!(y/n)"); ch = Convert.ToChar(Console.ReadLine()); if (ch == ‘y‘ || ch == ‘Y‘) { Environment.Exit(0); } else { Console.Clear(); } } break; case ‘A‘: case ‘a‘: { type = DIR_TYPE.LEFT; } break; case ‘D‘: case ‘d‘: { type = DIR_TYPE.RIGHT; } break; case ‘W‘: case ‘w‘: { type = DIR_TYPE.TOP; } break; case ‘S‘: case ‘s‘: { type = DIR_TYPE.BOTTOM; } break; } GameRun(type); Display(); } } //玩家可以选择上下左右四个方向,若棋盘内的数字出现位移或合并,视为有效移动 public static int GameMove( ) { int change = 0; int i = 0; int j = 0; do //去掉中间的0 { for (i = 0; i < n; ++i ) { if (0 == temp[i])//玩家选择的方向行或列前方有空格则出现位移 { if (temp[i] != temp[i + 1])//不相等移动 { change = 1;//有效移动 } temp[i] = temp[i + 1]; temp[i + 1] = 0; } } j++; } while (j < n); for (i = 1; i < n; ++i ) { //玩家选择的方向上若有相同的数字则合并,每次有效移动可以同时合并,但不可以连续合并 if (temp[i] == temp[i - 1])//相同的数字则合并 { if (0 != temp[i]) { change = 1; score += temp[i]; } temp[i - 1] += temp[i - 1];//前一个数字乘自己 temp[i] = 0;// 后一个位置 置0 } } return change; } public static void GameOverOrWine() { int count = 0;//记录格子里不为0 的个数 if (score >= 4096 << 1 ) { Console.WriteLine("您胜利了 88!"); Thread.Sleep(500); Environment.Exit(0); } else { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if ( map[i,j] != 0 ) { count++; if ( count == n * n ) { Console.WriteLine("game over"); Thread.Sleep(3000); Environment.Exit(0); } } if ( map[i,j] >= 2048 ) { Console.WriteLine("您胜利了 88!"); Thread.Sleep(500); //Environment.Exit(0); } } } } } public static void GameRun(DIR_TYPE type) { int i = 0; int j = 0; int change = 0;/*判断格子中的数是否改变,0不变*/ if (type == DIR_TYPE.LEFT || type == DIR_TYPE.RIGHT) { for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { if (type == DIR_TYPE.LEFT) { temp[j] = map[i, j]; } else { temp[j] = map[i, 3 - j]; } } //temp[4] = 0; change += GameMove(); for (j = 0; j < n; ++j) { if (type == DIR_TYPE.LEFT) { map[i, j] = temp[j]; } else { map[i, 3 - j] = temp[j]; } } } } else { for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { if (type == DIR_TYPE.TOP) { temp[j] = map[j, i];//把每一列数据存进去 } else if (type == DIR_TYPE.BOTTOM) { temp[j] = map[3 - j, i]; } } temp[4] = 0; change += GameMove(); for (j = 0; j < n; ++j) { if (type == DIR_TYPE.TOP) { map[j, i] = temp[j]; //把处理好的中间变量移回来 } else if (type == DIR_TYPE.BOTTOM) { map[3 - j, i] = temp[j]; } } } } if (change > 0) { GameRandom(); } } } }
标签:
原文地址:http://www.cnblogs.com/lihonglin2016/p/4307874.html