Minesweeper (2D Array - Static 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 <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 // // // alter these values to see what x and y represents #define FIELD_X 15 // number of rows? #define FIELD_Y 15 // number of columns? // alter these values to see what x and y represents // // // 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 [][FIELD_Y]); int main(void) { printf("\n2darrays_hardalloc_minesweeper.c by zephinzer"); printf("\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); // initialization int field[FIELD_X][FIELD_Y]; printf("Created a grid of %d rows x %d columns\n", FIELD_X, FIELD_Y); srand(time(NULL)); printf("Seeded random number with time(NULL)\n"); printf("Initializing a random grid now...\n"); int counter_x, counter_y; // this initializes contents of all cells using random numbers for(counter_x=0; counter_x<FIELD_X; counter_x++) for(counter_y=0; counter_y<FIELD_Y; counter_y++) field[counter_x][counter_y] = binary_rand(); printf("Printing the generated grid now...\n"); // this prints the 2D array onto the console for(counter_x=0; counter_x<FIELD_X; counter_x++) { for(counter_y=0; counter_y<FIELD_Y; counter_y++) printf("%d ", field[counter_x][counter_y]); printf("\n"); } // do the deed printf("Filling zeroes with a number corresponding to surrounding ones...\n"); minesweeperize(field); printf("Printing the finalized grid now...\n"); // this prints the 2D array onto the console again for(counter_x=0; counter_x<FIELD_X; counter_x++) { for(counter_y=0; counter_y<FIELD_Y; counter_y++) printf("%c ", ASCII(field[counter_x][counter_y])); printf("\n"); } printf("%c - represents a mine\n", ASCII(MINE)); printf("0-8 - represents number of mines around this position\n"); printf("Press any key to continue..."); getch(); putch('\n'); // chaoz 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[][FIELD_Y]) { int field[FIELD_X][FIELD_Y]; int counter_x, counter_y, sum; // go through entire matrix for(counter_x=0; counter_x<FIELD_X; counter_x++) { for(counter_y=0; counter_y<FIELD_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<FIELD_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<FIELD_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<FIELD_Y-1) sum += arr[counter_x-1][counter_y+1]; // count the bottom left if(counter_x<FIELD_X-1&&counter_y>0) sum += arr[counter_x+1][counter_y-1]; // count the bottom right if(counter_x<FIELD_X-1&&counter_y<FIELD_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<FIELD_X; counter_x++) for(counter_y=0; counter_y<FIELD_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; }
This program simulates the generation of a minefield for the Minesweeper game.
Covers the following concepts:
2D arary initialization using #defines
2D array manipulation
Random number generation

~ 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.