更加完善的扫雷游戏(升级版)
比上一期博客增加了“炸一片”和标记,取消标记的功能哦!!!
文章目录
前言
完善了上一篇博客的不足。
一、周围雷数量为0,连续自动翻棋子
扫雷不是有点击一个棋子,若是显示为0,表示周围都不是雷,这样周围几个棋子都会自动翻开来,若是周围的又有显示为0的,有把周围的自动翻开,依次如此;这样的功能我称为“炸屏”,有时候真的一炸一大片的,到时候就更好排雷;雷我放了10个,上期博客我有写的,都是很详细的;
代码如下:
void Findmines(char mine[Rows][Cols], char show[Rows][Cols], int x, int y) {if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {return ;}if (show[x][y] != '*')return;if (show[x][y] == '*') {int ret = minenum(mine, x, y);show[x][y] = ret + '0';}if (show[x][y] == '0') { Findmines(mine, show, x - 1, y - 1);Findmines(mine, show, x - 1, y);Findmines(mine, show, x - 1, y + 1);Findmines(mine, show, x, y - 1);Findmines(mine, show, x, y + 1);Findmines(mine, show, x + 1, y - 1);Findmines(mine, show, x + 1, y);Findmines(mine, show, x + 1, y + 1);}
二、标记雷和取消标记
标记和取消功能差不多,当我们能够确定雷的位置的时候我们就可以标记雷的位置了,当我们标记错误位置了就可以取消标记;代码实现如下:
void concel(char show[Rows][Cols], int x, int y) {if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {printf("坐标非法,请重新输入:\n");return;}if (show[x][y] != '!')return;if (show[x][y] == '!') {show[x][y] = '*';Initshow(show, Row, Col);}}
void add(char show[Rows][Cols], int x, int y) {
if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {printf("坐标非法,请重新输入:\n");return;}if (show[x][y] != '*')return;if (show[x][y] == '*') {show[x][y] = '!';Initshow(show, Row, Col);;}}
3.总代码如下
game.c
#include "game.h"
void Mine(char board[Rows][Cols], int rows, int cols, char set) {int i = 0, j = 0;for (i = 0; i < rows; i++) {for (j = 0; j < cols; j++) {board[i][j] = set;}}
}
void Initshow(char show[Rows][Cols], int row, int col) {int i = 0, j = 0;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 ", show[i][j]);}printf("\n");}printf("--------欢迎来到扫雷游戏--------\n");
}
void Mineset(char mine[Rows][Cols], int row, int col) {int n = mines;while (n) {int x = rand() % row + 1;int y = rand() % col + 1;if (x <= row && x >= 1 && y <= col && y >= 1) {mine[x][y] = '1';n--;}}
}
void concel(char show[Rows][Cols], int x, int y) {if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {printf("坐标非法,请重新输入:\n");return;}if (show[x][y] != '!')return;if (show[x][y] == '!') {show[x][y] = '*';Initshow(show, Row, Col);}}
void add(char show[Rows][Cols], int x, int y) {
if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {printf("坐标非法,请重新输入:\n");return;}if (show[x][y] != '*')return;if (show[x][y] == '*') {show[x][y] = '!';Initshow(show, Row, Col);;}}
int minenum(char mine[Rows][Cols], int x, int y) {return mine[x - 1][y - 1] + mine[x - 1][y]+ mine[x - 1][y + 1] + mine[x][y - 1]+ mine[x][y + 1] + mine[x + 1][y - 1]+ mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';}
void Checkmines(char show[Rows][Cols],int* x, int* y) {int i = 0, j = 0;for (i = 0; i < Row; i++) {for (j = 0; j < Col; j++) {if (show[i][j] != '*')x++;if (show[i][j] == '!')y++;}}
}
void Findmines(char mine[Rows][Cols], char show[Rows][Cols], int x, int y) {if (x == 0 || y == 0 || x == Rows - 1 || y == Cols - 1) {return ;}if (show[x][y] != '*')return;if (show[x][y] == '*') {int ret = minenum(mine, x, y);show[x][y] = ret + '0';}if (show[x][y] == '0') { Findmines(mine, show, x - 1, y - 1);Findmines(mine, show, x - 1, y);Findmines(mine, show, x - 1, y + 1);Findmines(mine, show, x, y - 1);Findmines(mine, show, x, y + 1);Findmines(mine, show, x + 1, y - 1);Findmines(mine, show, x + 1, y);Findmines(mine, show, x + 1, y + 1);}}
void menu1() {printf("***************************\n");printf("**** 0.退出游戏 ****\n");printf("**** 3.标记雷 ****\n");printf("**** 4.取消标记 ****\n");printf("**** 5.继续游戏 ****\n");printf("***************************\n");
}
void Findmine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col) {int rets = 0,qi=0;while (1) {int x = 0, y = 0;printf("请输入坐标>\n");scanf("%d%d", &x, &y);if (x <= row && x >= 1 && y <= col && y >= 1) {Findmines(mine, show, x, y);Initshow(show, Row, Col);if (mine[x][y] == '1') {printf("肯定是失误了,你怎么可能会输?只能再来一局了\n");show[x][y] = '1';Initshow(mine, Row, Col);return;}}else{printf("输入坐标非法,请重新输入:\n");}Checkmines(show,&rets, &qi);if (rets == Row * Col - mines || qi == mines){printf("恭喜你,简简单单赢了,小意思哦\n");return;}while (1) {menu1();int g = 0;printf("请输入>\n");scanf("%d", &g);if (g == 3){int x = 0, y = 0;printf("请输入坐标:\n");scanf("%d%d", &x, &y);add(show, x, y);}else if (g == 4){int x = 0, y = 0;printf("请输入坐标:\n");scanf("%d%d", &x, &y);concel(show, x, y);}else if (g == 5)break;elsegoto again;}}
again:return;
}
text.c
#include "game.h"
void menu() {printf("***************************\n");printf("**** 1.玩游戏 ****\n");printf("**** 0.退出游戏 ****\n");printf("***************************\n");
}
void game() {char mine[Rows][Cols] = { 0 };char show[Rows][Cols] = { 0 };Mine(mine, Rows, Cols, '0');Mine(show, Rows, Cols, '*');Initshow(show, Row, Col);Mineset(mine, Row, Col);//Initshow(mine, Row, Col);//Mineset(mine, Row, Col);Findmine(mine, show, Row, Col);
}
int main()
{int input = 0;srand((unsigned int)time(NULL));do {menu();printf("请输入>\n");scanf("%d", &input);switch (input) {case 1:game();break;case 0:printf("退出游戏\n");break;default:break;}} while (input);return 0;
}
game.h
#include
#include
#include
#include
#define mines 10
#define Row 9
#define Col 9
#define Rows Row+2
#define Cols Col+2
void Mine(char board[Rows][Cols], int rows , int cols, char set);
void Initshow(char show[Rows][Cols], int row, int col);
void Mineset(char mine[Rows][Cols], int row, int col);
void Findmine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col);
void Findmines(char mine[Rows][Cols], char show[Rows][Cols], int y, int x);
void Check(char show[Rows][Cols],int* x, int* y);
void add(char show[Rows][Cols], int x, int y);
void concel(char show[Rows][Cols], int x, int y);
总结
感谢大家的阅读了,这个改进的扫雷代码我觉得比上一期的代码优化了蛮多的,大家可以自行试试游戏体验,个人觉得还是更好了一些的;谢谢大家啦
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
