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

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <conio.h>
  6. using namespace std;
  7.  
  8. int map[5][5];
  9.  
  10. int score;
  11.  
  12. int move(int& a, int& b, int& c, int& d) //move nonzero digit to front
  13. {
  14. int vis_num = 0; //the number of visited digit, most is 4
  15. int zero_num = 0; //the number of zero digit
  16. while (1) {
  17. if (a != 0) {
  18. if (++vis_num >= 4) return zero_num;
  19. break;
  20. }
  21. ++zero_num;
  22. a = b;
  23. b = c;
  24. c = d;
  25. d = 0;
  26. if (++vis_num >= 4) return zero_num;
  27. }
  28. while (1) {
  29. if (b != 0) {
  30. if (++vis_num >= 4) return zero_num;
  31. break;
  32. }
  33. ++zero_num;
  34. b = c;
  35. c = d;
  36. d = 0;
  37. if (++vis_num >= 4) return zero_num;
  38. }
  39. while (1) {
  40. if (c != 0) {
  41. ++vis_num;
  42. if (vis_num >= 4) return zero_num;
  43. break;
  44. }
  45. ++zero_num;
  46. c = d;
  47. d = 0;
  48. if (++vis_num >= 4) return zero_num;
  49. }
  50. if (d == 0)
  51. ++zero_num;
  52. return zero_num;
  53. }
  54.  
  55. void change(int& a, int& b, int& c, int& d)
  56. {
  57. int zero_num = move(a, b, c, d);
  58. if (zero_num == 4 || zero_num == 3) return ;
  59. if (zero_num == 2) {
  60. if (a == b) {
  61. score += a * 10;
  62. a <<= 1;
  63. b = 0;
  64. }
  65. return ;
  66. }
  67. if (zero_num == 1) {
  68. if (a == b) {
  69. score += a * 10;
  70. a <<= 1;
  71. b = c;
  72. c = 0;
  73. } else if (c == b) {
  74. score += c * 10;
  75. b <<= 1;
  76. c = 0;
  77. }
  78. return ;
  79. }
  80. if (a == b) {
  81. if (c == d) {
  82. score += c * 10;
  83. score += a * 10;
  84. a <<= 1;
  85. b = c << 1;
  86. c = d = 0;
  87. } else {
  88. score += a * 10;
  89. a <<= 1;
  90. b = c;
  91. c = d;
  92. d = 0;
  93. }
  94. } else if (b == c){
  95. score += b * 10;
  96. b <<= 1;
  97. c = d;
  98. d = 0;
  99. } else if (c == d) {
  100. score += c * 10;
  101. c <<= 1;
  102. d = 0;
  103. }
  104. }
  105.  
  106. int rand_2_or_4()
  107. {
  108. if (rand() & 1) return 2;
  109. return 4;
  110. }
  111.  
  112. void rand_pos()
  113. {
  114. int x, y;
  115. do {
  116. x = rand() % 4;
  117. y = rand() % 4;
  118. } while (map[x][y]);
  119. map[x][y] = rand_2_or_4();
  120. }
  121.  
  122. void print()
  123. {
  124. int i, j;
  125. for (i = 0; i < 4; ++i) {
  126. for (j = 0; j < 4; ++j) {
  127. if (map[i][j] == 0) printf(" □ ");
  128. else printf("%4d ", map[i][j]);
  129. }
  130. printf("\n");
  131. }
  132. printf("score:%d\n", score);
  133. }
  134.  
  135. bool is_full()
  136. {
  137. int i, j;
  138. for (i = 0; i < 4; ++i)
  139. for (j = 0; j < 4; ++j)
  140. if (map[i][j] == 0) return false;
  141. return true;
  142. }
  143.  
  144. bool is_over_row(int a, int b, int c, int d)
  145. {
  146. if (a != b && b != c && c != d) return true;
  147. return false;
  148. }
  149.  
  150. bool is_over()
  151. {
  152. if (!is_full()) return false;
  153. int i;
  154. for (i = 0; i < 4; ++i) {
  155. if (!is_over_row(map[i][0], map[i][1], map[i][2], map[i][3]))
  156. return false;
  157. }
  158. for (i = 0; i < 4; ++i) {
  159. if (!is_over_row(map[0][i], map[1][i], map[2][i], map[3][i]))
  160. return false;
  161. }
  162. return true;
  163. }
  164.  
  165. int main()
  166. {
  167. // int a,b,c,d;
  168. // while(scanf("%d%d%d%d", &a, &b, &c, &d) != EOF){
  169. // change(a,b,c,d);
  170. // printf("%d %d %d %d\n", a, b, c, d);
  171. // }
  172. srand(unsigned(time(NULL)));
  173. rand_pos();
  174. rand_pos();
  175. print();
  176. while (!is_over()) {
  177. char dir = getch();
  178. int i;
  179. if (dir == 'w') {
  180. for (i = 0; i < 4; ++i) {
  181. change(map[0][i], map[1][i], map[2][i], map[3][i]);
  182. }
  183. } else if (dir == 's') {
  184. for (i = 0; i < 4; ++i)
  185. change(map[3][i], map[2][i], map[1][i], map[0][i]);
  186. } else if (dir == 'a') {
  187. for (i = 0; i < 4; ++i)
  188. change(map[i][0], map[i][1], map[i][2], map[i][3]);
  189. } else if (dir == 'd') {
  190. for (i = 0; i < 4; ++i)
  191. change(map[i][3], map[i][2], map[i][1], map[i][0]);
  192. } else continue;
  193. if (!is_full()) rand_pos();
  194. system("cls");
  195. print();
  196. }
  197. printf("Lose!");
  198. return 0;
  199. }

  

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

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之vim编辑器

    Vi简介1. Vi是一种广泛存在于各种UNIX和Linux系统中的文本编辑程序.2. Vi不是排版程序,只是一个纯粹的文本编辑程序.3. Vi是全屏幕文本编辑器,它没有菜单,只有命令.4. Vi不是基 ...

  2. 一步步学习ASP.NET MVC3 (6)——@helper,@functions

    请注明转载地址:http://www.cnblogs.com/arhat 在前一章中,我们讲述了View如何从Action中获得数据,并显示出来,但随着需求的变化,我们可能要对View中显示的数据作出 ...

  3. vue-cli + webpack

    vue-cli + webpack 关于vue.js vue.js是一套构建用户界面的 轻型的渐进式前端框架.它的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.使用vue可以给你 ...

  4. WIN32api总结

    1.鼠标操作: win32api.SetCursorPos((101,156)) win32api.mouse_event(win32con.MOUSEEVENT_LEFTDOWN,0,0,0,0) ...

  5. How Does #DeepDream Work?

    How Does #DeepDream Work? Do neural networks hallucinate of electronic dogs? If you’ve been browsing ...

  6. 《php和mysql web开发》读书笔记

    总算是强迫自己把第一篇给看完了,在这里做一个小结,将一些知识点记录下来. 一.第一篇 使用PHP 1.php中的注释.php支持c.c++和shell脚本风格注释 /**/多行注释  //单行注释   ...

  7. android 案例:从另一个activity选择信息并获取返回值

    主窗口: package com.example.test; import android.app.Activity; import android.app.AlertDialog; import a ...

  8. jsp空页面导致的jvm heap溢出

    为了性能测试需要,写一个了简单的jsp页面: <%@ page contentType="text/html;charset=UTF-8" language="ja ...

  9. Spring学习笔记(二)

    1.Spring MVC 返回json数据 <bean class="org.springframework.web.servlet.mvc.annotation.Annotation ...

  10. Document字段发生变化后,报的错

    2016-10-11 15:27:47,828 [ERROR] [main] SpringApplication:838 - Application startup failedorg.springf ...