Minesweeper (2D Array - Dynamic Memory Allocation)

// done by zephinzer // goto: http://zephinzer.tumblr.com for more // please leave this intact if you use this code // thank you #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <time.h> // modify this to change the mine's character #define RAW_MINE '*' // a macro function for converting numbers to ascii characters // *** do not modify *** #define ASCII(x) x+48 #define MINE RAW_MINE-48 // function prototypes /* binary_rand() generates a 1 or a 0 using rand() seed rand with time(NULL) before use */ int binary_rand(); /* minesweeperize(int [][FIELD_Y)]; processes entire matrix and fills 0s with numbers corresponding to the number of mines surrounding it */ void minesweeperize(int**, int, int); int main(void) { printf("\n2darrays_dynalloc_minesweeper.c by zephinzer"); printf("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); // get user input int counter, x, y; unsigned int rows = 0, cols = 0; printf(" Enter the number of rows: "); scanf("%u", &rows); printf("Enter the number of colums: "); scanf("%u", &cols); while(rows<1||cols<1) { printf("Number of rows or columns cannot be below 1.\nPlease try again...\n"); printf(" Enter the number of rows: "); scanf("%u", &rows); printf("Enter the number of colums: "); scanf("%u", &cols); } printf("Allocating memory for a %d by %d grid now...\n", rows, cols); // this allocates the memory according to numbers stored in rows/cols int **arr2d = (int**)malloc(rows*sizeof(int*)); for(counter=0; counter<rows; counter++) arr2d[counter] = (int*)malloc(cols*sizeof(int)); printf("Initializing the random grid now...\n"); // this initializes contents of all cells using random numbers srand(time(NULL)); for(x=0; x<rows; x++) for(y=0; y<cols; y++) arr2d[x][y] = binary_rand(); printf("Printing the generated grid now...\n"); // this prints the 2D array onto the console for(x=0; x<rows; x++) { for(y=0; y<cols; y++) printf("%d ", arr2d[x][y]); putch('\n'); } printf("Filling zeroes with a number corresponding to surrounding ones...\n"); minesweeperize(arr2d, rows, cols); printf("Printing the finalized grid now...\n"); // this prints the 2D array onto the console again for(x=0; x<rows; x++) { for(y=0; y<cols; y++) printf("%c ", ASCII(arr2d[x][y])); putch('\n'); } printf("%c - represents a mine\n", ASCII(MINE)); printf("0-8 - represents number of mines around this position\n\n"); printf("Freeing allocated memory for the grid... "); while(counter--) free(arr2d[counter]); free(arr2d); printf("Done. Goodbye.\n"); printf("Press any key to continue..."); getch(); putch('\n'); return 0; } /* binary_rand() generates a 1 or a 0 using rand() seed rand with time(NULL) before use */ int binary_rand() { return ((float)rand()/RAND_MAX>0.5)?1:0; } /* minesweeperize(int [][FIELD_Y)]; processes entire matrix and fills 0s with numbers corresponding to the number of mines surrounding it */ void minesweeperize(int **arr, int x, int y) { int counter_x, counter_y, sum; int **field = (int**)malloc(x*sizeof(int*)); for(sum=0; sum<x; sum++) field[sum] = (int*)malloc(y*sizeof(int)); for(counter_x=0; counter_x<x; counter_x++) for(counter_y=0; counter_y<y; counter_y++) field[counter_x][counter_y] = 0; // go through entire matrix for(counter_x=0; counter_x<x; counter_x++) { for(counter_y=0; counter_y<y; counter_y++) { sum = 0; if(arr[counter_x][counter_y]==0) { // count above if(counter_x>0) sum += arr[counter_x-1][counter_y]; // count below if(counter_x<x-1) sum += arr[counter_x+1][counter_y]; // count the left if(counter_y>0) sum += arr[counter_x][counter_y-1]; // count the right if(counter_y<y-1) sum += arr[counter_x][counter_y+1]; // count the top left if(counter_x>0&&counter_y>0) sum+= arr[counter_x-1][counter_y-1]; // count the top right if(counter_x>0&&counter_y<y-1) sum += arr[counter_x-1][counter_y+1]; // count the bottom left if(counter_x<x-1&&counter_y>0) sum += arr[counter_x+1][counter_y-1]; // count the bottom right if(counter_x<x-1&&counter_y<y-1) sum += arr[counter_x+1][counter_y+1]; } field[counter_x][counter_y] = sum>9?6:sum; } } // check values in original array, if it is one, ignore // else change the value to that of field for(counter_x=0; counter_x<x; counter_x++) for(counter_y=0; counter_y<y; counter_y++) if(!arr[counter_x][counter_y]) arr[counter_x][counter_y] = field[counter_x][counter_y]; else arr[counter_x][counter_y] = MINE; for(sum=0; sum<x; sum++) free(field[sum]); free(field); }
This program simulates the Minesweeper game.
Concepts include:
Random number generation
2D array dynamic memory allocation
2D array manipulation

~ zephinzer
- http://zephinzer.tumblr.com

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.