Implementation in C language to write a minesweeper game everyone must have played minesweeper, regardless of the version is difficult for the choice, below to share a C language in the mine game it! Writing language: C language software: Visual Studio 2017
1. The first is to write the test module of the game, to have prompted the player to enter the menu functions and select Functions and other functions need to write a whole framework to come up with
Testing the module test. C #include "game.h" void menu () {printf ("***********************************************************************\n" ); printf ("************************1.play 0.exit*************************\n"); printf ("***********************************************************************\n");} int Game () {int x = 0; int y = 0; int i = 0; int j = 0; int ret = 0; int num = count; Char Real[rows][cols] = {' 0 '}; Char Show[rows][cols] = {' * '}; Srand ((unsigned) time (NULL)); Init (Real,rows, COLS, ' 0 '); Init (show, ROWS, COLS, ' * '); Setmine (Real, ROWS, COLS, Count); Display (show, ROW, COL); printf ("Pelase Input x and y: \ n"); scanf_s ("%d%d", &x, &y); if (real[x][y] = = ' 1 ')//guarantee the first time not to step on the Thunder {do {setmine (real, ROWS, COLS, Count); } while (real[x][y] = = ' 1 '); } Show[x][y] = Searchmind (real, &x, &y, ROWS, COLS) + ' 0 '; for (i = 0; I <ROWS; i++)//Administrator monitor module {//printf ("%3d", I); for (j = 0; J < COLS; J + +) {printf ("%3c", Real[i][j]); } printf ("\ n"); } printf ("\ n"); Display (show, ROW, COL); while ((Row*col) > (row*col-num)) {printf ("pelase Input x and y: \ n"); scanf_s ("%d%d", &x, &y); ret = Searchmind (real, &x, &y, ROWS, COLS) + 1; if (ret!=0)//is not stepping on the condition of thunder into {show[x][y] = Searchmind (real, &x, &y, ROWS, COLS) + ' 0 ';//convert to character type Display (show, ROW, COL); } if (ret = = 0)//Step into the case of Thunder into {return 0; } num--; } return 1;} int main () {Menu (); while (1) {int m = 0; scanf_s ("%d", &m); Switch (m) {Case 1: {if (Game ()) {printf ("win\n"); } else {printf ("you failed\n"); Menu (); } break; } case 2:return 0; Break default:printf ("Please re-enter \ n"); Break } }}
The header file in the test function #include "game.h" instead of # include
Next is the header file game.h
#ifndef __GAME_H__#define __GAME_H__#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#define ROWS 11#define COLS 11#define ROW 9#define COL 9#define count 10void Init(char arr[ROWS][COLS],int rows, int cols, const char ch);void SetMine(char str[ROWS][COLS], int rows, int cols, int cot);void Display(char str[ROWS][COLS], int rows, int cols);int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows, int cols);#endif
Next is the most important part of the game is also the core part of the game function file game.c
#include "game.h" void Init (char arr[rows][cols], int ROWS, int COLS, const char ch) {int i = 0; int j = 0; for (i = 0; i < rows; i++) {for (j = 0; J < cols; J + +) {arr[i][j] = ch; }}}//Random buried Setmine () void Setmine (char str[rows][cols], int ROWS, int COLS, int cot) {int x = 0; int y = 0; while (Cot > 0) {x = rand ()% 9 + 1; y = rand ()% 9 + 1; if (str[x][y]== ' 0 ') {str[x][y] = ' 1 '; cot--; }}}//Displays the checkerboard display () void display (char str[rows][cols], int ROWS, int COLS) {int i = 0; int j = 0; for (i =-1; i < rows; i++) {printf ("%3d", i+1); } printf ("\ n"); for (i = 1; I <=rows; i++) {printf ("%3d", I); for (j = 1; J <= Cols; j + +) {printf ("%3c", Str[i][j]); } printf ("\ n"); }}//input coordinates, judging the number of mines around, and giving back the numbers, for 0 o'clock recursion searchmine () int searchmind (char str[rows][cols], int* x, int* y, int rows,int cols) {int number = 0; Determine whether to step on the Thunder if (str[*x][*y] = = ' 1 ') {return-1; } number = Str[*x-1][*y-1] + str[*x-1][*y] + str[*x-1][*y + 1] + str[*x][*y-1] + str[*x][*y + 1] + str[*x + 1][*y-1] + str[*x + 1][*y] + str[*x + 1][*y + 1]-8 * ' 0 '; if (number = = 0)//{////change the value of x, Y to achieve explosion effect//# = Searchmind (str, &x, &y, rows, cols); }//recursive return number;} Judging winning and losing distinguish ()
C language for minesweeper