目录
主函数:test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void menu() { printf("*****************************\n"); printf("****** 1.扫雷游戏 ******\n"); printf("****** 0.退出游戏 ******\n"); printf("*****************************\n"); } void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; Initboard(mine, ROWS, COLS, '0'); Initboard(show, ROWS, COLS, '*'); Display(show, ROW, COL); Setmine(mine, ROW, COL); Display(mine, ROW, COL); Findmine(mine,show, ROW, COL); } int main() { int input = 0; srand((unsigned)time(NULL)); do { menu(); printf("请输入选择:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("谢谢参加,已退出!\n"); break; default: printf("输入错误!\n"); break; } } while (input); return 0; }
声明:game.h
#pragma once #include<stdio.h> #define ROW 9 #define COL 9 #define number 10 #define ROWS ROW + 2 #define COLS COL + 2 void Initboard(char mine[ROWS][COLS], int rows, int cols,char set); void Display(char show[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);
函数:game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void Initboard(char mine[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++) { mine[i][j] = set; } } } void Display(char show[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("------扫雷游戏------\n"); for (i = 0; i <= row; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", show[i][j]); } printf("\n"); } } void Setmine(char mine[ROWS][COLS], int row, int col) { int a = number; while (a) { int b = rand() % row + 1; int c = rand() % col + 1; if (mine[b][c] == '0') { mine[b][c] = '1'; a--; } } } int get_number(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'; } //void get_more(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y) //{ // int i = 0; // int j = 0; // for (i = -1; i < 2; i++) // { // for (j = -1; j < 2; j++) // { // int count = get_number(mine, x + i, y + j); // show[x + i][y + j] = count + '0'; // if (show[x + i][y + j] == '0') // get_more(mine, show, x + i, y + j); // } // } //} void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int z = 0; while (z < row*col - number) { printf("请输入坐标:"); scanf("%d %d", &x, &y); if (mine[x][y] == '1') { printf("抱歉您被炸死了!\n"); Display(mine, ROW, COL); break; } else { int count = get_number(mine, x, y); show[x][y] = count + '0'; //if (show[x][y] == '0') // get_more(mine,show, x, y); Display(show, ROW, COL); } if (z = 0) { printf("恭喜您!成功拍雷。"); Display(mine,ROW,COL) } } }