Advertisement
brian_lee19

Untitled

Mar 28th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 KB | None | 0 0
  1. // Assignment 1 20T1 COMP1511: Minesweeper
  2. // minesweeper.c
  3. //
  4. // This program was written by Brian Lee (z5308686)
  5. // on 13-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. void row_detector(int userInput, int minefield[SIZE][SIZE]);
  36. void col_detector(int userInput, int minefield[SIZE][SIZE]);
  37. void square_detector(int rowSquare, int colSquare, int sizeSquare, int minefield[SIZE][SIZE]);
  38.  
  39. // Place your function prototyes here.
  40.  
  41. int main(void) {
  42.     int minefield[SIZE][SIZE];
  43.     int noMines = 0;
  44.     initialise_field(minefield);
  45.     printf("Welcome to minesweeper!\n");
  46.     printf("How many mines? ");
  47.     scanf("%d", &noMines);
  48.    
  49.     int row = 0;
  50.     int col = 0;
  51.     int i = 0;
  52.    
  53.     printf("Enter pairs:\n");
  54.     while(i < noMines) {
  55.         scanf("%d %d", &row, &col);
  56.         if(row >= 0 && col >= 0 && row <=SIZE && col <= SIZE) {
  57.             minefield[row][col] = HIDDEN_MINE;
  58.         }
  59.        
  60.         i++;
  61.     }
  62.     printf("Game Started\n");
  63.     print_debug_minefield(minefield);  
  64.    
  65.     int command = 0;
  66.     int userInput = 0;
  67.     int rowSquare = 0;
  68.     int colSquare = 0;
  69.     int sizeSquare = 0;
  70.    
  71.     while(scanf("%d", &command) == 1) {
  72.         if(command == DETECT_ROW) {
  73.             scanf("%d", &userInput);
  74.             row_detector(userInput, minefield);
  75.         }
  76.         if(command == DETECT_COL) {
  77.             scanf("%d", &userInput);
  78.             col_detector(userInput, minefield);
  79.         }
  80.         if(command == DETECT_SQUARE) {
  81.                 scanf("%d", &rowSquare);
  82.                 scanf("%d", &colSquare);
  83.                 scanf("%d", &sizeSquare);
  84.                 square_detector(rowSquare, colSquare, sizeSquare, minefield);
  85.             }
  86.         }
  87.     return 0;
  88. }
  89.  
  90. // Set the entire minefield to HIDDEN_SAFE.
  91. void initialise_field(int minefield[SIZE][SIZE]) {
  92.     int i = 0;
  93.     while (i < SIZE) {
  94.         int j = 0;
  95.         while (j < SIZE) {
  96.             minefield[i][j] = HIDDEN_SAFE;
  97.             j++;
  98.         }
  99.         i++;
  100.     }
  101. }
  102.  
  103. // Print out the actual values of the minefield.
  104. void print_debug_minefield(int minefield[SIZE][SIZE]) {
  105.     int i = 0;
  106.     while (i < SIZE) {
  107.         int j = 0;
  108.         while (j < SIZE) {
  109.             printf("%d ", minefield[i][j]);
  110.             j++;
  111.         }
  112.         printf("\n");
  113.         i++;
  114.     }
  115. }
  116.  
  117. // Detect Rows
  118. void row_detector(int userInput, int minefield[SIZE][SIZE]){
  119.     int rowScanner = 0;
  120.     int mineCounter = 0;
  121.    
  122.     while(rowScanner < SIZE) {
  123.         if (minefield[userInput][rowScanner] == HIDDEN_MINE) {
  124.             mineCounter++;
  125.         }
  126.         rowScanner++;
  127.     }
  128.     printf("There are %d mine(s) in row %d\n", mineCounter, userInput);
  129.     print_debug_minefield(minefield);
  130. }
  131.  
  132. // Detect Columns
  133. void col_detector(int userInput, int minefield[SIZE][SIZE]){
  134.     int colScanner = 0;
  135.     int mineCounter = 0;
  136.    
  137.     while(colScanner < SIZE) {
  138.         if (minefield[colScanner][userInput] == HIDDEN_MINE) {
  139.             mineCounter++;
  140.         }
  141.         colScanner++;
  142.     }
  143.     printf("There are %d mine(s) in column %d\n", mineCounter,          userInput);
  144.     print_debug_minefield(minefield);
  145. }
  146.  
  147. // Detect Squares
  148. void square_detector(int rowSquare, int colSquare, int sizeSquare, int minefield[SIZE][SIZE]) {
  149.     int mineSquareCounter = 0;    
  150.        
  151.     if(rowSquare >= 0 && colSquare >= 0 && rowSquare <=SIZE && colSquare <= SIZE) {
  152.         int i = rowSquare - (sizeSquare / 2);        
  153.         while(i < rowSquare + (sizeSquare / 2) + 1) {
  154.             int a = colSquare - (sizeSquare / 2);
  155.             while(a < colSquare + (colSquare / 2) + 1) {
  156.                 if(minefield[rowSquare][colSquare] == HIDDEN_MINE) {
  157.                 mineSquareCounter++;
  158.                 }
  159.                 a++;
  160.             }
  161.             i++;
  162.         }
  163.     }
  164.    
  165.     printf("There are %d mine(s) in the square centred at row %d, column %d of size %d", mineSquareCounter, rowSquare, colSquare, sizeSquare);
  166. }
  167. // Reveal Squares
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement