久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2290|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

A*尋路測試用貪吃蛇c++程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:108615 發(fā)表于 2016-3-13 00:18 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. #include "stdio.h"
  2. #include "stdlib.h"

  3. struct she {
  4. // 貪吃蛇節(jié)點
  5. int fx; // 貪吃蛇當前節(jié)點的運動方向
  6. struct she *next; // 下一個節(jié)點
  7. };

  8. class tanchishe {
  9. // 貪吃蛇類
  10. public:
  11. tanchishe(); // 構(gòu)造函數(shù)創(chuàng)建并初始化貪吃蛇
  12. void chi(int fx); // 吃并按方向增加一節(jié)長度
  13. void U(); // 向上運動一步
  14. void D(); // 向下運動一步
  15. void L(); // 向左運動一步
  16. void R(); // 向右運動一步

  17. void print(); // 打印函數(shù)
  18. // private:
  19. struct she *tou = NULL; // 蛇頭
  20. };

  21. tanchishe::tanchishe() {
  22. // 構(gòu)造函數(shù)
  23. tou = (she *) malloc(sizeof(she));
  24. if (tou == NULL) {
  25. printf("貪吃蛇創(chuàng)建失敗!");
  26. }
  27. tou->next = NULL;
  28. tou->fx = 4; // 1—上 2—下 3—左
  29. // 4—右
  30. // 頭節(jié)點初始化為4
  31. }

  32. void tanchishe::chi(int fx) {
  33. // 吃一節(jié)
  34. struct she *node = (she *) malloc(sizeof(she));
  35. if (node == NULL) {
  36. printf("添加新節(jié)失敗!");
  37. }
  38. node->fx = fx;
  39. node->next = tou;
  40. tou = node;
  41. }

  42. void tanchishe::U() {
  43. // 向上運動一步
  44. struct she *p;
  45. struct she *q;
  46. int a, b;
  47. p = tou;
  48. q = tou->next;
  49. a = p->fx;
  50. while (q != NULL) {
  51. b = q->fx;
  52. q->fx = a;
  53. a = b;
  54. p = p->next;
  55. q = q->next;
  56. }
  57. tou->fx=1;

  58. }

  59. void tanchishe::D() {
  60. // 向下運動一步
  61. struct she *p;
  62. struct she *q;
  63. int a, b;
  64. p = tou;
  65. q = tou->next;
  66. a = p->fx;
  67. while (q != NULL) {
  68. b = q->fx;
  69. q->fx = a;
  70. a = b;
  71. p = p->next;
  72. q = q->next;
  73. }
  74. tou->fx=2;
  75. }

  76. void tanchishe::L() {
  77. // 向左運動一步
  78. struct she *p;
  79. struct she *q;
  80. int a, b;
  81. p = tou;
  82. q = tou->next;
  83. a = p->fx;
  84. while (q != NULL) {
  85. b = q->fx;
  86. q->fx = a;
  87. a = b;
  88. p = p->next;
  89. q = q->next;
  90. }
  91. tou->fx=3;
  92. }

  93. void tanchishe::R() {
  94. // 向右運動一步
  95. struct she *p;
  96. struct she *q;
  97. int a, b;
  98. p = tou;
  99. q = tou->next;
  100. a = p->fx;
  101. while (q != NULL) {
  102. b = q->fx;
  103. q->fx = a;
  104. a = b;
  105. p = p->next;
  106. q = q->next;
  107. }
  108. tou->fx=4;
  109. }

  110. void tanchishe::print() {
  111. // 打印函數(shù)
  112. }
  113. #include "tanchishe.h"
  114. #include<conio.h>.
  115. #include<time.h>

  116. class ditu {
  117. public:
  118. ditu(); // 初始化
  119. void shiwu(); // 隨機生成一個食物
  120. void print(); // 打印地圖
  121. void U(); // 向上一步
  122. void D(); // 向下一步
  123. void L(); // 向左一步
  124. void R(); // 向右一步
  125. // private:
  126. int she_X, she_Y; // 蛇在地圖上的坐標
  127. int shiwu_X, shiwu_Y; // 食物在地圖上的坐標
  128. int dt[20][20]; // 地圖數(shù)組
  129. tanchishe she; // 貪吃蛇對象
  130. };

  131. ditu::ditu() {
  132. she_X = 0;
  133. she_Y = 1;
  134. shiwu();
  135. }

  136. void ditu::shiwu() {
  137. srand(time(0));
  138. int B = 1;
  139. while (B) {
  140. shiwu_Y = rand() % 18 + 1;
  141. shiwu_X = rand() % 18 + 1;
  142. if (dt[shiwu_Y][shiwu_X] == 0)
  143. B = 0;
  144. }
  145. dt[shiwu_Y][shiwu_X] = 6;
  146. }

  147. void ditu::U() {
  148. if (dt[she_Y - 1][she_X] == 0 && ((she_Y - 1) >= 0)) {
  149. she_Y--;
  150. she.U();
  151. } else if (dt[she_Y - 1][she_X] == 6 && ((she_Y - 1) >= 0)) {
  152. she_Y--;
  153. she.chi(1);
  154. shiwu();
  155. }
  156. }

  157. void ditu::D() {
  158. if (dt[she_Y + 1][she_X] == 0 && ((she_Y + 1) < 20)) {
  159. she_Y++;
  160. she.D();
  161. } else if (dt[she_Y + 1][she_X] == 6 && ((she_Y + 1) < 20)) {
  162. she_Y++;
  163. she.chi(2);
  164. shiwu();
  165. }
  166. }

  167. void ditu::L() {
  168. if (dt[she_Y][she_X - 1] == 0 && ((she_X - 1) >= 0)) {
  169. she_X--;
  170. she.L();
  171. } else if (dt[she_Y][she_X - 1] == 6 && ((she_X - 1) >= 0)) {
  172. she_X--;
  173. she.chi(3);
  174. shiwu();
  175. }
  176. }

  177. void ditu::R() {
  178. if (dt[she_Y][she_X + 1] == 0 && ((she_X + 1) < 20)) {
  179. she_X++;
  180. she.R();
  181. } else if (dt[she_Y][she_X + 1] == 6 && ((she_X + 1) < 20)) {
  182. she_X++;
  183. she.chi(4);
  184. shiwu();
  185. }
  186. }

  187. void ditu::print() {
  188. // 打印函數(shù)
  189. int x = she_X;
  190. int y = she_Y;

  191. for (int i = 0; i < 20; i++)
  192. for (int j = 0; j < 20; j++)
  193. dt[i][j] = 0;

  194. dt[shiwu_Y][shiwu_X] = 6;

  195. struct she *p = she.tou;
  196. dt[y][x] = 88; // 蛇頭
  197. if (p->fx == 1)
  198. y++;
  199. else if (p->fx == 2)
  200. y--;
  201. else if (p->fx == 3)
  202. x++;
  203. else if (p->fx == 4)
  204. x--;
  205. p = p->next;
  206. while (p != NULL) {
  207. dt[y][x] = 8; // 蛇身
  208. if (p->fx == 1)
  209. y++;
  210. else if (p->fx == 2)
  211. y--;
  212. else if (p->fx == 3)
  213. x++;
  214. else if (p->fx == 4)
  215. x--;
  216. p = p->next;
  217. }

  218. for (int i = 0; i < 20; i++) {
  219. for (int j = 0; j < 20; j++) {
  220. if (dt[i][j] == 8)
  221. printf("o");
  222. else if (dt[i][j] == 88)
  223. printf("@");
  224. else if (dt[i][j] == 6)
  225. printf("X");
  226. else
  227. printf(" ");
  228. }
  229. printf("\n");
  230. }

  231. }
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 亚洲国产一区视频 | 国产欧美久久精品 | 久久国产秒| 粉嫩高清一区二区三区 | 亚洲视频精品在线 | 午夜天堂精品久久久久 | 玖玖久久 | 一区二区三区国产 | 国产精品福利视频 | 亚洲免费视频一区 | 成人亚洲视频 | 欧美精品首页 | 日本五月婷婷 | 精品美女视频在免费观看 | 欧美日韩国产一区二区三区 | 国产视频一区二区 | 一级片免费视频 | 国产成人亚洲精品自产在线 | 亚洲欧美高清 | 日本午夜在线视频 | 美国一级黄色片 | 久久99这里只有精品 | 97国产在线视频 | 久久成人一区 | 国产在线1| 国产精品123区 | 免费观看www7722午夜电影 | 欧美性猛交一区二区三区精品 | 中文字幕国产 | 久久蜜桃资源一区二区老牛 | 在线观看www视频 | 一区二区高清 | 久久久久无码国产精品一区 | 九九在线精品视频 | 一区二区三区欧美 | 精品久久久久香蕉网 | 欧美区日韩区 | 日韩国产在线观看 | 天天干天天玩天天操 | 国产精品一区二区免费 | 操久久|