How to use C language code to realize minesweeping game
This article mainly explains "how to use C language code to implement minesweeper game". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use C language code to implement minesweeper game"!
Overview
Minesweeper is a popular educational Mini Game. The goal of the game is to find out all the non-Riggs according to the numbers that appear in the grid, and at the same time avoid stepping on mines. If you step on one thunder, you will lose the whole game.
Realization process
1. Create a user interaction menu
2. Mine laying function
3. Display minesweeping matrix
4. Players customize coordinates
5. Calculate the number of mines discharged
Multi-file implementation
Header file clear_mine.h
# pragma once / / prevent header files from being duplicated # define _ CRT_SECURE_NO_WARNINGS 1 / / implement scanf compilation through # include # define ROW 8 # define COL 8 # define STYLE'? / / initialize # define NUM 20 / / number of mines extern void Game ()
Source file main.c
Show the menu bar to the player
# include "clear_mine.h" static void Menu () / / user interaction menu {printf ("#\ n"); printf ("# 1. Play 0.Exit #\ n"); printf ("#\ n");} int main () {int quit = 0; int select = 0 While (! quit) {Menu (); printf ("Please Enter#"); scanf ("% d", & select); switch (select) {case 1: Game (); break; case 0: quit = 1; break; default: printf ("Position Error, Try Again!\ n"); break;} printf ("byebye!\ n"); system ("pause"); return 0;}
Source file clear_mine.c
# include "clear_mine.h" static void SetMines (char board [] [COL], int row, int col) / / Mine {int count = NUM; while (count) {int x = rand ()% (row-2) + 1; int y = rand ()% (col-2) + 1 / / Random number if (board [x] [y] = ='0') / / illegal judgment can only generate one random number {board [x] [y] = '1cm; count--;}} static void ShowLine (int col) {for (int I = 0; I)