C语言 扫雷小游戏(第一次下子,不炸死)

世事洞明皆学问,人情练达即文章。这篇文章主要讲述C语言 扫雷小游戏(第一次下子,不炸死)相关的知识,希望能为你提供帮助。
test.c

#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h"void menu()// 菜单 { printf("**********************\\n"); printf("*****1.play*****\\n"); printf("*****0.exit*****\\n"); printf("**********************\\n"); }void game() { char show[ROWS][COLS] = { 0 }; //玩家的数据 char mine[ROWS][COLS] = { 0 }; //地雷的数据 Initboard(show, ROWS, COLS, \'*\'); //初始化数据 Initboard(mine, ROWS, COLS, \'0\'); Displayboard(show, ROW, COL); // 显示游戏界面 Fillmine(mine, ROW, COL,MINE); //设置地雷 /*Displayboard(mine, ROW, COL); */ Findmine(show,mine, ROW, COL); //排雷}int main() { srand((unsigned int)time(NULL)); //生成随机数rand int input = 0; 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.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h"void Initboard(char arr[ROWS][COLS], introws, int cols, char set)//初始化数据 { int x = 0; int y = 0; for (x = 0; x < rows; x++) { for (y = 0; y < cols; y++) { arr[x][y] = set; } } }void Displayboard(char arr[ROWS][COLS], introw, int col)//显示游戏界面 { int x = 0; int y = 0; printf("-------------扫雷小游戏-------------\\n"); for (x = 0; x < = col; x++) printf("%d ", x); printf("\\n"); for (x = 1; x < = row; x++) { printf("%d ", x); for (y = 1; y < = col; y++) { printf("%c ", arr[x][y]); } printf("\\n"); } printf("-------------扫雷小游戏-------------\\n"); }void Fillmine(char arr[ROWS][COLS], introw, int col, int set)//设置地雷 { int i = 0; int j = 0; int count = 0; for (i = 1; i < = row; i++)//因为第一次下子,不炸死,重新填雷,计算雷的个数 { for (j = 1; j < =col; j++) { if (arr[i][j] == \'1\') { count++; } } } while (count< set) { int x = rand() % row + 1; int y = rand() % col + 1; if (arr[x][y] == \'0\') { arr[x][y] = \'1\'; count++; } } }int CRmine(char b[ROWS][COLS], int x,int y)//计算周边地雷个数 { int i = 0; int j = 0; int count = 0; for (i = -1; i < = 1; i++) { for (j = -1; j < = 1; j++) { if (b[x + i][y + j] == \'1\') { count++; } } } return count; }void Findmine(char a[ROWS][COLS], char b[ROWS][COLS], introw, int col)//排雷 { int x = 0; int y = 0; int set = 0; int count = 0; printf("请输入排雷坐标:"); scanf("%d%d", & x, & y); if (x > = 1 & & x < = row & & y > = 1 & & y < = col)//第一次下子,不炸死 { if (b[x][y] == \'1\') { b[x][y] = \'0\'; Fillmine(b, row, col, MINE); } set = CRmine(b, x, y); a[x][y] = set + \'0\'; count++; Displayboard(a, ROW, COL); } while (count< row*col-MINE) { printf("请输入排雷坐标:"); scanf("%d%d", & x, & y); if (x > = 1 & & x < = row & & y > = 1 & & y < = col) { if (b[x][y] == \'1\') { printf("踩雷,游戏结束\\n"); Displayboard(b, ROW, COL); break; } else { set = CRmine(b,x,y); a[x][y] = set+\'0\'; Displayboard(a, ROW, COL); count++; } } else { printf("输入范围有无,请重新输入\\n"); } } if (count == row * col - MINE) { printf("恭喜你,排雷成功\\n"); } }

game.h
#pragma once #include< stdio.h> #include< time.h> #include< stdlib.h> #define ROW 9 #define COL 9 #define ROWS 11 #define COLS 11 #define MINE 10void Initboard(char arr[ROWS][COLS], introws, int cols,char set); //初始化数据 void Displayboard(char arr[ROWS][COLS], introw, int col); //显示玩家界面 void Fillmine(char arr[ROWS][COLS], introw, int col, int set); //设置地雷 void Findmine(char a[ROWS][COLS], char b[ROWS][COLS], introw, int col); //排雷

运行结果:
【C语言 扫雷小游戏(第一次下子,不炸死)】
C语言 扫雷小游戏(第一次下子,不炸死)

文章图片


    推荐阅读