本篇内容主要讲解“C语言怎么实现简单的扫雷功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C语言怎么实现简单的扫雷功能”吧!
下面看代码:
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("请选择: "); scanf("%d", &input); switch (input) { case 1: printf("扫雷游戏!\n"); game(); break; case 0: printf("已退出游戏!\n"); break; default: printf("输入错误,请重新输入!\n"); break; } } while (input);}void game(){ //雷的信息存储 //1.布置雷的信息 char mine[ROWS][COLS] = { 0 }; //11*11 //2.排查出的雷的信息 char show[ROWS][COLS] = { 0 }; //初始化 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //打印棋盘 DisplyBoard(mine, ROW, COL); //DisplyBoard(show, ROW, COL); //布置雷 SetMine(mine, ROW, COL); DisplyBoard(mine, ROW, COL); //扫雷 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; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } }} void DisplyBoard(char board[ROWS][COLS], int row, int col){ int i = 0; int j = 0; //打印列号 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 ", board[i][j]); } printf("\n"); }} void SetMine(char board[ROWS][COLS], int row, int col){ int count = COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } }} 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 - COUNT) { printf("请输入坐标: "); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { //坐标合法 //1.踩雷 if (mine[x][y] == '1') { printf("你被炸死了!!!\n"); DisplyBoard(mine, row, col); break; } //不是雷 else { //计算x,y坐标周围有几个雷 int count = get_mine_count(mine, x, y); show[x][y] = count + '0'; DisplyBoard(show, row, col); win++; } } else { printf("坐标非法,请重新输入!"); } } if (win == ROW * COL - COUNT) { printf("恭喜你,排雷成功!!!\n"); DisplyBoard(mine, row, col); }}
game.h
#define ROW 9#define COL 9 #define ROWS ROW+2#define COLS COL+2 #define COUNT 80#include<stdio.h>#include<stdlib.h>#include<time.h> void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);void DisplyBoard(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);
到此,相信大家对“C语言怎么实现简单的扫雷功能”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!