Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.24 KB | None | 0 0
  1. // Assignment 1 20T1 COMP1511: Minesweeper
  2. // minesweeper.c
  3. //
  4. // This program was written by Tam Do (z5312910)
  5. // on 07/03/2020
  6. //
  7. // Version 1.0.0 (2020-03-08): Assignment released.
  8. // Version 1.0.1 (2020-03-08): Fix punctuation in comment.
  9. // Version 1.0.2 (2020-03-08): Fix second line of header comment to say minesweeper.c
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. // Possible square states.
  15. #define VISIBLE_SAFE 0
  16. #define HIDDEN_SAFE 1
  17. #define HIDDEN_MINE 2
  18.  
  19. // The size of the starting grid.
  20. #define SIZE 8
  21.  
  22. // The possible command codes.
  23. #define DETECT_ROW 1
  24. #define DETECT_COL 2
  25. #define DETECT_SQUARE 3
  26. #define REVEAL_SQUARE 4
  27. #define GAMEPLAY_MODE 5
  28. #define DEBUG_MODE 6
  29. #define REVEAL_RADIAL 7
  30.  
  31. // Add any extra #defines here.
  32.  
  33. void initialise_field(int minefield[SIZE][SIZE]);
  34. void print_debug_minefield(int minefield[SIZE][SIZE]);
  35.  
  36. // Game check function
  37. int game_check(int gameOver, int minefield[SIZE][SIZE]);
  38.  
  39. // Detect 3x3 function
  40. int square_detect(int minefield[SIZE][SIZE], int sizeDetect, int rowOrCol, int colOrRow);
  41.  
  42. // Detect row or column
  43. void detect_row_col(int integer, int minefield[SIZE][SIZE], int hintCounter);
  44.  
  45. // Valid input check
  46. int valid_check(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow, int numberOfMines);
  47.  
  48. // Reveal square 3x3
  49. void reveal_square(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow);
  50.  
  51. // Gameplay Mode
  52. void gameplay_mode(int minefield[SIZE][SIZE], int gameOver, int gameLost);
  53.  
  54. // Reveal radial
  55. void reveal_radial(int minefield[SIZE][SIZE], int rowX, int colY);
  56.  
  57. // Mine check
  58. int check_mines(int minefield[SIZE][SIZE], int rowX, int colY);
  59.  
  60.  
  61. // Place your function prototyes here.
  62.  
  63. int main(void) {
  64. int minefield[SIZE][SIZE];
  65. initialise_field(minefield);
  66. int gamePlayMode = 0; // Gameplay mode (false)
  67. printf("Welcome to minesweeper!\n");
  68. printf("How many mines? ");
  69.  
  70. // TODO: Scan in the number of pairs of mines.
  71.  
  72. int numMine;
  73. scanf("%d",&numMine);
  74. printf("Enter pairs:\n");
  75. int numRow;
  76. int numCol;
  77. int counterMine = 0;
  78. while (counterMine < numMine) {
  79. scanf("%d %d", &numRow, &numCol);
  80.  
  81. if (numRow >= 0 && numRow <= 7) {
  82. if (numCol >= 0 && numCol <= 7) {
  83. if (minefield[numRow][numCol] != HIDDEN_MINE) {
  84. minefield[numRow][numCol] = HIDDEN_MINE;
  85. }
  86. }
  87. }
  88. counterMine++;
  89. }
  90.  
  91. // TODO: Scan in the pairs of mines and place them on the grid.
  92.  
  93. printf("Game Started\n");
  94. print_debug_minefield(minefield);
  95.  
  96. // TODO: Scan in commands to play the game until the game ends.
  97.  
  98. int integer = 0;
  99. int gameOver = 0; // Game Over mechanic
  100. int gameLost = 0;
  101. int hintCounter = 0;
  102. while (gameOver == 0) {
  103. int end = scanf("%d", &integer);
  104. if (end == EOF){
  105. break;
  106. }
  107. // Detect row and column
  108. if (integer == DETECT_ROW || integer == DETECT_COL) {
  109. detect_row_col(integer, minefield, hintCounter);
  110. hintCounter++;
  111. }
  112. if (integer == DETECT_SQUARE) { // Detect square
  113. int rowOrCol = 0;
  114. int colOrRow = 0;
  115. int sizeDetect = 0;
  116. scanf(" %d %d %d", &rowOrCol, &colOrRow, &sizeDetect);
  117. int numMines = square_detect(minefield, sizeDetect, rowOrCol, colOrRow);
  118.  
  119. if (hintCounter >= 3) {
  120. printf("Help already used\n");
  121. } else {
  122. printf("There are %d mine(s) in the square centered at row %d, column %d of size %d\n", numMines, rowOrCol, colOrRow, sizeDetect);
  123. hintCounter++;
  124. }
  125.  
  126. }
  127.  
  128. if (integer == REVEAL_SQUARE) { // Reveal square
  129. int rowOrCol = 0;
  130. int colOrRow = 0;
  131. int numberOfMines = 0;
  132. scanf(" %d %d", &rowOrCol, &colOrRow);
  133. int numMines = valid_check(minefield, rowOrCol, colOrRow, numberOfMines);
  134. if (minefield[rowOrCol][colOrRow] == HIDDEN_MINE) { // Lose
  135. gameOver++;
  136. printf("Game over\n");
  137. if (gamePlayMode == 1) { // Lose in gameplay mode
  138. printf("xx\n");
  139. printf("/\\\n");
  140. gameLost = 1;
  141. }
  142. }
  143.  
  144. if (numMines > 0 && gameOver == 0) { // Reveal 1x1
  145. minefield[rowOrCol][colOrRow] = VISIBLE_SAFE;
  146. int score = game_check(gameOver, minefield);
  147. gameOver = gameOver + score;
  148. }
  149.  
  150. if (numMines == 0 && gameOver == 0) { // Reveal 3x3
  151. reveal_square(minefield, rowOrCol, colOrRow);
  152. int score = game_check(gameOver, minefield);
  153. gameOver = gameOver + score;
  154. }
  155.  
  156. }
  157. if (integer == GAMEPLAY_MODE) {
  158. printf("Gameplay mode activated\n");
  159. gamePlayMode = 1;
  160. }
  161. if (integer == DEBUG_MODE) {
  162. printf ("Debug mode activated\n");
  163. gamePlayMode = 0;
  164. }
  165. if (integer == REVEAL_RADIAL) {
  166. int rowX;
  167. int colY;
  168. scanf(" %d %d", &rowX, &colY);
  169. reveal_radial(minefield, rowX, colY);
  170. }
  171.  
  172. // Printing the minefield
  173. if (gamePlayMode == 0) {
  174. print_debug_minefield(minefield);
  175. } else {
  176. gameplay_mode(minefield, gameOver, gameLost);
  177. }
  178.  
  179. // A game ends when the player wins, loses, or enters EOF (Ctrl+D).
  180. // You should display the minefield after each command has been processed.
  181. }
  182. return 0;
  183. }
  184.  
  185. // Set the entire minefield to HIDDEN_SAFE.
  186. void initialise_field(int minefield[SIZE][SIZE]) {
  187. int i = 0;
  188. while (i < SIZE) {
  189. int j = 0;
  190. while (j < SIZE) {
  191. minefield[i][j] = HIDDEN_SAFE;
  192. j++;
  193. }
  194. i++;
  195. }
  196. }
  197.  
  198. // Print out the actual values of the minefield.
  199. void print_debug_minefield(int minefield[SIZE][SIZE]) {
  200. int i = 0;
  201. while (i < SIZE) {
  202. int j = 0;
  203. while (j < SIZE) {
  204. printf("%d ", minefield[i][j]);
  205. j++;
  206. }
  207. printf("\n");
  208. i++;
  209. }
  210. }
  211.  
  212. // Detect row or column
  213.  
  214. void detect_row_col(int integer, int minefield[SIZE][SIZE], int hintCounter) {
  215.  
  216. if (integer == DETECT_ROW) { // Detect Row
  217. int rowOrCol = 0;
  218. scanf(" %d", &rowOrCol);
  219. int counterRow = 0;
  220. int numberOfMines = 0;
  221. while (counterRow < SIZE) {
  222. if (minefield[rowOrCol][counterRow] == 2) {
  223. numberOfMines++;
  224. }
  225. counterRow++;
  226. }
  227. if (hintCounter >= 3) {
  228. printf("Help already used\n");
  229. } else {
  230. printf("There are %d mine(s) in row %d\n", numberOfMines, rowOrCol);
  231. }
  232. }
  233.  
  234. if (integer == DETECT_COL) { // Detect Column
  235. int rowOrCol = 0;
  236. scanf(" %d", &rowOrCol);
  237. int counterCol = 0;
  238. int numberOfMines = 0;
  239. while (counterCol < SIZE) {
  240. if (minefield[counterCol][rowOrCol] == 2) {
  241. numberOfMines++;
  242. }
  243. counterCol++;
  244. }
  245. if (hintCounter >= 3) {
  246. printf("Help already used\n");
  247. } else {
  248. printf("There are %d mine(s) in column %d\n", numberOfMines, rowOrCol);
  249. }
  250. }
  251.  
  252. }
  253.  
  254. // Detect 3x3 for number of mines.
  255.  
  256. int square_detect(int minefield[SIZE][SIZE], int sizeDetect, int rowOrCol, int colOrRow) {
  257. int numberOfMines = 0;
  258. int rowCounter = -1;
  259. while (rowCounter < sizeDetect - 1) {
  260. int colCounter = -1;
  261. while (colCounter < sizeDetect - 1) {
  262. // Valid input mechanism
  263. if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
  264. if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
  265. if (minefield[rowOrCol + rowCounter][colOrRow +
  266. colCounter] == HIDDEN_MINE){
  267. numberOfMines++;
  268. }
  269. }
  270. }
  271. colCounter++;
  272. }
  273. rowCounter++;
  274. }
  275. return numberOfMines;
  276. }
  277.  
  278. // Valid input check
  279.  
  280. int valid_check(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow, int numberOfMines) {
  281.  
  282. int rowCounter = -1;
  283. while (rowCounter < 2) { // Checking mechanism
  284. int colCounter = -1;
  285. while (colCounter < 2) {
  286. // Valid input mechanism
  287. if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
  288. if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
  289. if (minefield[rowOrCol + rowCounter][colOrRow +
  290. colCounter] == HIDDEN_MINE){
  291. numberOfMines++;
  292. }
  293. }
  294. }
  295. colCounter++;
  296. }
  297. rowCounter++;
  298. }
  299. return numberOfMines;
  300. }
  301.  
  302.  
  303. // Check to see if game is won
  304. int game_check(int gameOver, int minefield[SIZE][SIZE]) {
  305. int gameCounter = 0;
  306. if (gameOver == 0) {
  307. int untouchedSquare = 0;
  308. int gameRow = 0;
  309. while (gameRow < SIZE) {
  310. int gameCol = 0;
  311. while (gameCol < SIZE) {
  312. if (minefield[gameRow][gameCol] == HIDDEN_SAFE) {
  313. untouchedSquare++;
  314. }
  315. gameCol++;
  316. }
  317. gameRow++;
  318. }
  319. if (untouchedSquare == 0) {
  320. printf("Game Won!\n");
  321. gameCounter++;
  322. }
  323. }
  324. return gameCounter;
  325. }
  326.  
  327. // Reveal square
  328.  
  329. void reveal_square(int minefield[SIZE][SIZE], int rowOrCol, int colOrRow) {
  330.  
  331. int rowCounter = -1;
  332. while (rowCounter < 2) {
  333. int colCounter = -1;
  334. while (colCounter < 2) {
  335. if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
  336. if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
  337. minefield[rowOrCol + rowCounter][colOrRow +
  338. colCounter] = VISIBLE_SAFE;
  339. }
  340. }
  341. colCounter++;
  342. }
  343. rowCounter++;
  344. }
  345. }
  346.  
  347. // Gameplay mode activated
  348.  
  349. void gameplay_mode(int minefield[SIZE][SIZE], int gameOver, int gameLost) {
  350. if (gameLost == 0) {
  351. printf("..\n");
  352. printf("\\/\n");
  353. }
  354. printf(" 00 01 02 03 04 05 06 07\n");
  355. printf(" -------------------------\n");
  356. int i = 0;
  357. while (i < SIZE) {
  358. printf("0%d ", i);
  359. int j = 0;
  360. while (j < SIZE) {
  361. int numberOfMines = 0;
  362. int rowCounter = -1;
  363. while (rowCounter < 2) { // Checking mechanism
  364. int colCounter = -1;
  365. while (colCounter < 2) {
  366. // Valid input mechanism
  367. if (i + rowCounter >= 0 && i + rowCounter <= 7) {
  368. if (j + colCounter >= 0 && j + colCounter <= 7) {
  369. if (minefield[i + rowCounter][j +
  370. colCounter] == HIDDEN_MINE){
  371. numberOfMines++;
  372. }
  373. }
  374. }
  375. colCounter++;
  376. }
  377. rowCounter++;
  378. }
  379. if (j == 0) {
  380. printf("|");
  381. }
  382. if (minefield[i][j] == VISIBLE_SAFE && numberOfMines == 0) {
  383. printf(" ");
  384. } else if (minefield[i][j] == VISIBLE_SAFE && numberOfMines > 0) {
  385. printf("%.2d", numberOfMines);
  386. } else if (minefield[i][j] == HIDDEN_SAFE) {
  387. printf("##");
  388. } else if (minefield[i][j] == HIDDEN_MINE && gameLost == 1) {
  389. printf("()");
  390. } else if (minefield[i][j] == HIDDEN_MINE) {
  391. printf("##");
  392. }
  393. if (j == 7) {
  394. printf("|");
  395. } else {
  396. printf(" ");
  397. }
  398. j++;
  399. }
  400. printf("\n");
  401. i++;
  402. }
  403. printf(" -------------------------\n");
  404. }
  405.  
  406. // Mine check
  407.  
  408. int check_mines(int minefield[SIZE][SIZE], int rowX, int colY) {
  409.  
  410. int numberOfMines = 0;
  411. int rowCounter = -1;
  412. while (rowCounter < 2) { // Checking mechanism
  413. int colCounter = -1;
  414. while (colCounter < 2) {
  415. // Valid input mechanism
  416. if (rowX + rowCounter >= 0 && rowX + rowCounter <= 7) {
  417. if (colY + colCounter >= 0 && colY + colCounter <= 7) {
  418. if (minefield[rowX + rowCounter][colY +
  419. colCounter] == HIDDEN_MINE){
  420. numberOfMines++;
  421. }
  422. }
  423. }
  424. colCounter++;
  425. }
  426. rowCounter++;
  427. }
  428.  
  429. return numberOfMines;
  430. }
  431.  
  432. // Reveal radial
  433.  
  434. void reveal_radial(int minefield[SIZE][SIZE], int rowX, int colY) {
  435.  
  436. int numRow = 0;
  437. while (numRow < SIZE - rowX) { // South
  438. int numMines = check_mines(minefield, rowX, colY);
  439. if (numMines == 0) {
  440. minefield[rowX + numRow][colY] = VISIBLE_SAFE;
  441. }
  442. numRow++;
  443. }
  444.  
  445. /*
  446. int northCounter = 0;
  447. while (northCounter > -1 * rowX) { // North
  448. int numCol = 0;
  449. int numMines = check_mines(minefield, rowX, colY, numRow, numCol);
  450. if (numMines == 0) {
  451. if (minefield[rowX + numRow][colY + numCol] != HIDDEN_MINE) {
  452. minefield[rowX + numRow][colY + numCol] = VISIBLE_SAFE;
  453. }
  454. }
  455. northCounter--;
  456. }
  457.  
  458. while () { // East
  459.  
  460. }
  461. while () { // West
  462.  
  463. }
  464. while () { // North East
  465.  
  466. }
  467. while () { // North West
  468.  
  469. }
  470. while () { // South East
  471.  
  472. }
  473. while () { // South West
  474.  
  475. }
  476. */
  477. }
  478. /*
  479. int rowCounter = -1;
  480. while (rowCounter < 2) { // Checking mechanism
  481. int colCounter = -1;
  482. while (colCounter < 2) {
  483. // Valid input mechanism
  484. if (rowOrCol + rowCounter >= 0 && rowOrCol + rowCounter <= 7) {
  485. if (colOrRow + colCounter >= 0 && colOrRow + colCounter <= 7) {
  486. if (minefield[rowOrCol + rowCounter][colOrRow +
  487. colCounter] == HIDDEN_MINE){
  488. numberOfMines++;
  489. }
  490. }
  491. }
  492. colCounter++;
  493. }
  494. rowCounter++;
  495. }
  496. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement