欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

扫雷源代码(C语言)

时间:2023-04-30

game.h:函数声明
test.c:运行测试
game.c:游戏核心代码

game.h

#include#include#include#define easy_count 10//雷的个数#define ROW 9//棋盘大小#define COL 9//宏定义方便后续增大或缩小棋盘#define ROWS ROW+2#define COLS COL+2//初始化棋盘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);

test.c

#include"game.h"void menu(){printf("********************************n");printf("********** 1.play ***********n");printf("********** 0.exit ***********n");printf("********************************n");}void game(){system("cls");//清屏char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息char show[ROWS][COLS] = { 0 };//存放排查出雷的信息//初始化棋盘initboard(mine, ROWS, COLS, '0');initboard(show, ROWS, COLS, '*');//打印棋盘//displayboard(mine, ROW, COL);displayboard(show, ROW, COL);//布置雷setmine(mine, ROW, COL);//displayboard(mine, ROW, COL);//排查雷findmine(mine, show, ROW, COL);}int main(){int input;srand((unsigned int)time(NULL));do{menu();printf("请选择:>n");scanf("%d", &input);switch (input){case 1:printf("开始游戏n");game();break;case 0:printf("退出游戏n");break;default:printf("选择错误,请重新选择n");break;}} while (input);}

game.c

#include"game.h"void initboard(char board[ROWS][COLS], int rows, int cols, char set){int i, j;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){board[i][j] = set;}}}void displayboard(char board[ROWS][COLS], int row, int col){int i, j;printf("----------扫雷游戏-----------n");for (i = 0; i <= col; 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");}printf("----------扫雷游戏-----------n");}void setmine(char board[ROWS][COLS], int row, int col){//布置10个雷int count = easy_count;//产生随机下标while (count){int x = rand() % row + 1;int y = rand() % col + 1;//判断当前坐标是否有雷if (board[x][y] == '0'){board[x][y] = '1';count--;}}}int get_mine_count(char mine[ROWS][COLS], int x, int y){char num = '0';int i, j;for (i = -1; i <= 1; i++){for (j = -1; j <= 1; j++){num += mine[x + i][y + j];}}return num - 10 * '0';}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");scanf("%d%d", &x, &y);//判断坐标合法性if (x >= 1 && x <= row && y >= 1 && y <= col){if (mine[x][y] == '1'){printf("很遗憾,你被炸死了n");break;}else{//不是雷的情况下,判断x,y周围有几个雷int count = get_mine_count(mine, x, y);show[x][y] = count + '0';//显示排查到的信息displayboard(show, ROW, COL);win++;}}elseprintf("坐标不合法n");}if (mine[x][y] == '0')printf("恭喜你,你赢了n");displayboard(mine, ROW, COL);}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。