#include <stdio.h>
#include <stdlib.h>
#include <time.h>
unsigned const short int _HORIZON = 30 + 1;
unsigned const short int _VERTICAL = 20 + 1;
unsigned const short int _MINE = 800; // distribution
void examine_boom (int** , int** );
void print_space (int** , unsigned short int, unsigned short int);
int main(void) {
/* dynamic allocation */
int** space = calloc (_VERTICAL, sizeof (int*)); // dynamic allocation, vertical
int** emptySpace = calloc (_VERTICAL, sizeof (int*)); // dynamic allocation, vertical
unsigned short int horizon = 0, vertical = 0;
srand ((unsigned) time (NULL)); // random
for (vertical = 0; vertical < _VERTICAL; vertical++) {
space[vertical] = calloc (_HORIZON, sizeof (int)); // dynamic allocation, horizon
emptySpace[vertical] = calloc (_HORIZON, sizeof (int)); // dynamic allocation, horizon
}
/* create mine */
for (vertical = 0; vertical < _VERTICAL; vertical++) {
for (horizon = 0; horizon < _HORIZON; horizon++) {
if (vertical == 0 || horizon == 0 ||
vertical == _VERTICAL - 1 || horizon == _HORIZON - 1) // except edge
continue;
if ((rand() % (_HORIZON * _VERTICAL * 10) / _MINE) == 0)
space[vertical][horizon] = -1; // mine
else
space[vertical][horizon] = 0;
}
}
/*examine boom*/
examine_boom(space, emptySpace);
/* print */
print_space (emptySpace, horizon, vertical);
/* initialize dynamic allocation */
for (vertical = 0; vertical < _VERTICAL; vertical++) {
free (space[vertical]); // initialize horizon
}
free (space); // initialize vertical
return 0;
}
void examine_boom (int** space, int** emptySpace) {
unsigned short int horizon = 0, vertical = 0;
unsigned short int row = 0, column = 0, countMine = 0;
unsigned short int loop = 0;
signed short int mine = -1;
for (vertical = 0; vertical < _VERTICAL; vertical++) {
for (horizon = 0; horizon < _HORIZON; horizon++) {
if ( space[vertical][horizon] == mine ) {
emptySpace[vertical][horizon] = -1; // mine
continue;
}
if ( vertical == 0 || horizon == 0 ||
vertical == _VERTICAL - 1 || horizon == _HORIZON - 1) // except edge
continue;
countMine = 0; // initialize mine
row = horizon - 1;
column = vertical - 1;
for (loop = 0; loop < 3; loop++) { // 1 2 3
if (space[column][row++] == mine) // 4.4 5.4 6.4
countMine++;
if (space[column][row++] == mine) // 4.5 5.5 6.5
countMine++;
if (space[column++][row] == mine) // 4.6 5.6 6.6
countMine++;
row -= 2;
}
emptySpace[vertical][horizon] = countMine;
}
}
return;
}
void print_space (int** space, unsigned short int horizon, unsigned short int vertical) {
for (vertical = 0; vertical < _VERTICAL; vertical++) {
for (horizon = 0; horizon < _HORIZON; horizon++) {
if (vertical == 0 || horizon == 0 ||
vertical == _VERTICAL - 1 || horizon == _HORIZON - 1) // except edge
continue;
if (space[vertical][horizon] == -1)
printf("%c ", 42);
else
printf("%d ", space[vertical][horizon]);
}
printf("\n");
}
return;
}