2048 控制台版(C#)
2048游戏现在很火啊,很多人应该已经玩过了。在博客园上也看见有人模仿做的GDI+版 2048游戏,鄙人暂且不做那么多动画的东西,毕竟是个小东东,在此奉上一个《控制台版2048》。
本文程序源码下载:http://pan.baidu.com/s/1mg8zntu
Main 方法程序入口,RePaint 类似Win32程序中的刷新界面,SquareRot90 矩形矩阵顺时针旋转90度角(参数可以为负,表示逆时针)。Merge 向左移动,合并单元格中值相同的元素,并进行整理(将所有元素靠左放置)RandomPoint 随机生成点,从矩阵的空位置(值为0)中随机生成一个点,若不存在空位置返回null。CanMove 判断是否可以继续移动,也是判断游戏是否结束的方法。IsEquals 判断两矩阵的值是否相同。CopyToB 将矩阵复制一份。


static void Main(string[] args)
{
int[,] a = new int[4, 4];
a[1, 2] = 2;
a[2, 2] = 2;
a[2, 1] = 2;
RePaint(a);
while (true)
{
ConsoleKeyInfo key = Console.ReadKey();
switch (key.Key)
{
case ConsoleKey.UpArrow:
a = SquareRot90(a, 3);
a = Merge(a);
a = SquareRot90(a, -3);
break;
case ConsoleKey.DownArrow:
a = SquareRot90(a, 1);
a = Merge(a);
a = SquareRot90(a, -1);
break;
case ConsoleKey.LeftArrow:
a = Merge(a);
break;
case ConsoleKey.RightArrow:
a = SquareRot90(a, 2);
a = Merge(a);
a = SquareRot90(a, -2);
break;
}
Point cp = RandomPoint(a);
if (cp != null)
{
a[cp.X, cp.Y] = 2;
RePaint(a);
}
if (cp == null && !CanMove(a))
{
RePaint(a, "Game Over");
}
}
}
/// 矩形顺时针旋转90°
/// </summary>
/// <param name="rotNum">旋转次数</param>
public static int[,] SquareRot90(int[,] a, int rotNum)
{
while (rotNum < 0)
{
rotNum += 4;
}
for (int rot_i = 0; rot_i < rotNum; rot_i++)
{
int[,] b = new int[a.GetLength(1), a.GetLength(0)];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
b[j, a.GetLength(0) - i - 1] = a[i, j];
}
}
a = b;
}
return a;
}
public static Point RandomPoint(int[,] a)
{
List<Point> lstP = new List<Point>();
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (a[i, j] == 0)
{
lstP.Add(new Point(i, j));
}
}
}
if (lstP.Count == 0)
{
return null;
}
int rnd = new Random().Next(lstP.Count);
return lstP[rnd];
}
public static int[,] Merge(int[,] a)
{
for (int i = 0; i < a.GetLength(0); i++)
{
int lastNum = 0;
int last_j = 0;
for (int j = 0; j < a.GetLength(1); j++)//合并
{
if (lastNum != a[i, j] && a[i, j] != 0)
{
lastNum = a[i, j];
last_j = j;
}
else if (lastNum == a[i, j])
{
a[i, last_j] = 0;
a[i, j] = lastNum + a[i, j];
}
}
last_j = 0;
for (int j = 0; j < a.GetLength(1); j++)//整理
{
if (a[i, j] != 0)
{
a[i, last_j] = a[i, j];
if (last_j != j)
a[i, j] = 0;
last_j++;
}
}
}
return a;
}
public static bool CanMove(int[,] a)
{
bool res = false;
int[,] b = CopyToB(a);
b = Merge(b);
if (!IsEquals(a, b))
res = true;
b = CopyToB(a);
b = SquareRot90(b, 1);
b = Merge(b);
b = SquareRot90(b, -1);
if (!IsEquals(a, b))
res = true;
b = CopyToB(a);
b = SquareRot90(b, 2);
b = Merge(b);
b = SquareRot90(b, -2);
if (!IsEquals(a, b))
res = true;
b = CopyToB(a);
b = SquareRot90(b, 3);
b = Merge(b);
b = SquareRot90(b, -3);
if (!IsEquals(a, b))
res = true;
return res;
}
public static bool IsEquals(int[,] a, int[,] b)
{
bool res = true;
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (b[i, j] != a[i, j])
{
res = false;
break;
}
}
if (!res)
break;
}
return res;
}
public static int[,] CopyToB(int[,] a)
{
int[,] b = new int[a.GetLength(0), a.GetLength(1)];
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
b[i, j] = a[i, j];
}
}
return b;
}
public static void RePaint(int[,] a, string s)
{
while (true)
{
Console.Clear();
RePaint(a);
Console.WriteLine("\n\n\n\n\t\t" + s + "\n\n");
Console.ReadKey();
}
}
public static void RePaint(int[,] a)
{
Console.Clear();
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("───");
}
Console.Write("\n");
for (int i = 0; i < a.GetLength(0); i++)
{
Console.Write("│");
for (int j = 0; j < a.GetLength(1); j++)
{
string s = "";
if (a[i, j] == 0)
s = " ";
else if (a[i, j] < 10)
s = " " + a[i, j] + " ";
else if (a[i, j] < 100)
s = "" + a[i, j] + " ";
else
s = "" + a[i, j];
Console.Write(s + "│");
}
Console.Write("\n");
for (int j = 0; j < a.GetLength(1); j++)
{
Console.Write("───");
}
Console.Write("\n");
}
}
class Point
{
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
public int X
{
get;
set;
}
public int Y
{
get;
set;
}
}
2048 控制台版(C#)的更多相关文章
- c/c++ 贪吃蛇控制台版
贪吃蛇控制台版(操作系统win7 64位:编译环境gcc, vs2017通过,其它环境未测试 不保证一定通过) 运行效果: #include <iomanip> #include < ...
- 基于TCP协议的聊天室控制台版
我之前写过一篇博客,主要是基于TCP协议实现的聊天室swing版,在此再写一个基于TCP协议实现的聊天室控制台版,便于学习和比较. package 聊天室console版.utils; import ...
- c++实现通讯录管理系统(控制台版)
c++实现通讯录管理系统(控制台版) 此项目适合c++初学者,针对c++基础知识,涉及到变量.结构体定义使用.数组定义使用.指针定义使用等. 运行之后的结果如下: 代码: #include <i ...
- C++实现控制台版2048
前言 之前做过一个JavaScript版本的2048游戏,最近在学习C++,昨天晚上突然心血来潮,想用C++来实现,因为核心算法已十分理解,所以两个小时撸出来一个C++的简易版本. 简介 二维数组遍历 ...
- c#撸的控制台版2048小游戏
1.分析 最近心血来潮,突然想写一个2048小游戏.于是搜索了一个在线2048玩玩,熟悉熟悉规则. 只谈核心规则:(以左移为例) 1.1合并 以行为单位,忽略0位,每列依次向左进行合并,且每列只能合并 ...
- Delphi_01_控制台版HelloWorld
对于Windows下的控制台编程,我相信很多人都不陌生.而C语言开始的著名的“Hello world”程序基本是学习编程的第一步.我想对于 RAD开发,大家熟悉的一般都是GUI编程,而对于consol ...
- [C++项目]2048控制台游戏
#include <iostream> #include <windows.h> #include <ctime> using namespace std; ; ; ...
- Java小例子(学习整理)-----学生管理系统-控制台版
1.功能介绍: 首先,这个小案例没有使用数据库,用集合的形式暂时保存数据,做测试! 功能: 增加学生信息 删除学生信息 修改学生信息 查询学生信息: 按照学号(精确查询) 按照姓名(模糊查询) 打 ...
- 2048聚合版开源代码,cocos2d-js编写,基于CocosEditor开发工具,可运行Android,ios,html5等
1. [代码][JavaScript]代码 /** * @GameName : * 2048 * * @DevelopTool: * Cocos2d-x Editor (CocosEd ...
随机推荐
- 安装Cloudera Impala
安装Cloudera Impala Cloudera Impala是Cloudera Enterprise Core的开源扩展,用于快速返回查询结果. Impala作为你环境的插件,与其他组件的安装独 ...
- 升级vue全家桶过程记录
背景 如果你使用了element-ui的el-tabs组件,并且想要单独升级element-ui至2.10.0,你会发现,使用了el-tabs组件的页面只要打开就卡死.原因是element-ui~2. ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- 纯异步nodejs文件夹(目录)复制
思路: 1.callback 驱动 2.递归所有需要复制文件 3.在一定阀值下并发复制文件 4.运行需要安装 async.js npm install async 代码如下: var asyn ...
- php+mysql新无线级分类
create table cat( id int unsigned not null primary key auto_increment, pid , path ) not null default ...
- JavaScript面向对象之对象的声明、遍历和存储
一.对象的声明方式 1. 字面式(json格式)声明对象 var obj={ 属性名:属性值, 方法名:function(){ //函数执行体 } } 2. new 操作符+Object 声明对象 v ...
- white box白盒测试
逻辑覆盖法:语句覆盖,判定覆盖,条件覆盖,判定/条件覆盖,组合覆盖,路径覆盖 基本路径测试法:Control Flow Graphs, CFG.带箭头的边 条件覆盖:使每个判定中每个条件的可能值至少满 ...
- CDH 5.15.2 离线安装
一.前置准备 1. 基础信息 1.1 机器 机器名 服务 hadoop1 主节点 hadoop2 data.task hadoop3 data.task 1.2 服务版本 服务 版本 cdh 5.15 ...
- 那些有实力进入 BAT 的本科生,都做对了什么事?
作者:黄小斜 文章来源:微信公众号[黄小斜] 最近这段时间,我们部门来了几个年纪轻轻的本科生,最小的比我们小五岁左,这对于我来说还是比较有冲击力的. 想想我也是九0出头的老腊肉了,想当年我上大学的时候 ...
- 什么是JDK什么是JRE?JDK和JRE的关系
什么是JDK什么是JRE?JDK和JRE的关系 我们看看来自百度百科的解释: JDK是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心 ...