3.完整代码
头文件game.h
#pragma once #include<stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); //显示棋盘 void DisplayBoard(char board[ROWS][COLS], int row, int col); //布置雷 void SetMine(char board[ROWS][COLS], int row, int col); //排查雷的 void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); //特殊情况大片展开 void Clear(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col, int* win);
game.c
#include"game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) board[i][j] = set; } } void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count)//一共设置了10个雷,没设置一颗雷,count就减一 { //生成随机下标,这里利用时间戳和rand()函数 int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; printf("------------------------\n"); for (i = 0; i <= 9; i++)//方便观看棋盘,给每行每列标上序号,标行号 { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%d ", i);//标列号 for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("------------------------\n"); } int GetMineCount(char mine[ROWS][COLS], int x, int y)//因为雷是‘1’,所以将排查点周围的点相加 //因为‘1’-‘0’=1,Ascii码 //所以周围八个字符相加减去8*‘0’等于几 //说明有几颗雷在旁边 { 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 Clear(char mine[ROWS][COLS], char show[ROWS][COLS] , int x , int y , int row , int col, int*win) { if (x >= 1 && x <= row && y >= 1 && y <= col) { if (show[x][y] == ' ' || show[x][y]!='*') return; else if (GetMineCount(mine, x, y) != 0) { show[x][y] = GetMineCount(mine, x, y) + '0'; (*win)++; return; } else { show[x][y] = ' '; (*win)++; Clear(mine, show, x - 1, y, ROW, COL, win); Clear(mine, show, x + 1, y, ROW, COL, win); Clear(mine, show, x, y - 1, ROW, COL, win); Clear(mine, show, x, y + 1, ROW, COL, win); } }return; } void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col) { int x = 0; int y = 0; int win = 0; while (win < row * col - EASY_COUNT)//一共10颗雷,只要排过的点<所有格子减去10个雷,就继续 { printf("请输入要排查的坐标:>"); scanf_s("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("很遗憾,你被炸死了\n"); DisplayBoard(mine, ROW, COL); break; } else { Clear(mine, show, x, y, ROW, COL, &win); DisplayBoard(show, ROW, COL); printf("已排除位置数= %d\n", win); } else { printf("坐标非法,重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }
扫雷.c
#include"game.h" void menu() { printf("****作者:敲代码的布莱恩特******\n"); printf("********* 1. play ********\n"); printf("********* 0. exit ********\n"); printf("********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 };//初始化两个二维数组,一个有用于存放雷的信息 char show[ROWS][COLS] = { 0 };//一个用于存放排查出的雷的信息 //对棋盘进行初始化 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); ///布置雷 SetMine(mine, ROW, COL); //打印布置好了雷的棋盘,将设置好雷的棋盘显示出来 DisplayBoard(show, ROW, COL); //排雷 FindMine(mine, show, ROW, COL); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu(); scanf_s("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("没有这个选项,重新选择\n"); break; } } while (1); return 0; }