花了两个多小时,用最蠢的方法写的……最简陋版……

还不确定这么写逻辑对不对……

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std; int map[5][5]; int score; int move(int& a, int& b, int& c, int& d) //move nonzero digit to front
{
int vis_num = 0; //the number of visited digit, most is 4
int zero_num = 0; //the number of zero digit
while (1) {
if (a != 0) {
if (++vis_num >= 4) return zero_num;
break;
}
++zero_num;
a = b;
b = c;
c = d;
d = 0;
if (++vis_num >= 4) return zero_num;
}
while (1) {
if (b != 0) {
if (++vis_num >= 4) return zero_num;
break;
}
++zero_num;
b = c;
c = d;
d = 0;
if (++vis_num >= 4) return zero_num;
}
while (1) {
if (c != 0) {
++vis_num;
if (vis_num >= 4) return zero_num;
break;
}
++zero_num;
c = d;
d = 0;
if (++vis_num >= 4) return zero_num;
}
if (d == 0)
++zero_num;
return zero_num;
} void change(int& a, int& b, int& c, int& d)
{
int zero_num = move(a, b, c, d);
if (zero_num == 4 || zero_num == 3) return ;
if (zero_num == 2) {
if (a == b) {
score += a * 10;
a <<= 1;
b = 0;
}
return ;
}
if (zero_num == 1) {
if (a == b) {
score += a * 10;
a <<= 1;
b = c;
c = 0;
} else if (c == b) {
score += c * 10;
b <<= 1;
c = 0;
}
return ;
}
if (a == b) {
if (c == d) {
score += c * 10;
score += a * 10;
a <<= 1;
b = c << 1;
c = d = 0;
} else {
score += a * 10;
a <<= 1;
b = c;
c = d;
d = 0;
}
} else if (b == c){
score += b * 10;
b <<= 1;
c = d;
d = 0;
} else if (c == d) {
score += c * 10;
c <<= 1;
d = 0;
}
} int rand_2_or_4()
{
if (rand() & 1) return 2;
return 4;
} void rand_pos()
{
int x, y;
do {
x = rand() % 4;
y = rand() % 4;
} while (map[x][y]);
map[x][y] = rand_2_or_4();
} void print()
{
int i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
if (map[i][j] == 0) printf(" □ ");
else printf("%4d ", map[i][j]);
}
printf("\n");
}
printf("score:%d\n", score);
} bool is_full()
{
int i, j;
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
if (map[i][j] == 0) return false;
return true;
} bool is_over_row(int a, int b, int c, int d)
{
if (a != b && b != c && c != d) return true;
return false;
} bool is_over()
{
if (!is_full()) return false;
int i;
for (i = 0; i < 4; ++i) {
if (!is_over_row(map[i][0], map[i][1], map[i][2], map[i][3]))
return false;
}
for (i = 0; i < 4; ++i) {
if (!is_over_row(map[0][i], map[1][i], map[2][i], map[3][i]))
return false;
}
return true;
} int main()
{
// int a,b,c,d;
// while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF){
// change(a,b,c,d);
// printf("%d %d %d %d\n", a, b, c, d);
// }
srand(unsigned(time(NULL)));
rand_pos();
rand_pos();
print();
while (!is_over()) {
char dir = getch();
int i;
if (dir == 'w') {
for (i = 0; i < 4; ++i) {
change(map[0][i], map[1][i], map[2][i], map[3][i]);
}
} else if (dir == 's') {
for (i = 0; i < 4; ++i)
change(map[3][i], map[2][i], map[1][i], map[0][i]);
} else if (dir == 'a') {
for (i = 0; i < 4; ++i)
change(map[i][0], map[i][1], map[i][2], map[i][3]);
} else if (dir == 'd') {
for (i = 0; i < 4; ++i)
change(map[i][3], map[i][2], map[i][1], map[i][0]);
} else continue;
if (!is_full()) rand_pos();
system("cls");
print();
}
printf("Lose!");
return 0;
}

  

后来发现我的写法有问题,因为如果不移动的话,就不会产生新的数字,而我的逻辑是只要有空位就有新的数字。之前没有认真观察过,懒得改了(其实是没想到什么好的方法……

c语言 字符版 简易2048的更多相关文章

  1. C#版简易RSS阅读器

    C#版简易RSS阅读器.由VB版修改完成,感谢aowind的技术支持! 源代码: using System; using System.Drawing; using System.Collection ...

  2. C语言字符串匹配函数

    C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...

  3. C语言字符知识狭区

    C语言字符在用户接口软件编程上经常用到,但是有一些狭区会让编程出现一些小BUG,现在总结与此. 1.'\\' 代表的是字符\,而'\'是不能代表字符\的.通常\后面都要跟上数字或者其他字母来表示一个特 ...

  4. C语言 字符数组与字符指针比较

    C语言 字符数组与字符指针比较 #include<stdio.h> /* 字符数组会在定以后预先分配内存空间字符串是常量所以会直接把字符串拷贝到数组中, 因为数组地址不同,所以不相等· 字 ...

  5. C语言-字符类型

    C语言-字符类型 char不仅是一种整数,也是一种特殊的类型:字符(character). 常用单引号表示字符的字面量,如'a', '1'. 单引号''也是一个字符,printf和scanf里用的%c ...

  6. C语言字符数组超细讲解

    看到标题,有不少朋友会想:字符数组不也是数组吗?为什么要单独拿出来讲哩?莫非它是朵奇葩? 哈哈,确实,一起来认识一下这朵数组界的奇葩吧! 一.字符数组的定义.引用.初始化 大家好!我是字符数组,看我的 ...

  7. UnifyRemoteManager-多国语言绿色版v1.3-20200315,统一远程连接自动登录软件,欢迎测试

    UnifyRemoteManager-多国语言绿色版v1.3-20200315,统一远程连接自动登录软件,欢迎测试 下载参考: 百度网盘:https://pan.baidu.com/s/15g-oXT ...

  8. Vnc自动登录器-多国语言绿色版

    推荐:介绍一个VNC连接工具:iis7服务器管理工具.IIs7服务器管理工具可以批量连接并管理VNC服务器.作为服务器集成管理器,它最优秀的功能就是批量管理windows与linux系统服务器.vps ...

  9. C语言实现简易2048小游戏

    一直很喜欢玩这个小游戏,简单的游戏中包含运气与思考与策略,喜欢这种简约又不失内涵的游戏风格.于是萌生了用C语言实现一下的想法. 具体代码是模仿这个:https://www.cnblogs.com/ju ...

随机推荐

  1. linux下修改IP信息

    在Linux的系统下如何才能修改IP信息 以前总是用ifconfig修改,重启后总是得重做.如果修改配置文件,就不用那么麻烦了- A.修改ip地址 即时生效: # ifconfig eth0 192. ...

  2. CATransition的动画效果类型及实现方法--老代码备用参考

    实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一种是使用CATransition进行更低层次的控制, 第一种是UIView,UIView方式可能在低层也是使用CATransi ...

  3. C#.net winform 控件和皮肤大全

    1. 东日IrisSkin IrisSkin 共有两个版本,一个是IrisSkin.dll 用于.Net Framework1.0/1.1 和IrisSkin2.dll 用于.Net Framewor ...

  4. VS Extension: Open Web Address with Visual Studio Browser

    使用VS 打开链接 using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; ... public ...

  5. Maven仓库详解

    转载自:Maven入门指南④:仓库   1 . 仓库简介 没有 Maven 时,项目用到的 .jar 文件通常需要拷贝到 /lib 目录,项目多了,拷贝的文件副本就多了,占用磁盘空间,且难于管理.Ma ...

  6. java连接数据库时出现如下错误:

    java.lang.ClassNotFoundException: com.mysql.jdbc.driver at org.apache.catalina.loader.WebappClassLoa ...

  7. Linux按照时间查找文件

    linux按照时间查找文件 需要用到一个根据最后修改时间来处理的脚本. 前面有个有关find的基本用法,根据文件大小,类型什么的,这个是关于时间的.  linux 文件的三种时间(以 find 为例) ...

  8. 国内静态文件CDN服务介绍 国内js公共库

    国内静态文件CDN服务介绍 新浪SAE  介绍页 文件页 百度云 介绍页 七牛云存储介绍页 优势,可以提交没有的库,支持https,但证书不可信. 又拍云 介绍页 建议使用阿里云OSS自己上传所需文件 ...

  9. Unity3D热更新

    原地址:http://crazylights.cnblogs.com/ 下载在这个时代实在是太平常了,每个人都深刻的理解着下载到底是什么. 这一篇文字只是把下载的代码分享并介绍,而已. 首先,下载系统 ...

  10. FZU 2140 Forever 0.5

     Problem 2140 Forever 0.5 Accept: 36    Submit: 113    Special JudgeTime Limit: 1000 mSec    Memory ...