catalogue
Project introduction
Like Sanzi, this project can be easily dealt with after learning functions and arrays.
Rules of mine sweeping: enter the coordinates of a grid randomly, and a number will appear on the grid. The number above represents the number. There will be several mines in the eight grids around. It is 1. There is only one mine around him, and there are two mines in 2······
In this project, we take the 9 * 9 grid as an example, in which there are 10 mines (of course, you can adjust it appropriately according to your needs).
File creation
First create three source files, test c,game.c,game.h
menu
Like Sanzi, in test Create a menu in the C source file. Because the game may be completed more than once, here we use the do while loop. In the menu, you can add the battle mode you want, and then add the corresponding options in the switch case statement below.
For example, select 1 to enter the man-machine mode, and select 0 to exit the game, because the loop condition in while is input and the condition is false, so the loop will end naturally.
test.c
void menu() { printf("*****************************\n"); printf("************1.play***********\n"); printf("************0.exit***********\n"); printf("*****************************\n"); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please select:>"); scanf("%d", &input); switch (input) { case 1: printf("Start the game\n"); game(); break; case 0: printf("Exit the game\n"); break; default: printf("Selection error,Please reselect\n"); break; } } while (input); return 0; }
Create chessboard
Then the problem comes. If we create a chessboard with a size of 9 * 9, we need to check the surrounding 8 squares when displaying the number of surrounding mines. If the array crosses the boundary, we must limit the scope of investigation when checking the squares of each boundary, which is too troublesome. For example, in these locations.
So we simply initialize the size of the chessboard to 11 * 11 when creating the chessboard, but play the game in the size of 9 * 9, so we don't have to worry about crossing the boundary.
test.c
#pragma once #include<stdio.h> #define ROW 9 / / minesweeping line #define COL 9 / / minesweeping column #define ROWS ROW+2 / / chessboard row #define COLS COL+2 / / checkerboard column
test.c
void game() { //Two binary arrays are created here //One for storing information on mine placement //Another chessboard for display to the player char mine[ROWS][COLS];//Store mine information char show[ROWS][COLS];//Store the information of the detected mine }
Initialize chessboard
When no mines are deployed, initialize all checkerboards storing mine information to 0, and initialize all checkerboards displayed to players to '*' to cover the checkerboard.
game.h
//Initialize chessboard void InitBoard(char board[ROWS][COLS], int rows,int cols,char set);
game.c
//Initialize chessboard 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; } } }
test.c
void game() { char mine[ROWS][COLS];//Store mine information char show[ROWS][COLS];//Store the information of the detected mine //Initialize chessboard InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); }
Print chessboard
When arranging mines, we can print the mine laying chessboard for our maintainers to observe; At the same time, thunder should also print the display chessboard on the screen every time the player rows. (ps: there is no need to print out the grid here)
game.h
//Print chessboard void DisplayBoard(char board[ROWS][COLS], int row, int col);
game.c
//Print chessboard void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; for (i = 0; i <= col; i++) { printf("%d ", i);//Print column sequence number } printf("\n"); for (i = 1; i <= row; i++)//In order to meet the player experience, both rows and columns start from 1 { int j = 0; printf("%d ", i);//Print line serial number for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); }
test.c
void game() { char mine[ROWS][COLS];//Store mine information char show[ROWS][COLS];//Store the information of the detected mine //Initialize chessboard InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //Print chessboard DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); }
Now we have a minesweeping chessboard:
Lay mines
With the chessboard, now we can bury mines. Like the random chess played by the computer in Sanzi chess, here we also bury 10 mines in the chessboard at random.
game.h
//Number of Mines #define EASY_COUNT 10 //Lay mines void SetMine(char board[ROWS][COLS], int row, int col);
game.c
//Lay mines void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT;//Set 10 mines while (count) { //Randomly generate coordinates within the chessboard range int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1')//It is necessary to judge whether mines have been laid at this location { board[x][y] = '1';//Use '1' to indicate that there are mines in this position count--; } } }
test.c
void game() { char mine[ROWS][COLS];//Store mine information char show[ROWS][COLS];//Store the information of the detected mine //Initialize chessboard InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //Print chessboard DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); //Lay thunder SetMine(mine, ROW, COL); DisplayBoard(mine, ROW, COL); }
Here, let's test the mines:
Demining
After the foreshadowing, we can now complete the player's demining procedure.
game.h
//mine clearance void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c
int GetMineCount(char mine[ROWS][COLS], int x, int y)//Check the number of mines around the coordinates { int count = 0; int i = 0; for (i = x - 1; i <= x + 1; i++)//The range is a 3 * 3 grid centered on coordinates { int j = 0; for (j = y - 1; j <= y + 1; j++) { count += mine[i][j] - '0'; } } return count; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0;//Abscissa int y = 0;//Ordinate int win = 0;//Number of checked grids while (win<row*col-EASY_COUNT) { printf("Please enter the coordinates to check:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1')//Step on a mine { printf("----------------------\n"); printf("I'm sorry you were killed\n"); printf("----------------------\n"); DisplayBoard(mine, ROW, COL);//At the end of the game, print out the chess board arranged by Lei break; } else { int count = GetMineCount(mine,x,y); show[x][y] = count + '0';//There are characters in the grid DisplayBoard(show, ROW, COL);//Print the display chessboard every time win++; } } else { printf("Coordinate error,Please re-enter\n");//Coordinate out of bounds } } if (win == row * col - EASY_COUNT)//When the checked grid is equal to the total grid minus the number of mines //The number of grids is successful { printf("----------------------\n"); printf("congratulations,Demining succeeded!\n"); printf("----------------------\n"); DisplayBoard(mine, ROW, COL);//At the end of the game, print out the chess board arranged by Lei } }
test.c
void game() { char mine[ROWS][COLS];//Store mine information char show[ROWS][COLS];//Store the information of the detected mine //Initialize chessboard InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //Print chessboard DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); //Lay thunder SetMine(mine, ROW, COL); DisplayBoard(mine, ROW, COL); //mine clearance FindMine(mine, show, ROW, COL); }
Here, our game is basically completed.
Complete code
game.h
#pragma once #include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 void InitBoard(char board[ROWS][COLS], int rows,int cols,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);
game.c
#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; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; for (i = 0; i <= col; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); } void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } } int GetMineCount(char mine[ROWS][COLS], int x, int y) { int count = 0; int i = 0; for (i = x - 1; i <= x + 1; i++) { int j = 0; for (j = y - 1; j <= y + 1; j++) { count += mine[i][j] - '0'; } } return count; } 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 to check:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("----------------------\n"); printf("I'm sorry you were killed\n"); printf("----------------------\n"); DisplayBoard(mine, ROW, COL); break; } else { int count = GetMineCount(mine,x,y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("Coordinate error,Please re-enter\n"); } } if (win == row * col - EASY_COUNT) { printf("----------------------\n"); printf("congratulations,Demining succeeded!\n"); printf("----------------------\n"); DisplayBoard(mine, ROW, COL); } }
test.c
#include"game.h" void menu() { printf("*****************************\n"); printf("************1.play***********\n"); printf("************0.exit***********\n"); printf("*****************************\n"); } void game() { char mine[ROWS][COLS];//Store mine information char show[ROWS][COLS];//Store the information of the detected mine //Initialize chessboard InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //Print chessboard //DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); //Lay thunder SetMine(mine, ROW, COL); //DisplayBoard(mine, ROW, COL);// Don't print the Bray board when playing the game //mine clearance FindMine(mine, show, ROW, COL); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please select:>"); scanf("%d", &input); switch (input) { case 1: printf("Start the game\n"); game(); break; case 0: printf("Exit the game\n"); break; default: printf("Selection error,Please reselect\n"); break; } } while (input); return 0; }
Let's see how it works:
Have you learned?