Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple minesweeping function in C language

Shulou Source: shulou.com Published: 2022-06-02 16:48:03 10月03日 Update

This article mainly explains "how to achieve simple minesweeping function in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C language how to achieve simple minesweeping function"!

Let's look at the code:

Test.c

# define _ CRT_SECURE_NO_WARNINGS#include "game.h" void test (); void menu (); void game (); int main () {test (); return 0;} void menu () {printf ("*\ n"); printf ("* 1.play *\ n") Printf ("* 0.exit *\ n"); printf ("*\ n");} void test () {int input = 0; srand ((unsigned int) time (NULL)); do {menu (); printf ("Please choose:"); scanf ("% d", & input) Switch (input) {case 1: printf ("Minesweeper!\ n"); game (); break; case 0: printf ("quit the game!\ n"); break; default: printf ("input error, please re-enter!\ n"); break;}} while (input);} void game () {/ / mine information storage / / 1. Information about mine arrangement char mine [ROWS] [COLS] = {0}; / / 1111 / / 2. Char show [ROWS] [COLS] = {0}; / initialize InitBoard (mine, ROWS, COLS,'0'); InitBoard (show, ROWS, COLS,'*'); / / print chessboard DisplyBoard (mine, ROW, COL); / / DisplyBoard (show, ROW, COL); / / deploy SetMine (mine, ROW, COL); DisplyBoard (mine, ROW, COL); / / Minesweeper FindMine (mine, show, ROW, COL);}

Game.c

# define _ CRT_SECURE_NO_WARNINGS#include "game.h" / / '1'-'0'=1//'3'-'0'=3int get_mine_count (char mine [ROWS] [COLS], int x Int y) {return mine [x-1] [y-1] -'0' + mine [x] [y-1] -'0' + mine [x + 1] [y-1] -'0' + mine [x + 1] [y] -'0' + mine [x + 1] [y + 1] -'0' + mine [x] [y + 1] -'0' + mine [x-1] [y + 1]- '0' + mine [x-1] [y] -' 0' } void InitBoard (char board [ROWS] [COLS], int rows, int cols, char set) {int I = 0; int j = 0; for (I = 0; I < rows; iTunes +) {for (j = 0; j < cols; jacks +) {board [I] [j] = set;}} void DisplyBoard (char board [ROWS] [COLS], int row, int col) {int I = 0; int j = 0; / / print column number for (I = 0; I

Tags: Minesweeping function language information coordinates input content jackers learning legality practicality deeper success code interest practicality practicality simplicity of operation methods more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology NVidia Shulou Tech Info MySQL