"C Language" simple minesweeper game--c language realization

Source: Internet
Author: User
Tags rand


We often play on the computer minesweeper game, very test our judgment ability, but realize a mine-clearing game is not very difficult, as long as more attention to some details, it can be a simple minesweeper game to write out.

Here's what the minesweeper game is about:
First, to initialize the array, be aware of defining two arrays when initializing, one is the matrix that allows us to mine the mines, the other is the matrices that show the total number of mines around a certain place, and in order to avoid the problem of sending the address, we write it in the main function.

	Char Mine[rows][cols];
	Char Show[rows][cols];
	int i = 0;
	int j = 0;
	for (i = 0; i < rows-1. i++)
	{for
		(j = 0; J < Cols-1; j)
		{
			Mine[i][j] = ' 0 ';
			SHOW[I][J] = ' * ';
		}
	}

Then there is the computer's random layout of the function, which uses the rand () &nbsp; function to generate random values and to mine randomly in a minefield.

void Set_mine (char mine[rows][cols])
{
	int count = count;
	int x = 0;
	int y = 0;
	Srand ((unsigned) time (NULL));
	while (count)
	{
		x = rand ()% 9 + 1;
		y = rand ()% 9 + 1;
		if (mine[x][y] = = ' 0 ')
		{
			Mine[x][y] = ' 1 ';
			count--
		}}
	}

Then there is a function to calculate the number of thunder, to say that a coordinate position in the vicinity of the 8 points of the number of mines to calculate, and the number displayed

int Get_num (char mine[rows][cols], int x, int y)
{
	int count = 0;
	if (mine[x-1][y-1] = = ' 1 ')//upper left
	{
		count++
	}
	if (mine[x-1][y] = = ' 1 ')//left
	{
		count++
	}
	if (mine[x-1][y + 1] = = ' 1 ')//lower left
	{
		count++
	}
	if (mine[x][y-1] = = ' 1 ')//above
	{
		count++
	}
	if (mine[x][y + 1] = = ' 1 ')//below
	{
		count++
	}
	if (mine[x + 1][y-1] = = ' 1 ')//upper right
	{
		count++;
	}
	if (mine[x + 1][y] = = ' 1 ')//right
	{
		count++;
	}
	if (mine[x + 1][y + 1] = = ' 1 ')//lower right
	{
		count++;
	}
	return  count;
}

After you have implemented each function of the minesweeper function, let's look at the complete code

Header file Game.h

#define _crt_secure_no_warnings 1
#include <stdio.h>
#include <stdlib.h>
#include < time.h>
#include <string.h>
#define ROWS
#define COLS
#define COUNT

int menu (); /Menu function
void display (char show[rows][cols]);
int Game (char Mine[rows][cols],char show[rows][cols]);//game
void Set_mine (char mine[rows][cols);/Set the location
of the Thunder int Sweep (char mine[rows][cols], char show[rows][cols]);/start minesweeper
int get_num (char mine[rows][cols], int x, int y); Calculate the number of thunder

Implement function game.c

#include "game.h"//Menu function int menu () {printf ("********************************************\n");
	printf ("********************************************\n");
	printf ("*************welcome to saolei*************\n");      printf ("************* 1.
	Play *************\n ");      printf ("************* 0.
	Exit *************\n ");
	printf ("********************************************\n");
	printf ("********************************************\n");
return 0;
	}//Set the location of the Thunder void Set_mine (char mine[rows][cols]) {int count = count;
	int x = 0;
	int y = 0;
	Srand ((unsigned) time (NULL));
		while (count) {x = rand ()% 9 + 1;
		y = rand ()% 9 + 1;
			if (mine[x][y] = = ' 0 ') {mine[x][y] = ' 1 ';
		count--;
	Finish playing chess the interface void display (char show[rows][cols]) {int i = 0;
	int j = 0;
	printf ("");
	for (i = 1; i < cols-1; i++) {printf ("%d", I);
	printf ("\ n");
		for (i = 1; i < rows-1; i++) {printf ("%d", I); for (j = 1; j < Cols-1; J +) {printf ("%c ", Show[i][j]);
	printf ("\ n");
	Count the number of mines int Get_num (char mine[rows][cols], int x, int y) {int count = 0;
	if (mine[x-1][y-1] = = ' 1 ')//upper left {count++;
	} if (mine[x-1][y] = = ' 1 ')//left {count++;
	} if (Mine[x-1][y + 1] = = ' 1 ')//lower left {count++;
	} if (mine[x][y-1] = = ' 1 ')//above {count++;
	} if (Mine[x][y + 1] = = ' 1 ')//below {count++;
	} if (Mine[x + 1][y-1] = = ' 1 ')//upper right {count++;
	if (mine[x + 1][y] = = ' 1 ')//right {count++;
	if (mine[x + 1][y + 1] = = ' 1 ')//lower right {count++;
return count;
	}//Minesweeper int Sweep (char mine[rows][cols], char show[rows][cols]) {int count = 0;
	int x = 0;
	int y = 0;
		while (count!= (rows-2) * (cols-2)-count)) {printf ("Please enter coordinates: \ n");
		scanf ("%d%d", &x, &y); if (mine[x][y] = = ' 1 ') {printf ("You stepped on Ray.")
			\ n ");
		return 0;
			else {int ret = Get_num (mine, x, y);
			Show[x][y] = ret + ' 0 ';
			Set_mine (mine);
			Display (show);
		count++; } printf ("Congratulations on winning.")
	\ n ");
	Display (mine); return 0;
}//Game int Game (char Mine[rows][cols],char show[rows][cols]) {set_mine (mine);
	Display (show);
	Display (mine);//can show the position of the Thunder Sweep (mine,show);
return 0; }

The last is the test function text.c

#include "game.h"

int main ()
{
	int input = 0;
	Char Mine[rows][cols];
	Char Show[rows][cols];
	int i = 0;
	int j = 0;
	for (i = 0; i < rows-1. i++)
	{for
		(j = 0; J < Cols-1; j)
		{
			Mine[i][j] = ' 0 ';
			SHOW[I][J] = ' * ';
		}
	}
	menu ();
	while (1)
	{
		printf ("Please select:");
		scanf ("%d", &input);
		if (input = = 1)
		{
			printf ("Enter game \ n");
			Game (mine,show);
			break;
		else if (input = = 0)
		{
			printf ("Quit the game.") \ n ");
			Exit (0);
			break;
		else
		{
			printf ("wrong input.") \ n ");
		}
	}
	return 0;
}


So a simple minesweeper function is written, let's play one:






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.