C language minesweeper game implementation code
- 2020-11-26 18:52:58
- OfStack
This article examples for you to share C language minesweeper game specific code, for your reference, specific content is as follows
The realization of minesweeper game
1. game. h module
The code implementation is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
// Initialize the
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
// Printed board
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int row,int col);
//1 Fixed programming logic +
// An array of -2 Dimensional array
// function - Combination in 1 since - function
// cycle - variable
2. game. c block:
The code implementation is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
#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;
printf("----------------------------------\n");
// Print column Numbers
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// every 1 The line number at the front of the line
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)
{
//1. Randomly find coordinates to arrange mines
// How many mines - 10
int count = EASY_COUNT;
while (count)
{
// Decorate a success 1 Ray, count--
//1. Produce random coordinates
int x = rand()%row+1;//1-9
int y = rand()%col+1;//1-9
//2. decorate
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(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';
}
//
// How did the minesweeper end?
//1. killed
//2. We checked all the non-ray locations
//
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 you want to check :>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
// judge x,y Is it thunder at the coordinates
if (mine[x][y] == '1')
{
printf(" I'm sorry you were blown up \n");
DisplayBoard(mine, row, col);
break;
}
else
{
// if x,y Coordinates are not mines, just count the number of mines around
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf(" Coordinates the illegal \n");
}
}
if (win == ROW*COL - EASY_COUNT)
{
printf(" Congratulations on your success in demining \n");
DisplayBoard(mine, row, col);
}
}
3. test. c module
The code implementation is as follows:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**********************\n");
printf("**** 1. play ***\n");
printf("**** 0. exit ***\n");
printf("**********************\n");
}
void game()
{
// The actual process of mine clearance
// create 2 An array
// Store arranged mines
char mine[ROWS][COLS] = {0};//'0'
// Store information about the ray that was discovered
char show[ROWS][COLS] = {0};//'*'
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
// Lay out ray's information should not be printed easily
//DisplayBoard(mine, ROW, COL);
//1. Decorate a ray
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
//2. Mine clearance
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
//time_t -- plastic
srand((unsigned int)time(NULL));// Set the starting point of random number generation
do
{
menu();
printf(" Please select a :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();// minesweeper
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice! \n");
break;
}
//
} while (input);
return 0;
}
More interesting classic games to realize the special topic, to share with you:
C++ classic game summary
python classic game summary
python Tetris game collection
JavaScript classic games don't stop playing
java classic game summary
javascript classic game summary