preface

Last time, we mainly used the two-dimensional array and function in C language to realize Sanzi chess. This time, we use the two-dimensional array in C language to realize another very common minesweeping game.

thinking

Let's sort out the idea of the game from the perspective of a player:

1. Enter the interface and choose to start the game or leave the game

2. Entering the game, we see a chessboard

3. Select a location for mine clearance

4. The computer judges whether there is thunder at the position. If there is thunder at the end of the game, if the total number of thunder near the position is not marked on the chessboard

5. Repeat 3.4 until the game is over

Let's translate the above description into program ideas

1. The game interface appears and allows players to enter information for selection

2. Define two arrays, one for recording actual information and the other for displaying print information

3. Initialize two arrays and arrange them randomly

4. Print chessboard

5. Players choose location

6. The computer judges the situation of thunder: if it is the end of the thunder game, print the real situation of the chessboard. If it is not thunder, print the number of thunder near the position

7. Repeat 5 and 6 until the end of the game (the player is killed or all mineless positions are opened by the player)

code implementation

Now we can write code..... The framework has been written, and the code is as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
//Define two arrays, one to record the actual situation (mine) and the other to display the printing effect (show)
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//To initialize the array, we will fully initialize the character 0 for mine and fully initialize the character show*
Initboard(mine, ROWS, COLS, '0');
Initboard(show, ROWS, COLS, '*');
//Lay thunder
setmine(mine, ROW, COL);
//Print chessboard
display(show, ROW, COL);
//Players choose computer judgment
FindMine(show, mine, ROW, COL);

}
{
printf("*****************\n");
printf("****0----exit****\n");
printf("****1----play****\n");
printf("*****************\n");

}
void test()
{
srand((unsigned int)time(NULL));//To generate random numbers later
int input = 0;
do
{
//Player input
scanf("%d", &input);
//judge
switch (input)
{
case 0:printf("game over!");
break;
case 1:game();
break;
break;

}
} while (input);
}
int main()
{
test();
return 0;
}

The framework is still very simple. And gave detailed notes. If you don't understand, you can take a good look at the notes. There is a problem worthy of your attention, that is, when calling different functions, we will enter ROWS, COLS, ROW and COL for the actual parameters we enter. We will see the result later.

Let's write these functions implemented by the game The function code of the game is as follows:

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//First acquaintance
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;
}
}
}
//Function of layout ray
void setmine(char mine[ROWS][COLS], int row, int col)
{
int count = 10;//The value of count is the total number of thunder in the game
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] != '1')//Where there are already mines, no more mines can be arranged
{
mine[x][y] = '1';
count--;
}
}
}
//Functions for printing checkerboard
void display(char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
//In order to make the rows and columns clearer, we print them out
//Print column number
for (x = 0; x < 10; x++)
{
printf("%d ", x);
}
printf("\n");
for (x = 1; x <= row; x++)
{
//Print line number
printf("%d ", x);
for (y = 1; y <= col; y++)
{
printf("%c ", show[x][y]);
}
printf("\n");
}
}
//Calculate the total number of nearby mines
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x][y - 1] +
mine[x][y + 1] +
mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';

}
Location selection and judgment
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;//Use win to record the total number of mineless cells turned over
int s = 0;
//Only when there is a mine free position that has not been opened can it cycle
while (row * col - COUNT - win > 0)
{
scanf("%d %d", &x, &y);
// If the coordinates are out of bounds, re-enter them. If the coordinates are normal, it can be judged
if (x > 0 && x <= row && y > 0 && y <= col)
{
//Dig thunder
if (mine[x][y] == '1')
{
printf("I'm sorry you were killed!\n");
display(mine, row, col);
break;
}
//It is not a mine. The total number of mines near the location is displayed
else
{
show[x][y] = '0' + get_mine_count(mine, x, y);
display(show, ROW, COL);
win++;
}
}
else
{
printf("Lose wrong and start again!");
}
}
//After jumping out of the loop, win is used to judge whether to open all mineless positions, and then judge whether the player wins
if (row * col - COUNT - win == 0)
{
printf("You won!");
}
}

All the functions to be used in the game, library functions and global variables, we put them in the same header file. As follows:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
void setmine(char mine[ROWS][COLS], int row, int col);
void display(char show[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

Now let's explain the question just now: If we define the size of the chessboard just right, the positions of those edges will cross the boundary when calculating the number of surrounding mines. Therefore, in order to facilitate the calculation of the number of surrounding mines, we add two lines and two columns when defining the size of the array. Just set the edges of the chessboard to be mine free. In this way, the problem is well solved.

Operation results Tags: C

Posted on Thu, 11 Nov 2021 16:54:35 -0500 by jpraj