当前位置:   article > 正文

控制台版扫雷程序_控制台启动扫雷代码

控制台启动扫雷代码

测试平台: WIN7

工具: VC6.0 , VS2008都能编译得过。

花了两天时间写的,里面涉及的算法大都是自己想的,所以可能有些BUG。

如果出现错误请提醒,鞠躬,谢谢!

  1. #include <iostream>
  2. #include <time.h>
  3. #include <windows.h>
  4. using namespace std;
  5. #pragma comment (linker,"/subsystem:console")
  6. #define BLACK 0 //空白
  7. #define MINE 100 //地雷
  8. #define NOSWEEP 0 //未扫过
  9. #define SWEEP 1 //扫雷
  10. #define FLAG 2 //标记
  11. class WinMine
  12. {
  13. public:
  14. WinMine(int iRow, int iColumn);
  15. ~WinMine();
  16. public:
  17. void InitMine(int iMineMax);
  18. void SetColor();
  19. void StatisticsMine();
  20. void Map();
  21. bool OpenWhites(int x, int y, int status);
  22. void GameStart(int iMineMax);
  23. void GameOver();
  24. bool GoodOver();
  25. private:
  26. int **m_ppMine;
  27. int **m_ppSweep;
  28. bool m_bMineflag;
  29. bool m_bSweepflag;
  30. int m_row;
  31. int m_column;
  32. int m_minenum;
  33. };
  34. int main(void)
  35. {
  36. system("color 0d");
  37. while (true)
  38. {
  39. int level;
  40. WinMine *Mine;
  41. cout << "请输入游戏等级:" <<endl;
  42. cout << "1.初级" <<endl;
  43. cout << "2.中级" <<endl;
  44. cout << "3.高级" <<endl;
  45. cout << "4.退出" <<endl;
  46. cin >> level;
  47. if (level == 1)
  48. {
  49. Mine = new WinMine(9,9);
  50. Mine->GameStart(10);
  51. }
  52. else if (level == 2)
  53. {
  54. Mine = new WinMine(16,16);
  55. Mine->GameStart(40);
  56. }
  57. else if (level == 3)
  58. {
  59. Mine = new WinMine(16,30);
  60. Mine->GameStart(70);
  61. }
  62. else if (level == 4)
  63. {
  64. return 0;
  65. }
  66. else
  67. {
  68. cout << "输入错误!" <<endl;
  69. continue;
  70. }
  71. delete Mine;
  72. }
  73. return 0;
  74. }
  75. WinMine::WinMine(int iRow, int iColumn)
  76. {
  77. int i;
  78. //储雷状态
  79. m_ppMine = (int **) new int[iRow]; if (!m_ppMine) return;
  80. m_ppMine[0] = new int[iRow * iColumn]; if (!*m_ppMine) { delete[] m_ppMine; m_ppMine = NULL; return;}
  81. m_bMineflag = true;
  82. //扫雷状态
  83. m_ppSweep = (int **) new int[iRow]; if (!m_ppSweep) return;
  84. m_ppSweep[0] = new int[iRow * iColumn]; if (!*m_ppSweep) { delete[] m_ppSweep; m_ppSweep = NULL; return;}
  85. m_bSweepflag = true;
  86. m_row = iRow; m_column = iColumn;
  87. for (i = 1; i < iRow; i++)
  88. {
  89. m_ppMine[i] = m_ppMine[0] + i * iRow;
  90. }
  91. for (i = 1; i < iRow; i++)
  92. {
  93. m_ppSweep[i] = m_ppSweep[0] + i * iRow;
  94. }
  95. memset(m_ppSweep[0], 0, iRow * iColumn * sizeof(int));
  96. memset(m_ppMine[0], 0, iRow * iColumn * sizeof(int));
  97. }
  98. WinMine::~WinMine()
  99. {
  100. if (m_bMineflag)
  101. {
  102. if (m_ppMine[0]) delete[] m_ppMine[0];
  103. if (m_ppMine) delete[] m_ppMine;
  104. }
  105. if (m_bSweepflag)
  106. {
  107. if (m_ppSweep[0]) delete[] m_ppSweep[0];
  108. if (m_ppSweep) delete[] m_ppSweep;
  109. }
  110. }
  111. //设置颜色
  112. void WinMine::SetColor()
  113. {
  114. system("color 0a");
  115. }
  116. //初始化雷数
  117. void WinMine::InitMine(int iMineMax)
  118. {
  119. int x, y,num = 0;
  120. srand( (unsigned)time(NULL) );
  121. for (int i = 0; num != iMineMax; i++)
  122. {
  123. x = rand()%m_row;
  124. y = rand()%m_column;
  125. if (MINE != m_ppMine[x][y])
  126. {
  127. m_ppMine[x][y] = MINE;
  128. num++;
  129. }
  130. }
  131. m_minenum = num;
  132. }
  133. //统计雷数
  134. void WinMine::StatisticsMine()
  135. {
  136. int i, j;
  137. int n, m;
  138. int num = 0; //保存雷数
  139. //中间
  140. for ( i = 1; i < m_row - 1; i++)
  141. {
  142. for ( j = 1; j < m_column - 1; j++)
  143. {
  144. if ( m_ppMine[i][j] == BLACK)
  145. {
  146. for (n = i - 1; n <= i + 1; n++)
  147. {
  148. for (m = j - 1; m <= j + 1; m++)
  149. {
  150. if ( m_ppMine[n][m] == MINE )
  151. num++;
  152. }
  153. }
  154. m_ppMine[i][j] = num;
  155. num = 0;
  156. }
  157. }
  158. }
  159. //顶边
  160. for ( i = 1; i < m_column - 1; i++)
  161. {
  162. if (m_ppMine[0][i] == BLACK)
  163. {
  164. for (n = 0; n < 2; n++)
  165. {
  166. for (m = i - 1; m <= i + 1; m++)
  167. {
  168. if (m_ppMine[n][m] == MINE)
  169. num++;
  170. }
  171. }
  172. m_ppMine[0][i] = num;
  173. num = 0;
  174. }
  175. }
  176. //下边
  177. for ( i = 1; i < m_column - 1; i++)
  178. {
  179. if (m_ppMine[m_row - 1][i] == BLACK)
  180. {
  181. for (n = m_row - 2; n < m_row; n++)
  182. {
  183. for (m = i - 1; m <= i + 1; m++)
  184. {
  185. if (m_ppMine[n][m] == MINE)
  186. num++;
  187. }
  188. }
  189. m_ppMine[m_row - 1][i] = num;
  190. num = 0;
  191. }
  192. }
  193. //左边
  194. for ( i = 1; i < m_row - 1; i++ )
  195. {
  196. if (m_ppMine[i][0] == BLACK)
  197. {
  198. for (n = i - 1; n <= i + 1; n++)
  199. {
  200. for (m = 0; m < 2; m++)
  201. if (m_ppMine[n][m] == MINE)
  202. num++;
  203. }
  204. m_ppMine[i][0] = num;
  205. num = 0;
  206. }
  207. }
  208. //右边
  209. for ( i = 1; i < m_row - 1; i++ )
  210. {
  211. if (m_ppMine[i][m_column - 1] == BLACK)
  212. {
  213. for (n = i - 1; n <= i + 1; n++)
  214. {
  215. for (m = m_column - 2; m < m_column; m++)
  216. {
  217. if (m_ppMine[n][m] == MINE)
  218. num++;
  219. }
  220. }
  221. m_ppMine[i][m_column - 1] = num;
  222. num = 0;
  223. }
  224. }
  225. //左上角
  226. if (m_ppMine[0][0] != MINE)
  227. {
  228. if (m_ppMine[0][1] == MINE)
  229. num++;
  230. if (m_ppMine[1][1] == MINE)
  231. num++;
  232. if (m_ppMine[1][0] == MINE)
  233. num++;
  234. m_ppMine[0][0] = num;
  235. num = 0;
  236. }
  237. //左下角
  238. if (m_ppMine[m_row - 1][0] != MINE)
  239. {
  240. if (m_ppMine[m_row - 2][0] == MINE)
  241. num++;
  242. if (m_ppMine[m_row - 2][1] == MINE)
  243. num++;
  244. if (m_ppMine[m_row - 1][1] == MINE)
  245. num++;
  246. m_ppMine[m_row - 1][0] = num;
  247. num = 0;
  248. }
  249. //右上角
  250. if (m_ppMine[0][m_column - 1] != MINE)
  251. {
  252. if (m_ppMine[1][m_column - 1] == MINE)
  253. num++;
  254. if (m_ppMine[1][m_column - 2] == MINE)
  255. num++;
  256. if (m_ppMine[0][m_column - 2] == MINE)
  257. num++;
  258. m_ppMine[0][m_column - 1] = num;
  259. num = 0;
  260. }
  261. //右下角
  262. if (m_ppMine[m_row - 1][m_column - 1] != MINE)
  263. {
  264. if (m_ppMine[m_row - 2][m_column - 1] == MINE)
  265. num++;
  266. if (m_ppMine[m_row - 2][m_column - 2] == MINE)
  267. num++;
  268. if (m_ppMine[m_row - 1][m_column - 2] == MINE)
  269. num++;
  270. m_ppMine[m_row - 1][m_column - 1] = num;
  271. num = 0;
  272. }
  273. }
  274. //展开空白
  275. bool WinMine::OpenWhites(int row, int column, int status)
  276. {
  277. if (row < 0 || row > (m_row - 1) || column < 0 || column > (m_column - 1))
  278. return true;
  279. if (status == SWEEP && m_ppMine[row][column] == MINE)
  280. {
  281. return false;
  282. }
  283. if (status == FLAG)
  284. {
  285. m_ppSweep[row][column] = FLAG;
  286. return true;
  287. }
  288. if (m_ppSweep[row][column] == NOSWEEP && m_ppMine[row][column] != MINE)
  289. {
  290. if (m_ppMine[row][column] > 0)
  291. {
  292. m_ppSweep[row][column] = SWEEP;
  293. return true;
  294. }
  295. else
  296. {
  297. m_ppSweep[row][column] = SWEEP;
  298. OpenWhites(row-1, column, status);
  299. OpenWhites(row-1, column-1, status);
  300. OpenWhites(row-1, column+1, status);
  301. OpenWhites(row, column-1, status);
  302. OpenWhites(row, column+1, status);
  303. OpenWhites(row+1, column, status);
  304. OpenWhites(row+1, column-1, status);
  305. OpenWhites(row+1, column+1, status);
  306. }
  307. }
  308. return true;
  309. }
  310. //地图
  311. void WinMine::Map()
  312. {
  313. SetColor();
  314. int i, j;
  315. for ( i = 0; i < m_row; i++)
  316. {
  317. for (j = 0; j < m_column; j++)
  318. {
  319. if (m_ppSweep[i][j] == SWEEP)
  320. {
  321. if (m_ppMine[i][j] == 0)
  322. {
  323. cout << "□";
  324. }
  325. else if (m_ppMine[i][j] == 1)
  326. {
  327. cout << "①";
  328. }
  329. else if (m_ppMine[i][j] == 2)
  330. {
  331. cout << "②";
  332. }
  333. else if (m_ppMine[i][j] == 3)
  334. {
  335. cout << "③";
  336. }
  337. else if (m_ppMine[i][j] == 4)
  338. {
  339. cout << "④";
  340. }
  341. else if (m_ppMine[i][j] == 5)
  342. {
  343. cout << "⑤";
  344. }
  345. else if (m_ppMine[i][j] == 6)
  346. {
  347. cout << "⑥";
  348. }
  349. else if (m_ppMine[i][j] == 7)
  350. {
  351. cout << "⑦";
  352. }
  353. else if (m_ppMine[i][j] == 8)
  354. {
  355. cout << "⑧";
  356. }
  357. }
  358. else if (m_ppSweep[i][j] == FLAG)
  359. {
  360. cout << "⊙";
  361. }
  362. else
  363. cout << "▇";
  364. }
  365. cout << endl;
  366. }
  367. }
  368. //游戏结束
  369. void WinMine::GameOver()
  370. {
  371. int i, j;
  372. for ( i = 0; i < m_row; i++)
  373. {
  374. for (j = 0; j < m_column; j++)
  375. {
  376. if (m_ppMine[i][j] == MINE)
  377. cout << "★";
  378. else
  379. cout << "□";
  380. }
  381. cout << endl;
  382. }
  383. }
  384. //检查是否雷标记正确。
  385. bool WinMine::GoodOver()
  386. {
  387. int i, j;
  388. int num = 0;
  389. for (i = 0; i < m_row; i++)
  390. {
  391. for (j = 0; j < m_column; j++)
  392. {
  393. if (m_ppSweep[i][j] == FLAG && m_ppMine[i][j] == MINE)
  394. num++;
  395. }
  396. }
  397. if (num == m_minenum)
  398. return true;
  399. else
  400. return false;
  401. }
  402. //开始游戏
  403. void WinMine::GameStart(int iMineMax)
  404. {
  405. int x, y;
  406. int flag;
  407. begin:
  408. memset(m_ppSweep[0], 0, m_row * m_column * sizeof(int));
  409. memset(m_ppMine[0], 0, m_row * m_column * sizeof(int));
  410. InitMine(iMineMax);
  411. StatisticsMine();
  412. while (true)
  413. {
  414. system("cls");
  415. Map();
  416. cout << "请输入要扫的区域的坐标:" <<endl;
  417. cout << "请输入纵坐标:";cin>>x;
  418. cout << "请输入横坐标:";cin>>y;
  419. if (x <= 0 || x > m_row || y <= 0 || y > m_column)
  420. {
  421. cout <<"输入错误请重新输入" <<endl;
  422. Sleep(1000);
  423. continue;
  424. }
  425. cout << "请输入是要做标记还是扫雷,扫雷请按1,标记请按2:" <<endl;
  426. cin >> flag;
  427. if ( false == OpenWhites(x-1, y-1, flag) )
  428. {
  429. int i;
  430. system("cls");
  431. GameOver();
  432. cout << "游戏结束!" <<endl;
  433. cout << "继续游戏请按1,退出游戏请按0:"<<endl;
  434. cin >> i;
  435. if (i == 1)
  436. goto begin;
  437. else
  438. goto end;
  439. }
  440. else
  441. {
  442. if (GoodOver() == true)
  443. {
  444. int i;
  445. cout << "扫雷成功!" <<endl;
  446. cout << "继续游戏请按1,退出游戏请按0:"<<endl;
  447. cin >> i;
  448. if (i == 1)
  449. goto begin;
  450. else
  451. goto end;
  452. }
  453. }
  454. }
  455. end:
  456. return;
  457. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/748901
推荐阅读
相关标签
  

闽ICP备14008679号