本文实例为你们分享了C语言实现扫雷游戏及其优化的具体代码,供你们参考,具体内容以下数组
关于扫雷优化工具
1.核心思想:使用两个二维数组进行设计,一个用于显示,一个用于后台雷的布置。布局
2.使用宏常量,后期能够任意修改游戏难度。学习
3.关于扫雷拓展模块,目前使用的方法比较low,若周围均没有,则所有显示。开发工具
4.剩余位置数使用全局变量count,必须考虑拓展以后count变化。优化
有待改进之处ui
1.需设计标记雷的步骤,增长用户体验。职业规划
2.拓展方式有待改进。spa
3.界面布局仍须要进行优化设计
这里推荐一下我建的C/C++语言学习交流秋秋裙,前三位是:110,中间三位是:355,最后三位是:025,里面有学习不错视频教程、开发工具、电子书籍,完整的项目源码等,专业的老师解答问题!
虽说C语言开发发展前景好,但易学难精。因为入门容易这也致使了市场上人员泛滥、人才稀缺的局面产生。可是在互联网愈来愈强烈的竞争下,这样的人也最终会被市场淘汰。对于想要从事C语言行业的小伙伴来讲,必定要清楚本身将来的职业规划和就业方向。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<Windows.h> 4 #include<string.h> 5 #include<time.h> 6 #define ROW 12 7 #define COL 12 8 #define MINE_NUM 15 9 #define TOTAL 100 10 #pragma warning(disable:4996) 11 int count = TOTAL; 12 void inter(){ 13 printf("=======================\n"); 14 printf("=======游戏菜单========\n"); 15 printf("======1.开始游戏=======\n"); 16 printf("========2.退出=========\n"); 17 printf("=======================\n"); 18 printf("请输入您的选择: \n"); 19 } 20 int GetRandIndex(int start, int end){ 21 return rand() % (end - start + 1) + start; 22 } 23 void layout(char mine[][COL], int row, int col){ 24 srand((unsigned long)time(NULL)); 25 int count = 0; 26 while (count<MINE_NUM){ 27 int x = GetRandIndex(1, 10); 28 int y = GetRandIndex(1, 10); 29 if (mine[x][y] == '0'){ 30 mine[x][y] = '1'; 31 count++; 32 } 33 } 34 } 35 36 void Board(char board[][COL], int row, int col){ 37 printf(" "); 38 int i = 1; 39 for (; i <= 10; i++) 40 { 41 printf(" %d ", i); 42 } 43 printf("\n----"); 44 for (i = 1; i <= 29; i++) 45 { 46 printf("-"); 47 } 48 printf("\n"); 49 for (i = 1; i <= 10; i++) 50 { 51 printf("%2d|",i); 52 int j = 1; 53 for (; j <= 10; j++){ 54 printf(" %c|", board[i][j]); 55 } 56 printf("\n"); 57 int k = 1; 58 for (k = 1; k <= 11; k++) 59 { 60 printf("---"); 61 } 62 printf("\n"); 63 } 64 } 65 66 char GetMines(char mine[][COL],int row,int col){ 67 return mine[row - 1][col - 1] + mine[row - 1][col] + mine[row - 1][col + 1]\ 68 + mine[row][col - 1] + mine[row][col + 1]\ 69 + mine[row + 1][col - 1] + mine[row + 1][col] + mine[row +1][col + 1]-7*'0'; 70 } 71 void expand(char mine[ROW][COL], char board[ROW][COL], int x, int y){ 72 if ((x >= 1) && (y >= 1) && (x <= ROW) && (y <= COL)) 73 { 74 if (GetMines(mine, x, y) == '0') 75 { 76 if (x > 1 && x < 10 && y>1 && y < 10) 77 { 78 count = count - 8; 79 } 80 else if((x==1&&y==1)||(x==10&&y==10) || (x == 1 && y == 10) || (x == 10 && y == 1)) { 81 count -= 3; 82 } 83 else { 84 count -= 5; 85 } 86 board[x - 1][y - 1] = GetMines(mine, x-1, y-1); 87 board[x - 1][y] = GetMines(mine, x - 1, y); 88 board[x - 1][y + 1] = GetMines(mine, x - 1, y + 1); 89 board[x][y - 1] = GetMines(mine, x , y - 1); 90 board[x][y + 1] = GetMines(mine, x , y + 1); 91 board[x + 1][y - 1] = GetMines(mine, x + 1, y - 1); 92 board[x + 1][y] = GetMines(mine, x + 1, y); 93 board[x + 1][y + 1] = GetMines(mine, x + 1, y + 1); 94 } 95 } 96 } 97 void Game(){ 98 char mine[ROW][COL]; 99 char board[ROW][COL]; 100 memset(mine,'0',sizeof(mine)); 101 memset(board, '*', sizeof(board)); 102 layout(mine, ROW, COL); 103 Board(mine, ROW, COL); 104 int x = 0; 105 int y = 0; 106 while (1){ 107 int i = 0; 108 Board(board, ROW, COL); 109 printf("请选择您要排除的位置: "); 110 scanf("%d %d", &x, &y); 111 if (x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2){ 112 if (mine[x][y] == '0'){ 113 char num = GetMines(mine,x,y); 114 board[x][y] = num; 115 expand(mine, board, x, y); 116 Board(board, ROW, COL); 117 count--; 118 if (count == MINE_NUM) 119 { 120 Board(board, ROW, COL); 121 printf("你赢了!\n"); 122 break; 123 } 124 } 125 else{ 126 printf("您输了!\n"); 127 Board(mine, ROW, COL); 128 break; 129 } 130 printf("还有%d个位置 \n", count); 131 } 132 else{ 133 printf("你输入的坐标有误,请从新输入!\n"); 134 } 135 } 136 } 137 int main(){ 138 int quit = 0; 139 int select = 0; 140 while (!quit){ 141 inter(); 142 scanf("%d", &select); 143 switch (select) 144 { 145 case 1: 146 Game(); 147 Sleep(5000); 148 system("cls"); 149 break; 150 case 2: 151 printf("再见!\n"); 152 quit = 1; 153 break; 154 default: 155 printf("您的输入不正确,请从新输入!\n"); 156 break; 157 } 158 } 159 system("pause"); 160 return 0; 161 }
以上就是本文的所有内容,但愿对你们的学习有所帮助,也但愿你们多多关注支持。