In the last issue, we implemented the sanziqi game. In this issue, we explained mine sweeping. Let's take a look at the effect first: (personally, I think it is a little more difficult than sanziqi, because it is designed with two two-dimensional arrays, boundary settings and interface printing are more complex, but its code is not as much as sanziqi)
1 means ray, 0 means not ray.
1. First of all, let's pack the sections: test.c,game.c,game.h:
test.c: the basic logic of the game.
game.c: the specific implementation of game code.
game.h: header file to declare functions
2. Realize the interface of the game. Here, the dowhile loop and switch statement are used to skillfully use 0 as false to exit the game;
#include<stdio.h> void game() { //The concrete realization of the game; } void menu() { printf("**************************\n"); printf("****** 1.play ******\n"); printf("****** 0.exit ******\n"); printf("**************************\n"); } test() { int input = 0; do { menu(); printf("Please select:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("Exit the game\n"); break; default: printf("Input error, please re-enter!\n"); break; } } while (input); } int main() { test(); return 0; }
2. Let's realize the specific implementation of the game. First of all, understand the principle. We will judge whether it is thunder by using 1 and 0. When there is only one thunder in the surrounding 8 thunder, we don't know whether 1 represents thunder or there is one thunder in the surrounding 8. So here we need to create two two-dimensional arrays to store mine sweeping information. At the same time, in order to make the two arrays compatible, we define them as char. One is used to store the mine and the other is used to show it to the user. However, when we encounter the edge, there are no 8 around us, so we print 9x9 code, we need to create 11x11 grid, and only show users 9x9 grid. In the later stage, because we need to use rows and columns in many places, the header file definition (#define) ROW=9, COL=9, ROWS=11, COLS=11 .
Start initialization The mine array is 0, and the show array is defined as *: the header file should reference #include"game.h":. Here, the three sections should be controlled. I'll demonstrate it once, and then I'll show the contents of game.c. the same is true for others.
test.c release:
game.h:
game.c:
Code for initializing chessboard:
#include"game.h" void Initboard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { board[i][j] = set; } } }
3. Print the chessboard (it takes some effort here because there should be coordinates easy to confirm)
(1) Print 0-9
(2) Line feed
(3) Print line number + board[i][j]; (note that I and j should start from 1 to ensure boundary effectiveness)
(4) Line feed
The effect comes out.
The code in game.c is as follows:
void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; //Print 0-9 for (i = 0; i <= col; i++) { printf("%d ", i); } //Line feed printf("\n"); for (i = 1; i <= row; i++) { //Print line number printf("%d ", i); int j = 1; for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } //Line feed printf("\n"); } }
4. Lay out thunder (random numbers must be used for laying out thunder, so we put #include < time. H >, #include < stdlib. H > in the header file and test() function: srand((unsigned int)time(NULL));
), we need to set the number of mines here. Since we want to control the number of mines, we define EASY_COUNT in the header file is 10
X and Y in the value of mine[x][y] must be from 1 to 9, so the rand function is cleverly used to control 1-9. The code is as follows: relatively simple:
void SetMine(char mine[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } }
5. Demining
(1) Judge whether the input coordinates are normal
(2) Judge whether he is hit by thunder
(3) Zhonglei shows the location of Lei
(4) Start counting the number of people around without hitting the thunder:
Here, the ascll code value of the number + '0' is equal to the ascll code value of the 'number'. Similarly, 'number' - '0' equals number. We use a function get_mine_count to calculate the number of surrounding mines. At the same time, define a win variable, win + + without thunder, and then win = = row * col easy_ Count can exit the while cycle, mine clearance is successful, and the mine location is displayed. The code is as follows:
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 coordinates;>"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("I'm sorry you were killed!\n"); DisplayBoard(mine, row, col); break; } else { int n = get_mine_count(mine, x, y); show[x][y] = n + '0'; DisplayBoard(show, row, col); win++; } } else { printf("Illegal input, please re-enter!\n"); } } if (win == row * col - EASY_COUNT) { printf("Congratulations, mine clearance success!\n"); DisplayBoard(mine, row, col); } }
6.get_ mine_ Implementation of count
It has been said that 'number' - '0' is equal to number
Just return directly. The code is as follows
static int get_mine_count(char mine[ROWS][COLS], int x, int y) { return mine[x-1][y]+ mine[x-1][y-1]+ mine[x][y-1]+ mine[x+1][y-1]+ mine[x+1][y]+ mine[x+1][y+1]+ mine[x][y+1]+ mine[x-1][y+1]-8*'0'; }
All right, it's done.