Advertisement
Guest User

RON

a guest
Jul 28th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.20 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <time.h>
  4. # define N 5
  5. # define M 10
  6.  
  7. void OpenSquareContainsZero(char board[ ][N], char flags[ ][N],int i,int j);
  8. int CountBombsLeft(char board[][N]);
  9. int CheckOpenedSquares(char flags[][N]);
  10. int CheckOpenedBomb(char board[][N], char flags[][N]);
  11. void InitBoards(char board[][N], char flags[][N]);
  12. int IsMine(char board[ ][N], int i, int j);
  13. void SetFlag(char flags[ ][N],int i,int j);
  14. void OpenSquare(char board[ ][N], char flags[ ][N],int i,int j);
  15. void PrintBoard(char board[ ][N],char flags[ ][N]);
  16. int CountOpenSquares(char flags[ ][N]);
  17. int CountBombsAround(char board[ ][N], int i, int j);
  18.  
  19. void OpenSquareContainsZero(char board[ ][N], char flags[ ][N],int i,int j) // The function open a square if it is circled by 0 bombs, according to the rules
  20. {
  21. if(i<0 || i>N-1 || j<0 || j>N-1 || flags[i][j] == 'F' || CountBombsAround(board, i, j) != 0) return; // Breakpoint of the recursion
  22. else // For each square which circles the current square([i][j]) if the circling square is closed, it opens it, and do the same recursion for the circling square
  23. {
  24. flags[i][j] = '1'; // If the program entered here, it means that the 'If' above did not interrupt to the program, so make this square open
  25. if(i-1>=0 && i-1<=N-1 && j-1>=0 && j-1<=N-1 && flags[i-1][j-1] == '0') // For instance, if the row and the column of this square is legal, and the square is closed, then open it and make recursion for the square.
  26. {
  27. flags[i-1][j-1] = '1';
  28. OpenSquareContainsZero(board, flags, i-1, j-1);
  29. }
  30. if(i-1>=0 && i-1<=N-1 && j>=0 && j<=N-1 && flags[i-1][j] == '0')
  31. {
  32. flags[i-1][j] = '1';
  33. OpenSquareContainsZero(board, flags, i-1, j);
  34. }
  35. if(i-1>=0 && i-1<=N-1 && j+1>=0 && j+1<=N-1 && flags[i-1][j+1] == '0')
  36. {
  37. flags[i-1][j+1] = '1';
  38. OpenSquareContainsZero(board, flags, i-1, j+1);
  39. }
  40. if(i>=0 && i<=N-1 && j-1>=0 && j-1<=N-1 && flags[i][j-1] == '0')
  41. {
  42. flags[i][j-1] = '1';
  43. OpenSquareContainsZero(board, flags, i, j-1);
  44. }
  45. if(i>=0 && i<=N-1 && j+1>=0 && j+1<=N-1 && flags[i][j+1] == '0')
  46. {
  47. flags[i][j+1] = '1';
  48. OpenSquareContainsZero(board, flags, i, j+1);
  49. }
  50. if(i+1>=0 && i+1<=N-1 && j-1>=0 && j-1<=N-1 && flags[i+1][j-1] == '0')
  51. {
  52. flags[i+1][j-1] = '1';
  53. OpenSquareContainsZero(board, flags, i+1, j-1);
  54. }
  55. if(i+1>=0 && i+1<=N-1 && j>=0 && j<=N-1 && flags[i+1][j] == '0')
  56. {
  57. flags[i+1][j] = '1';
  58. OpenSquareContainsZero(board, flags, i+1, j);
  59. }
  60. if(i+1>=0 && i+1<=N-1 && j+1>=0 && j+1<=N-1 && flags[i+1][j+1] == '0')
  61. {
  62. flags[i+1][j+1] = '1';
  63. OpenSquareContainsZero(board, flags, i+1, j+1);
  64. }
  65. }
  66. }
  67. void InitBoards(char board[][N], char flags[][N]) // Initialization the boards
  68. {
  69. srand(time(NULL)); // Function to make the random to work well
  70. int rowpos, colpos, i, j;
  71. for(i=0; i<N; i++) // This loop makes the whole flag board as closed (All the squares are closed)
  72. {
  73. for(j=0; j<N; j++)
  74. {
  75. flags[i][j] = '0';
  76. }
  77. }
  78. for(i=0; i<M; i++) // The loop puts bombs in the board
  79. {
  80. rowpos = rand()%N; // Random a row position
  81. colpos = rand()%N; // Random a column position
  82. while(IsMine(board, rowpos, colpos) == 1) // If the program did random to square that already has bomb, it would make another random
  83. {
  84. rowpos = rand()%N;
  85. colpos = rand()%N;
  86. }
  87. board[rowpos][colpos] = 'X'; // After the program found a square that did not have already bomb it puts a bomb there
  88. }
  89. for(i=0; i<N; i++)
  90. {
  91. for(j=0; j<N; j++)
  92. {
  93. if(IsMine(board, i, j) == 0) board[i][j] = (char)(((int)'0')+CountBombsAround(board, i,j)); // If the square does not contain bomb, the square shows the number of the bomb that circle it
  94. }
  95. }
  96. }
  97. int IsMine(char board[ ][N], int i, int j) // The function checks if the square [i][j] contains bomb --> return 1, else return 0.
  98. {
  99. if(board[i][j] == 'X') return 1;
  100. return 0;
  101. }
  102.  
  103. void SetFlag(char flags[ ][N],int i,int j) // If the square [i][j] has already 'F' the function make it closed. Otherwise the function mark the square as flag
  104. {
  105. if(flags[i][j] == '1')printf("This Square is already open.\n");
  106. else if(flags[i][j] == 'F') flags[i][j] = '0';
  107. else
  108. flags[i][j] = 'F';
  109. }
  110.  
  111. void OpenSquare(char board[ ][N], char flags[ ][N],int i,int j) // The function opens square according the rules of the game
  112. {
  113. if(flags[i][j] == '1') printf("This square is already open.\n"); // If the user wants to open opened square then the message will show
  114. else if(board[i][j] == 'X' && flags[i][j] != 'F') // If the user chose to open a square which included bomb then...
  115. {
  116. flags[i][j] = '1'; // Make the square opened (a message of loss shows in the main...)
  117. PrintBoard(board, flags); // It prints the board of the game
  118. }
  119. else if(CountBombsAround(board, i, j) >= 1 && CountBombsAround(board, i, j) <= 8 && flags[i][j] != 'F') flags[i][j] = '1'; // If the square shows number in [1,8] then open it as usual
  120. else if(CountBombsAround(board, i, j) == 0) OpenSquareContainsZero(board, flags, i, j); // If the square shows '0' then open it according to the rules of the called function..
  121. else if(flags[i][j] == 'F') // If the user wants to open a square which he marked it as a bomb then....
  122. {
  123. int choice;
  124. printf("This square is flagged as a mine. Are you sure you want to open it?\n");
  125. printf("Enter your choice: 1. Yes 2. No\n");
  126. scanf("%d", &choice);
  127. if(choice == 1) flags[i][j] = '0'; // If the user wants to open it the program will make the square as close
  128. }
  129. }
  130.  
  131. void PrintBoard(char board[ ][N],char flags[ ][N]) // The function prints the board
  132. {
  133. int i, j;
  134. printf(" 01234\n");
  135. printf(" -----\n");
  136. for(i=0; i<N; i++)
  137. {
  138. printf("%d%c",i, ':');
  139. for(j=0; j<N; j++)
  140. {
  141. if(board[i][j] == 'X')printf("X");
  142. if(flags[i][j] == '0' && board[i][j] != 'X')printf("%c", 178); // If the square is closed then it prints the char 178
  143. else if(flags[i][j] == 'F')printf("F"); // If the square marked as a bomb by the user, it prints the square as 'F'
  144. else if(flags[i][j] == '1') // If the square is opened...
  145. {
  146. if(IsMine(board, i, j) == 1)printf("X"); // If the square includes bomb it prints 'X'
  147. else
  148. printf("%d", CountBombsAround(board, i, j)); // Otherwise it prints the number of the bombs that circle the square
  149. }
  150. }
  151. printf("\n");
  152. }
  153.  
  154. }
  155.  
  156. int CountOpenSquares(char flags[ ][N]) // The function returns the number of the opened squares
  157. {
  158. int i, j, count = 0;
  159. for(i=0; i<N; i++)
  160. {
  161. for(j=0; j<N; j++)
  162. {
  163. if(flags[i][j] == '1')count++; // The function go all over the flags board and raise the count in 1 if the square is opened
  164. }
  165. }
  166. return count;
  167. }
  168.  
  169. int CountBombsAround(char board[ ][N], int i, int j) // The function counts the number of the bombs that circle the square i,j
  170. {
  171. int count = 0;
  172. if(i == 0 && j == 0) // The case that the bomb in the left top corner of the board
  173. {
  174. if(board[i][j+1] == 'X')count++;
  175. if(board[i+1][j] == 'X')count++;
  176. if(board[i+1][j+1] == 'X')count++;
  177. }
  178. else if(i == N-1 && j == N-1) // The case that the square in the right bottom corner of the board
  179. {
  180. if(board[i-1][j] == 'X')count++;
  181. if(board[i][j-1] == 'X')count++;
  182. if(board[i-1][j-1] == 'X')count++;
  183. }
  184. else if(i == N-1 && j == 0) // The case that the square in the left bottom corner of the board
  185. {
  186. if(board[i][j+1] == 'X')count++;
  187. if(board[i-1][j] == 'X')count++;
  188. if(board[i-1][j+1] == 'X')count++;
  189. }
  190. else if(i == 0 && j == N-1) // The case that the square in the right top corner of the board
  191. {
  192. if(board[i][j-1] == 'X')count++;
  193. if(board[i+1][j] == 'X')count++;
  194. if(board[i+1][j-1] == 'X')count++;
  195. }
  196. else if(j == 0) // The case that the square is not in any corner, but the square is in the left edge of the board
  197. {
  198. if(board[i-1][j] == 'X')count++;
  199. if(board[i+1][j] == 'X')count++;
  200. if(board[i][j+1] == 'X')count++;
  201. if(board[i+1][j+1] == 'X')count++;
  202. if(board[i-1][j+1] == 'X')count++;
  203. }
  204. else if(j == N-1) // The case that the square is not in any corner, but the square is in the right edge of the board
  205. {
  206. if(board[i-1][j] == 'X')count++;
  207. if(board[i+1][j] == 'X')count++;
  208. if(board[i][j-1] == 'X')count++;
  209. if(board[i-1][j-1] == 'X')count++;
  210. if(board[i+1][j-1] == 'X')count++;
  211. }
  212. else if(i == 0) // The case that the square is not in any corner, but the square is in the top edge of the board
  213. {
  214. if(board[i][j-1] == 'X')count++;
  215. if(board[i][j+1] == 'X')count++;
  216. if(board[i+1][j] == 'X')count++;
  217. if(board[i+1][j-1] == 'X')count++;
  218. if(board[i+1][j+1] == 'X')count++;
  219. }
  220. else if(i == N-1) // The case that the square is not in any corner, but the square is in the bottom edge of the board
  221. {
  222. if(board[i][j-1] == 'X')count++;
  223. if(board[i][j+1] == 'X')count++;
  224. if(board[i-1][j] == 'X')count++;
  225. if(board[i-1][j-1] == 'X')count++;
  226. if(board[i-1][j+1] == 'X')count++;
  227. }
  228. else // The case that the square is not in any edge or corner of the board
  229. {
  230. if(board[i-1][j-1] == 'X')count++;
  231. if(board[i-1][j] == 'X')count++;
  232. if(board[i-1][j+1] == 'X')count++;
  233. if(board[i][j-1] == 'X')count++;
  234. if(board[i][j+1] == 'X')count++;
  235. if(board[i+1][j-1] == 'X')count++;
  236. if(board[i+1][j] == 'X')count++;
  237. if(board[i+1][j+1] == 'X')count++;
  238. }
  239.  
  240. return count;
  241. }
  242.  
  243. int CheckOpenedBomb(char board[][N], char flags[][N]) // The function checks if any square opened by the user and included bomb
  244. {
  245. int i, j;
  246. for(i=0; i<N; i++)
  247. {
  248. for(j=0; j<N; j++)
  249. {
  250. if(IsMine(board, i, j) == 1 && flags[i][j] == '1') return 1; // If any square in the board was opened and it included bomb then return 1
  251. }
  252. }
  253. return 0; // If the loop 'for' did not break by the 'return' it means the user did not open bomb so return 0
  254. }
  255.  
  256. int CountBombsLeft(char flags[][N]) // The function counts the number of the bombs that left, by the mark of the flags
  257. {
  258.  
  259. int i, j, count = M; // Count is M (Number of the bombs)
  260. for(i=0; i<N; i++)
  261. {
  262. for(j=0; j<N; j++)
  263. {
  264. if(flags[i][j] == 'F') count--; // If any square contains Flag (it means the user flagged the square) then count--...
  265. }
  266. }
  267. return count;
  268. }
  269.  
  270. int main()
  271. {
  272. int choice, rowchoice, colchoice;
  273. char board[N][N]; // Creating board NxN
  274. char flags[N][N]; // Creating board NxN
  275. InitBoards(board, flags); // initialization the boards
  276. while(CheckOpenedBomb(board, flags) != 1 && CountOpenSquares(flags) != ((N*N)-M)) // The loop will stop once one of those cases will happen
  277. {
  278. PrintBoard(board,flags); // Each loop it prints the board
  279. printf("There are %d mines left.\nWhat would you like to do now:\n", CountBombsLeft(flags));
  280. printf("1. Open a new square.\n2. Flag a square as a mine.\n");
  281. scanf("%d", &choice); // Receive from the user the choice
  282. if(choice == 2) // The case the user will choose to flag a square as a mine
  283. {
  284. printf("Please insert row and column number: ");
  285. scanf("%d", &rowchoice); // Receive the row of the flag from the user
  286. scanf("%d", &colchoice); // Receive the column of the flag from the user
  287. SetFlag(flags, rowchoice, colchoice); // Put a flag into the square the user chose
  288. }
  289. if(choice == 1) // The case the user will choose to open any square
  290. {
  291. printf("Please insert row and column number: ");
  292. scanf("%d", &rowchoice); // Receive the row of the square that the user wants to open
  293. scanf("%d", &colchoice); // Receive the column of the square that the user wants to open
  294. OpenSquare(board, flags, rowchoice, colchoice); // The program will open the square of the user according to the rules of the game
  295. }
  296. }
  297. if(CountOpenSquares(flags) == ((N*N)-M))
  298. {
  299. PrintBoard(board,flags);
  300. printf("Congratulations!! You have won!");
  301. }
  302. else if(CheckOpenedBomb(board, flags) == 1) printf("Boom!!! You have lost.");
  303. // If the loop just stopped for any reason there are 2 cases which could caused that:
  304. // 1. The user opened a bomb, so the program will inform him he lost by the message above.
  305. // 2. The user won the game because he opened the whole squares but the bombs --> the sentence above will be informed to the user.
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement