“This is my seventh day of the first Gwen Challenge 2022.First challenge in 2022”
Writing in the front
We have already written three pieces of chess game certainly did not enjoy playing, let’s write a minesweeper game!
@TOC
Know the game
I believe you are familiar with mine clearance! Every computer must have a small game!
The rules of the game
Is in the specified time to bury all the mines found, that is, the game victory!
Victory condition: You need to mark all the mines as quickly as possible without making a mistake, and if you make a mistake, you have to start all over again, so there is a certain element of luck in minesweeper.
Simply put, mark all the places that are not mines, and win the game. If the mark points to mines, the game fails!
How do you find out if it’s not a landmine? Each point opens a small grid, if it is thunder, the game is over, if not, there will be a number, the number of the number of lightning around the circle! 2
Denotes the number of mines in the surrounding circle2
A. According to these numbers, we can remove all mines and lead to victory!
The game framework
The framework is the same as tic-tac-toe!
// Game frame
int input = 0;
do
{
menu();/ / the menu
scanf("%d", &input); / / select
switch (input)
{
case 1:game();
break;
case 0:printf("Quit the game \n");
break;
default:printf("Wrong input, please retype \n");
break; }}while (input);
Copy the code
This is basically how we write small games!
The game to achieve
The implementation of the game is the focus of our game! How do we make it happen? Start with the principle!
- mine
How to buy mine? The minesweeper we play with is a matrix of many small cells! So we can use two-dimensional arrays to bury landmines!
- Displays the number of mines around the point
The calculation of the number of lightning around, is to judge the change point around is not thunder, and calculate the number of display to the player!
- Judge the State of the game
Click on the mines to indicate the end of the game, all mines will be found, the game victory.
The idea of using a two-dimensional array to bury mines and another two-dimensional array to display to the player
When we make thunderAxA
The mine array hides the mine, will find that if we carry out the lookup of the number of mines around is the array out of bounds, so we can create(A+2)x(A+2)
A minefield, and then a minefieldAxA
In the!
void game(a)
{
// Initialize the checkerboard array
// Set mines -- > Display board -- > Player minesweeper
/ / mine
char mine[ROWS][COLS] = { 0 }; / / a minefield
char show[ROWS][COLS] = { 0 }; / / player
// Initialize the checkerboard
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS, The '*');
// Set the mine
SetMine(mine, ROW, COL,MineCount);
PrintBoard(show, ROW, COL);
// Player minesweeper
int count = ROW*COL-MineCount;
while (CheckMine(mine,show, ROW, COL))
{
count--;
PrintBoard(show, ROW, COL);
if (count == 0)
{
printf("Congratulations, game win \n");
PrintBoard(mine, ROW, COL);
break; }}}Copy the code
// Initialize the checkerboard
void InitBoard(char board[ROW][COL], int row, int col, char x)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++) { board[i][j] = x; }}return ;
}
Copy the code
// Print the checkerboard
void PrintBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
// Print the column number
for (i = 0; i <= col; i++)
printf(" %d ", i);
printf("\n");
for (i = 0; i < row; i++)
{
printf(" %d ", i+1);
int j = 0;
for (j = 0; j <col; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
return;
}
Copy the code
/ / mine
void SetMine(char board[ROW][COL], int row, int col, int count)
{
while (count)
{
int i = rand() % row;
int j = rand() % col;
if (board[i][j] == '0')
{
board[i][j] = '1'; count--; }}}Copy the code
The core code
/ / check ray
int CheckMine(char mine[ROW][COL],char show[ROW][COL], int row, int col)
{
printf("Please enter the coordinates you want to mine: >");
while (1)
{
int i = 0, j = 0;
scanf("%d%d", &i, &j);
if (i >=1 && i <= row && j >=1 && j <= col)
{
if (mine[i- 1][j- 1] != '1')
{
int count=IsMineCount(mine,i- 1,j- 1);
show[i- 1][j- 1] = count+'0';
return 1;
}
else
{
printf("I'm sorry you were blown up \n");
PrintBoard(mine, ROW, COL);
return 0; }}}}int IsMineCount(char mine[ROW][COL], 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++)
{
if (mine[i][j] == '1') { count++; }}}return count;
}
Copy the code
Results show
All the code
test.c
file
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu(a)
{
printf("******************************\n");
printf("********** 1.play ******\n");
printf("********** 0.exit ******\n");
printf("******************************\n");
printf("Input(1/0):>");
}
void game(a)
{
// Initialize the mine array -- > initialize the checkerboard array -- > Set the mine
// -- > display board -- > Player minesweeper
/ / mine
char mine[ROWS][COLS] = { 0 }; / / a minefield
char show[ROWS][COLS] = { 0 }; / / player
// Initialize the checkerboard
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS, The '*');
// Set the mine
SetMine(mine, ROW, COL,MineCount);
PrintBoard(show, ROW, COL);
// Player minesweeper
int count = ROW*COL-MineCount;
while (CheckMine(mine,show, ROW, COL))
{
count--;
PrintBoard(show, ROW, COL);
if (count == 0)
{
printf("Congratulations, game win \n");
break; }}}Copy the code
game.h
file
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MineCount 6
void InitBoard(char board[ROWS][COLS],int row,int col,char x);
void SetMine(char board[ROWS][COLS], int row, int col,int count);
void PrintBoard(char board[ROW][COL], int row, int col);
int CheckMine (char mine[ROWS][COLS], char show[ROW][COL], int row, int col);
int IsMineCount(char mine[ROWS][COLS],int i, int j);
Copy the code
game.c
file
#include"game.h"
void InitBoard(char board[ROW][COL], int row, int col, char x)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++) { board[i][j] = x; }}return ;
}
void SetMine(char board[ROW][COL], int row, int col, int count)
{
while (count)
{
int i = rand() % row;
int j = rand() % col;
if (board[i][j] == '0')
{
board[i][j] = '1'; count--; }}}void PrintBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
// Print the column number
for (i = 0; i <= col; i++)
printf(" %d ", i);
printf("\n");
for (i = 0; i < row; i++)
{
printf(" %d ", i+1);
int j = 0;
for (j = 0; j <col; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
return;
}
int CheckMine(char mine[ROW][COL],char show[ROW][COL], int row, int col)
{
printf("Please enter the coordinates you want to mine: >");
while (1)
{
int i = 0, j = 0;
scanf("%d%d", &i, &j);
if (i >=1 && i <= row && j >=1 && j <= col)
{
if (mine[i- 1][j- 1] != '1')
{
int count=IsMineCount(mine,i- 1,j- 1);
show[i- 1][j- 1] = count+'0';
return 1;
}
else
{
printf("I'm sorry you were blown up \n");
return 0; }}}}int IsMineCount(char mine[ROW][COL], 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++)
{
if (mine[i][j] == '1') { count++; }}}return count;
}
Copy the code