<> Minesweeping source code
Compiling environment : I'm using VS2013, More advanced versions are also available .
Create three files , Header file game.h, Two source files game.c and test.c, Header file game.h It's mainly about the various declarations of functions , source file game.c In this paper, the realization of mine sweeping algorithm is introduced , source file test.c It's mainly main function , The body of a function .
test.c file
#include "game.h" void menu() {
printf("**************************************\n"); printf("************ 1.
play **********\n"); printf("************ 0. exit *********\n");
printf("**************************************\n"); } void game() { // Information storage of mine
//1. Set up the information of the thunder char mine[ROWS][COLS] = { 0 };//11*11 //2. Information of the investigated mines char
show[ROWS][COLS] = { 0 }; // initialization InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*'); // Printing chessboard //DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL); // Lay out the thunder SetMine(mine, ROW, COL); // mine clearance
DisplayBoard(mine, ROW, COL); FindMine(mine, show, ROW, COL); } void test() {
int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf(" Please select :>");
scanf("%d", &input); switch (input) { case 1: game(); break; case 0:
printf(" Quit the game \n"); break; default: printf(" Wrong choice , Please select again "); break; } } while
(input); } int main() { test(); return 0; }
game.h file
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #include
<stdio.h> #include <stdlib.h> #include <time.h> // The number of Mines #define EASY_COUNT 10
void InitBoard(char board[ROW][COL], int row, int col, char set); void
DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char
board[ROWS][COLS], int row, int col); void FindMine(char mine[ROWS][COLS], char
show[ROWS][COLS], int row, int col); void SearchMine(char mine[ROWS][COLS],
char show[ROWS][COLS], int i, int j);
game.c
#include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols,
char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j <
cols; j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS],
int row, int col) { int i = 0; int j = 0; // Print column number for (i = 0; i <= col; i++) {
printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ",
i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); }
} void SetMine(char board[ROWS][COLS], int row, int col) { int count =
EASY_COUNT; while (count) { int x = rand() % row + 1;//1-9 int y = rand() % col
+ 1;//1-9 if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } int
get_mine_count(char mine[ROWS][COLS],int x,int y) { return mine[x - 1][y] +
mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0'; } void
FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int
x = 0; int y = 0; int win = 0; while (win<row*col - EASY_COUNT) {
printf(" Please enter the coordinates of the mine :>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row &&y >= 1
&& y <= col) { // Coordinate method //1. Stepping on thunder if (mine[x][y] == '1') { printf(" unfortunately , You were killed in the blast !\n");
DisplayBoard(mine, row, col); break; } else// It's not ray { // calculation x,y How many mines are there around the coordinates int count =
get_mine_count(mine, x, y); if (count == 0) { SearchMine(mine, show, x, y);
DisplayBoard(show, row, col); } else { show[x][y] = count + '0';
DisplayBoard(show, row, col); } } } else { printf(" Please enter legal coordinates , Please re-enter !\n"); } win =
win_count(show, ROW, COL); } if (win == row*col - EASY_COUNT) {
printf(" Congratulations on your success !\n"); DisplayBoard(mine, row, col); } } int win_count(char
show[ROWS][COLS],char row,char col) { int count = 0; int i = 0; int j = 0; for
(i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { //if (show[i][j] == ' '
|| show[i][j] >= 1 || show[i][j] <= 8) if (show[i][j] != '*') { count++; } } }
return count; } // Expand function void SearchMine(char mine[ROWS][COLS], char
show[ROWS][COLS], int i, int j) { if (i < 1 || i > ROW || j < 1 || j > COL) {
return; } show[i][j] = ' '; int posArr[8][2] = { { i - 1, j - 1 }, { i - 1, j
}, { i - 1, j + 1 }, { i, j - 1 }, { i, j + 1 }, { i + 1, j - 1 }, { i + 1, j
}, { i + 1, j + 1 } }; for (int k = 0; k < 8; k++) { int row = posArr[k][0];
int col = posArr[k][1]; // What has been explored if (show[row][col] >= '1' && show[row][col] <=
'8' || show[row][col] == ' ') { continue; } //1. If there is a circle of thunder in this direction , The number of mines is displayed , Stop searching
//2. If there is no thunder in this direction , Then recursion continues to search around int mineNum = 0; mineNum = get_mine_count(mine, row,
col); if (mineNum > 0) { //show[i - 1][j - 1] = mineNum + '0';
//DisplayBoard(show, row, col); show[row][col] = mineNum + '0'; } else {
SearchMine(mine, show, row, col); } } }
Technology