C语言实现扫雷

    xiaoxiao2022-07-02  98

    扫雷游戏

    我天我给你大家发一下我最近做的扫雷,纯c写的,并且完成了两个功能,1、是第一次扫雷,不会被炸死。2、是每次可以扫出一大片。 希望大家可以从有所收获! 源码呈上!

    game.h

    #ifndef GAME.H #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define ROWS ROW+2 #define COLS COL+2 #define ROW 9 #define COL 9 #define MINENUM 15 #define UN_MINENUM (ROW*COL - MINENUM) void Init_broad(char arr_str[ROWS][COLS], int row, int col, char set);//初始化函数 void Print_broad(char arr_str[ROWS][COLS], int row, int col);//打印函数 void Desposit_Mine(char mine_broad[ROWS][COLS], int row, int col);//存放雷 void Mine_Clean(char test[ROWS][COLS], char show_broad[ROWS][COLS], char mine_broad[ROWS][COLS], int row, int col);//清理雷 static int Calculate_number(char show_broad[ROWS][COLS], int x, int y);//计算函数 void Judge_F_mine(char mine_broad[ROWS][COLS], int x, int y, int row, int col);//判断第一次是否为雷 void Clean_Mine(char show_broad[ROWS][COLS], char mine_broad[ROWS][COLS], int x, int y);//一大片清雷 int Computer_num(char test[ROWS][COLS], char show_broad[ROWS][COLS], int row, int col);//统计每次显示的个数 #endif // !GAME.H

    game.c

    #include"game.h" //实现文件 void Init_broad(char arr_str[ROWS][COLS], int row, int col, char set) { int i = 0, j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { arr_str[i][j] = set;//#号代表非雷 } } //memset(mine_broad, set, row*col * sizeof(mine_broad[0][0])); } void Print_broad(char arr_str[ROWS][COLS], int row, int col) { //打印表 system("cls"); int i = 0, 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 ", arr_str[i][j]); } printf("\n"); } } void Desposit_Mine(char mine_broad[ROWS][COLS], int row, int col) { //生成随机数 int x = 0; int y = 0; int number = MINENUM;//雷的个数 //#-非雷 *-雷 //判断是否被占用 while (number) { x = rand() % row + 1; y = rand() % col + 1; if (mine_broad[x][y] == '0') { mine_broad[x][y] = '1'; number--; } } } void Mine_Clean(char test[ROWS][COLS], char show_broad[ROWS][COLS], char mine_broad[ROWS][COLS], int row, int col) { int x = 0, y = 0;//输入的值 int unmine_num = UN_MINENUM;//非雷的计数器 while (unmine_num)//查找循环 { printf("请输入您想输入的坐标>"); scanf_s("%d%d", &x, &y); if ((x >= 1 && x <= row) && (y >= 1 && y <= col))//判断输入合不合适 { if (show_broad[x][y] == '*') { if (mine_broad[x][y] == '1' && unmine_num == UN_MINENUM)//防止第一次被炸死 { Judge_F_mine(mine_broad, x, y, row, col); unmine_num--; } if (mine_broad[x][y] == '1')//判断是否被雷炸死了! { printf("很遗憾,踩到地雷了!\n"); //Print_broad(mine_broad, ROW, COL); break; } else { /*char number = Calculate_number(mine_broad, x, y);//正常运行的情况 show_broad[x][y] = number + '0';*/ Clean_Mine(show_broad, mine_broad, x, y); unmine_num -= Computer_num(test, show_broad, row, col); } Print_broad(show_broad, ROW, COL); } else { printf("坐标被占用,请重新输入!\n"); } } else { printf("坐标错误,请重新输入!\n"); } //Print_broad(show_broad, ROW, COL);//打印一下 } if (unmine_num == 0) { printf("恭喜您,勇士,顺利通关!\n"); } } static int Calculate_number(char mine_broad[ROWS][COLS], int x, int y) { return mine_broad[x - 1][y] + mine_broad[x - 1][y - 1] \ + mine_broad[x][y - 1] + mine_broad[x + 1][y - 1] \ + mine_broad[x + 1][y] + mine_broad[x + 1][y + 1] \ + mine_broad[x][y + 1] + mine_broad[x - 1][y + 1] \ - 8 * '0' ;//但是都是字符相加的, } void Judge_F_mine(char mine_broad[ROWS][COLS], int x, int y, int row, int col) { if (mine_broad[x][y] == '1') { mine_broad[x][y] = '0'; int a = 0, b = 0; while (1) { a = rand() % row + 1; b = rand() % col + 1; if (mine_broad[a][b] == '0' && a != x) { mine_broad[a][b] = '1'; break; } } } return; } void Clean_Mine(char show_broad[ROWS][COLS], char mine_broad[ROWS][COLS], int x, int y) { if (mine_broad[x][y] == '0' && x >= 0 && y >= 0 && show_broad[x][y] == '*')//0 { show_broad[x][y] = Calculate_number(mine_broad, x, y) + '0';//show_broad[x][y]是存储字符型的,而函数的返回值却为整数 } if (mine_broad[x - 1][y] == '0' && x >= 0 && y >= 0 && show_broad[x - 1][y] == '*')//1 { show_broad[x - 1][y] = Calculate_number(mine_broad, x - 1, y) + '0'; if (Calculate_number(mine_broad, x - 1, y) == 0) { Clean_Mine(show_broad, mine_broad, x - 1, y); } } if (mine_broad[x - 1][y - 1] == '0' && x >= 0 && y >= 0 && show_broad[x - 1][y - 1] == '*')//2 { show_broad[x - 1][y - 1] = Calculate_number(mine_broad, x - 1, y - 1) + '0'; if (Calculate_number(mine_broad, x - 1, y - 1) == 0) { Clean_Mine(show_broad, mine_broad, x - 1, y - 1); } } if (mine_broad[x][y - 1] == '0' && x >= 0 && y >= 0 && show_broad[x][y - 1] == '*')//3 { show_broad[x][y - 1] = Calculate_number(mine_broad, x, y - 1) + '0'; if (Calculate_number(mine_broad, x, y - 1) == 0) { Clean_Mine(show_broad, mine_broad, x, y - 1); } } if (mine_broad[x + 1][y - 1] == '0' && x >= 0 && y >= 0 && show_broad[x + 1][y - 1] == '*')//4 { show_broad[x + 1][y - 1] = Calculate_number(mine_broad, x + 1, y - 1) + '0'; if (Calculate_number(mine_broad, x + 1, y - 1) == 0) { Clean_Mine(show_broad, mine_broad, x + 1, y - 1); } } if (mine_broad[x + 1][y] == '0' && x >= 0 && y >= 0 && show_broad[x + 1][y] == '*')//5 { show_broad[x + 1][y] = Calculate_number(mine_broad, x + 1, y) + '0'; if (Calculate_number(mine_broad, x + 1, y) == 0) { Clean_Mine(show_broad, mine_broad, x + 1, y); } } if (mine_broad[x + 1][y + 1] == '0' && x >= 0 && y >= 0 && show_broad[x + 1][y + 1] == '*')//6 { show_broad[x + 1][y + 1] = Calculate_number(mine_broad, x + 1, y + 1) + '0'; if (Calculate_number(mine_broad, x + 1, y + 1) == 0) { Clean_Mine(show_broad, mine_broad, x + 1, y + 1); } } if (mine_broad[x][y + 1] == '0' && x >= 0 && y >= 0 && show_broad[x][y + 1] == '*')//7 { show_broad[x][y + 1] = Calculate_number(mine_broad, x, y + 1) + '0'; if (Calculate_number(mine_broad, x, y + 1) == 0) { Clean_Mine(show_broad, mine_broad, x, y + 1); } } if (mine_broad[x - 1][y + 1] == '0' && x >= 0 && y >= 0 && show_broad[x - 1][y + 1] == '*')//8 { show_broad[x - 1][y + 1] = Calculate_number(mine_broad, x - 1, y + 1) + '0'; if (Calculate_number(mine_broad, x - 1, y + 1) == 0) { Clean_Mine(show_broad, mine_broad, x - 1, y + 1); } } } int Computer_num(char test[ROWS][COLS], char show_broad[ROWS][COLS], int row, int col) { int count = 0; int i = 1, j = 0; for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { if (show_broad[i][j] != '*') { if (test[i][j] != 'a') { count++; test[i][j] = 'a';//只有代表雷 } } } } return count; }

    test.c

    #include"game.h" //测试文件 void Tips() { printf("\t 游戏玩法介绍\n"); printf("一、按1游戏开始,0退出游戏!\n"); printf("二、游戏需要输入坐标才可以扫雷!\n"); printf("三、扫出所有雷后取得游戏胜利,碰到雷游戏结束!\n"); printf("------------------------------------\n\n"); } void menu() { printf("************************\n"); printf("****** 1、play *******\n"); printf("****** 0、exit *******\n"); printf("************************\n"); } void game() { char mine_broad[ROWS][COLS] = { '0' };//雷区的数组 char show_broad[ROWS][COLS] = { '0' };//打印的数组 char test[ROWS][COLS] = { '0' }; //初始化 Init_broad(mine_broad, ROWS, COLS, '0'); Init_broad(show_broad, ROWS, COLS, '*'); Init_broad(test, ROWS, COLS, 'M');//用来统计非雷个数--效率变低 Print_broad(show_broad, ROW, COL); //存放雷 Desposit_Mine(mine_broad, ROW, COL); //Print_broad(mine_broad, ROW, COL);//test1 //排雷 Mine_Clean(test, show_broad, mine_broad, ROW, COL); } void test() { Tips(); srand((unsigned int)time(NULL));//设置种子 int input = 0; do { menu();//菜单 printf("请选择>"); scanf_s("%d", &input); switch (input) { case 1: //游戏大循环体 game();//游戏 break; case 0: //退出 printf("退出游戏\n"); break; default: printf("选择错误,请重新选择!\n"); break; } } while (input); } int main() { test();//game()函数是函数运行的函数 system("pause"); return 0; }

    在这里,我说一下,显示一大片我们采用的是递归的思想,我们输入一个坐标(x,y), 假设不为雷, 我们对一个不为雷(x,y),判断它的周围是否有雷,有雷的不管,没雷的我们显示它的数字,如果该数是零,就把它作为新的(x,y),继续判断他的周围,以此类推,直到我们申请的二维数组的0和MAX位置(i>=0&&i<=ROWS, j>=0&&j<=COLS),当然你打印大一大片里面判断成功条件就要有所改变。这个不难,方法也特别多。嘿嘿,我才用的方法比较笨拙,浪费内存。大家可以做出更好的优化方法。 谢谢大家,就分享到这了,希望对您有帮助

    最新回复(0)