Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include "lab6.h"
- #include <stdlib.h>
- #include <math.h>
- int main(void) {
- //define variables
- int noOfRows,noOfCols, rows, cols;
- double probability, randProb;
- //user inputs size of board
- printf("Enter the number of rows: ");
- scanf("%d", &noOfRows);
- printf("Enter the number of columns: ");
- scanf("%d", &noOfCols);
- printf("Enter the probability p value: ");
- scanf("%lf", &probability);
- int const MAX_ROWS = noOfRows+2;
- int const MAX_COLS = noOfCols+2;
- char gridArray[ MAX_ROWS ][ MAX_COLS ];
- //for loop to iterate through each row
- for (rows = 0; rows <= noOfRows + 1; rows++) {
- for (cols = 0; cols <= noOfCols+1; cols++) {
- if (rows == 0 || rows == noOfRows + 1 || cols == 0 || cols == noOfCols +1 ) {
- gridArray[ rows ] [ cols ] = '.';
- }
- else {
- randProb = rand() % 101 * 0.01;
- if (randProb < probability) {
- gridArray[ rows ] [ cols ] = '*';
- }
- else {
- gridArray[ rows ] [ cols ] = '.';
- }
- printf(" %c", gridArray[ rows ][ cols ]);
- }
- }
- printf("\n");
- }
- //check for number of bombs surrounding a dot
- for (rows = 1; rows <= noOfRows; rows++) {
- for (cols = 1; cols <= noOfCols; cols++) {
- int count = 0;
- if (gridArray[ rows ] [ cols ] == '.') {
- int i, j;
- for (i = rows-1; i <= rows+1; i++) {
- for (j = cols-1; j <= cols+1; j++) {
- if (gridArray[ i ] [ j ] == '*'){
- count++;
- }
- }
- }
- gridArray[ rows ] [ cols ] = count + 48;
- }
- //gridArray[ rows ] [ cols ] = count + 48;
- printf(" %c", gridArray[ rows ][ cols ]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement