Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "lab6.h"
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. int main(void) {
  7.    
  8.     //define variables
  9.     int noOfRows,noOfCols, rows, cols;
  10.    
  11.     double probability, randProb;
  12.    
  13.     //user inputs size of board
  14.     printf("Enter the number of rows: ");
  15.     scanf("%d", &noOfRows);
  16.     printf("Enter the number of columns: ");
  17.     scanf("%d", &noOfCols);
  18.     printf("Enter the probability p value: ");
  19.     scanf("%lf", &probability);
  20.    
  21.     int const MAX_ROWS = noOfRows+2;
  22.     int const MAX_COLS = noOfCols+2;
  23.    
  24.     char gridArray[ MAX_ROWS ][ MAX_COLS ];
  25.    
  26.     //for loop to iterate through each row
  27.     for (rows = 0; rows <= noOfRows + 1; rows++) {
  28.         for (cols = 0; cols <= noOfCols+1; cols++)    {
  29.             if (rows == 0 || rows == noOfRows + 1 || cols == 0 || cols == noOfCols +1 ) {
  30.                 gridArray[ rows ] [ cols ] = '.';
  31.             }
  32.             else {
  33.                 randProb = rand() % 101 * 0.01;
  34.                 if (randProb < probability) {
  35.                     gridArray[ rows ] [ cols ] = '*';
  36.                 }
  37.                 else {
  38.                     gridArray[ rows ] [ cols ] = '.';
  39.                 }
  40.                 printf(" %c", gridArray[ rows ][ cols ]);
  41.             }
  42.         }
  43.         printf("\n");
  44.        
  45.     }
  46.    
  47.    
  48.     //check for number of bombs surrounding a dot
  49.     for (rows = 1; rows <= noOfRows; rows++) {
  50.         for (cols = 1; cols <= noOfCols; cols++)  {
  51.             int count = 0;
  52.             if (gridArray[ rows ] [ cols ] == '.')  {
  53.                 int i, j;
  54.                 for (i = rows-1; i <= rows+1; i++) {
  55.                     for (j = cols-1; j <= cols+1; j++) {
  56.  
  57.                         if (gridArray[ i ] [ j ] == '*'){
  58.                             count++;
  59.                         }
  60.                     }
  61.                 }
  62.                 gridArray[ rows ] [ cols ] = count + 48;
  63.             }
  64.             //gridArray[ rows ] [ cols ] = count + 48;
  65.             printf(" %c", gridArray[ rows ][ cols ]);
  66.         }
  67.         printf("\n");
  68.     }
  69.    
  70.    
  71.     return 0;
  72.    
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement