Advertisement
Guest User

Untitled

a guest
Mar 5th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <time.h>
  4. #include <ctime>
  5. #include <fstream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. //help function
  10. void help() {
  11. cout << endl;
  12. cout << "Welcome to Minesweeper Game Made by Dr.Death" << endl;
  13. cout << "At any given Time you can press D for Replay or E for Exiting the game" << endl;
  14. cout << "1.during play use left and right click option for replay and Exit " << endl;
  15. cout << "2.when u see 0 revealed then u can open its neighboring cell without any risk" << endl;
  16. cout << "3.only 10 Flags are available.Flags are your hint that mine is there " << endl;
  17. cout << "4.when you will open all the numbers without mine.you will win " << endl;
  18. cout << "5.To unflag a flag jump to same cell and press r" << endl;
  19. }
  20.  
  21.  
  22.  
  23. //Placing mines
  24. void mines(int a[][8], int size) {
  25. for (int i = 0; i<10; i++)
  26. {
  27. int row = rand() % size;
  28. int col = rand() % size;
  29. if (a[row][col] == 9)
  30. {
  31. i--;
  32. }
  33. a[row][col] = 9;
  34. }
  35. }
  36.  
  37. //placing numbers
  38. void numbers(int array[][8], int size) {
  39. int j,k;
  40.  
  41. for (int j = 0; j<size; j++) {
  42. for (int k = 0; k<size; k++) {
  43.  
  44.  
  45. if (array[j][k] != 9 && k>0 && k<7) {
  46. if (array[j - 1][k - 1] == 9) {
  47. array[j][k]++;
  48. }
  49. }
  50. if (array[j][k] != 9 && k>0 && k<7) {
  51. if (array[j - 1][k] == 9) {
  52. array[j][k]++;
  53. }
  54. }
  55.  
  56. if (array[j][k] != 9 && k>0 && k<7) {
  57. if (array[j - 1][k + 1] == 9) {
  58. array[j][k]++;
  59. }
  60. }
  61.  
  62. if (array[j][k] != 9 && k>0 && k<7) {
  63. if (array[j][k - 1] == 9) {
  64. array[j][k]++;
  65. }
  66. }
  67.  
  68. if (array[j][k] != 9 && k>0 && k<7) {
  69. if (array[j][k + 1] == 9) {
  70. array[j][k]++;
  71. }
  72. }
  73.  
  74. if (array[j][k] != 9 && k>0 && k<7) {
  75. if (array[j + 1][k - 1] == 9) {
  76. array[j][k]++;
  77. }
  78. }
  79.  
  80. if (array[j][k] != 9 && k>0 && k<7) {
  81. if (array[j + 1][k] == 9) {
  82. array[j][k]++;
  83. }
  84. }
  85. if (array[j][k] != 9 && k>0 && k<7) {
  86. if (array[j + 1][k + 1] == 9) {
  87. array[j][k]++;
  88. }
  89. }
  90. }
  91. }
  92. for (j = 0; j<size; j++) {
  93. for (int k = 0; k<size; k++) {
  94.  
  95. if ((array[j][k] != 9) && (k == 0)) {
  96. if (array[j - 1][k] == 9) {
  97. array[j][k]++;
  98. }
  99. }
  100.  
  101. if ((array[j][k] != 9) && (k == 0)) {
  102. if (array[j - 1][k + 1] == 9) {
  103. array[j][k]++;
  104. }
  105. }
  106.  
  107. if ((array[j][k] != 9) && (k == 0)) {
  108. if (array[j][k + 1] == 9) {
  109. array[j][k]++;
  110. }
  111. }
  112.  
  113. if ((array[j][k] != 9) && (k == 0)) {
  114. if (array[j + 1][k] == 9) {
  115. array[j][k]++;
  116. }
  117. }
  118.  
  119. if ((array[j][k] != 9) && (k == 0)) {
  120. if (array[j + 1][k + 1] == 9) {
  121. array[j][k]++;
  122. }
  123. }
  124.  
  125.  
  126. if ((array[j][k] != 9) && (k == 7)) {
  127. if (array[j - 1][k - 1] == 9) {
  128. array[j][k]++;
  129. }
  130. }
  131.  
  132. if ((array[j][k] != 9) && (k == 7)) {
  133. if (array[j - 1][k] == 9) {
  134. array[j][k]++;
  135. }
  136. }
  137.  
  138. if ((array[j][k] != 9) && (k == 7)) {
  139. if (array[j][k - 1] == 9) {
  140. array[j][k]++;
  141. }
  142. }
  143.  
  144. if ((array[j][k] != 9) && (k == 7)) {
  145. if (array[j + 1][k - 1] == 9) {
  146. array[j][k]++;
  147. }
  148. }
  149.  
  150. if ((array[j][k] != 9) && (k == 7)) {
  151. if (array[j + 1][k] == 9) {
  152. array[j][k]++;
  153. }
  154. }
  155.  
  156. }
  157. }
  158.  
  159. }
  160.  
  161. //setting board
  162. void board(char array2[][8], int size)
  163.  
  164. {
  165.  
  166. int cell = 178;
  167. for (int m = 0; m<size; m++)
  168.  
  169. {
  170. for (int j = 0; j<size; j++)
  171.  
  172. {
  173.  
  174. array2[m][j] = cell;
  175.  
  176. }
  177. }
  178. }
  179.  
  180. //moving in the board this is the main function
  181. int axis(int array[][8], char array2[][8], int array3[][8], int array4[][8], int size) {
  182. int counterf = 0;
  183. int total = 0; int totalf = 0;
  184. int set = 0;
  185. int a = 178;
  186. int b = 179;
  187. int urow;
  188. int ucol;
  189. char o;
  190. int temp;
  191. int count = 0;
  192. for (int x = 0; x<8; x++) {
  193. for (int y = 0; y<8; y++) {
  194. if (array[x][y] != 9) {
  195. totalf = totalf + array[x][y];
  196. }
  197. }
  198. }
  199.  
  200. int time = 0;
  201. int set2;
  202. for (int i = 1; i>0; i++)
  203. {
  204. count++;
  205. if (i>0) {
  206.  
  207. time = clock();
  208. if (time / 1000>999) // minesweeper has three digit timer
  209. {
  210. time = 999999;
  211. }
  212. cout << "time elasped : " << time / 1000 << " seconds" << endl;
  213. cout << "total : " << total << endl;
  214. cout << "Enter row no. : ";
  215. cin >> urow;
  216. cout << "Enter coloumn no. : ";
  217. cin >> ucol;
  218. cout << "Enter right click or left click : ";
  219. cin >> o;
  220. urow = urow - 1;
  221. ucol = ucol - 1;
  222. if (o == 'D' || o == 'd') {
  223. cout << "do you really want to restart game(press 1 for yes or any other key for no) ";
  224. cin >> set2;
  225. if (set2 == 1) {
  226. break;
  227. }
  228. if (set2 == 2) {
  229. cout << " ";
  230. continue;
  231. }
  232. }
  233. if (o == 'e' || o == 'E') {
  234. cout << "do you really want to Exit from game(press 1 for yes or anyother key for no) ";
  235. cin >> set2;
  236. if (set2 == 1) {
  237. cout << "Thanks for Playing Alah Hafiz" << endl;
  238. system("PAUSE");
  239. return 0;
  240. }
  241. if (set2 >= 2) {
  242. cout << " ";
  243. continue;
  244. }
  245. }
  246.  
  247. system("cls");
  248. }
  249. for (int m = 0; m<size; m++) {
  250. for (int n = 0; n<size; n++) {
  251. if (m == urow && n == ucol && o == 'l'&&array[m][n] != 9 && array3[m][n] == 0 && array2[m][n] != 'f')
  252. {
  253. temp = array[m][n];
  254.  
  255. if (temp == 0) {
  256. array2[m][n] = 48;
  257. array3[m][n] = 1;
  258. }
  259.  
  260. if (temp == 1) {
  261. array2[m][n] = 49;
  262. array3[m][n] = 1;
  263. }
  264.  
  265. if (temp == 2) {
  266. array2[m][n] = 50;
  267. array3[m][n] = 1;
  268. }
  269.  
  270. if (temp == 3) {
  271. array2[m][n] = 51;
  272. array3[m][n] = 1;
  273. }
  274.  
  275. if (temp == 4) {
  276. array2[m][n] = 52;
  277. array3[m][n] = 1;
  278. }
  279.  
  280. if (temp == 5) {
  281. array2[m][n] = 53;
  282. array3[m][n] = 1;
  283. }
  284.  
  285. if (temp == 6) {
  286. array2[m][n] = 54;
  287. array3[m][n] = 1;
  288. }
  289.  
  290. if (temp == 7) {
  291. array2[m][n] = 55;
  292. array3[m][n] = 1;
  293. }
  294.  
  295. if (temp == 8) {
  296. array2[m][n] = 56;
  297. array3[m][n] = 1;
  298.  
  299. }
  300. total = total + array[m][n];
  301. }
  302. if (m == urow && n == ucol && o == 'r'&&array3[m][n] == 0 && array4[m][n] == 0 && counterf<10)
  303. {
  304. array2[m][n] = 'f';
  305. array4[m][n] = 1;
  306. counterf = counterf + 1;
  307.  
  308. }
  309. else if (m == urow && n == ucol && o == 'r'&&array4[m][n] == 1)
  310. {
  311. array2[m][n] = 0;
  312. array4[m][n] = 0;
  313. counterf = counterf - 1;
  314.  
  315. }
  316.  
  317. cout << array2[m][n] << " " << " ";
  318.  
  319. }
  320. cout << endl << endl;
  321. }
  322. if (array[urow][ucol] == 9 && o == 'l'&&array2[urow][ucol] != 'f') {
  323. system("cls");
  324. for (int(m) = 0; m<8; m++) {
  325. for (int n = 0; n<8; n++) {
  326. if (array[m][n] == 9)
  327. {
  328. array2[m][n] = 'X'; // x is mine
  329. }
  330.  
  331. cout << array2[m][n] << " " << " ";
  332.  
  333.  
  334. }
  335. cout << endl << endl;
  336. }
  337.  
  338. cout << "you Just hit a mine.ohhh thats sad.better luck next time : " << endl;
  339.  
  340. break;
  341. }
  342. if (total == totalf) {
  343. int temp4 = time / 1000;
  344. system("cls");
  345. for (int r = 0; r<8; r++) {
  346. for (int l = 0; l<8; l++) {
  347. if (array2[r][l] == 'f' || array[r][l] == 9) {
  348. array2[r][l] = 'X';
  349. }
  350. cout << array2[r][l] << " " << " ";
  351. }
  352. cout << endl << endl;
  353. }
  354. cout << "congratulation You win :" << endl;
  355. //highscore(temp4);
  356. break;
  357. }
  358.  
  359. }
  360. }
  361.  
  362. /*
  363. void highscore(int & b) {
  364. char array[20];
  365. ifstream ifile;
  366. ifile.open("highscore.txt");
  367. ofstream ofile;
  368. ofile.open("highscore.txt");
  369. int a;
  370. while (!ifile.eof()) {
  371. ifile >> a;
  372. if (a>b) {
  373. ofile << a; ofile << " ";
  374. cout << "Enter name ";
  375. cin.getline(array, 20, '\n');
  376. for (int i = 0; array[i] != '\0'; i++) {
  377. ofile << array[i];
  378. }
  379. }
  380. }
  381. ifile.close();
  382. ofile.close();
  383. }
  384. */
  385.  
  386.  
  387.  
  388. //Main
  389. int main() {
  390. int array[8][8] = { 0 };
  391. int array3[8][8] = { 0 };
  392. int array4[8][8] = { 0 };
  393. char array2[8][8];
  394. int size = 8;
  395. int choice;
  396. //int choice2;
  397. cout << "Enter 1 for Help or 3 to play game " << endl;
  398. cin >> choice;
  399. if (choice == 1) {
  400. help();
  401. cout << endl << endl;
  402. cout << "To play game now.Press 3 ";
  403. cin >> choice;
  404. }
  405. #pragma warning(suppress: 6001)
  406. if (choice >= 2 || choice == 3) {
  407.  
  408. //srand(time(0));
  409.  
  410. mines(array, size);
  411. numbers(array, size);
  412.  
  413. /*for(int i=0;i<8;i++){
  414. for(int j=0;j<8;j++){
  415. cout<<" "<<array[i][j]<<" ";
  416. }
  417. cout<<endl<<endl;
  418. }
  419. */
  420. board(array2, size);
  421. axis(array, array2, array3, array4, size);
  422. char o;
  423. cout << "Do you want to countinue or exit, Enter R for replay or E for exit ";
  424. cin >> o;
  425. switch (o) {
  426. case 'r':
  427. case 'R':
  428. {
  429. system("cls");
  430. for (int f = 0; f<8; f++) {
  431. for (int n = 0; n<8; n++) {
  432. array[f][n] = 0;
  433. array3[f][n] = 0;//this array checks whether mine is revealed or unrevealed
  434. array4[f][n] = 0; // this array checks that whether blocks has some flag or not
  435. //array2[f][n] = 178;
  436. }
  437. }
  438. mines(array, size);
  439. numbers(array, size);
  440. //board(array,size);
  441. axis(array, array2, array3, array4, size);
  442. break;
  443. }
  444. case 'e':
  445. case 'E':
  446. cout << "Thanks for Playing" << endl;
  447. system("PAUSE");
  448. break;
  449.  
  450. default:
  451. cout << "Invalid option Entered" << endl;
  452. break;
  453. }
  454. }
  455. return 0;
  456.  
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement