Minesweeper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #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; } | cs |
0 개의 댓글:
댓글 쓰기