标签:scan 检验 get 并且 随机数 数组 The 设计 set
1.建雷盘(9*9雷盘)
2.初始化雷盘,雷盘1每个位置初始化为字符0,雷盘2每个位置初始化为字符*。
3.为了检验,输出我们初始化好的雷盘。
4.向棋盘放雷,这块我们可以以雷数设计难度。摆雷是以随机数放入的,下面会附上代码,详见代码
5.扫雷。
详细说明一下扫雷的思想:
5.1.先定义int win = 0(用来判断结束的条件)当我们向控制台输入坐标后,先判断输入坐标是否合法,不合法继续执行5.1,合法执行5.2。
5.2.判断输入坐标是否是雷?如果是雷,直接输出“菜鸟,你踩雷了”,否则,执行5.3。
5.3.统计该位置周围八个的雷数,如果雷数不为0,将雷数转换成字符赋给该位置,否则执行5.4。
5.4.将上述坐标的周围八个坐标放入一个二维数组,递归判断,将周围不是雷全部置空,周围有雷的置为雷数(字符型)操作完之后执行5.5(详见下面代码)
5.5一遍输入后输出本次雷盘1,以便下次输入,执行5.6
5.6标记雷数,(我们用字符m表示该位置有雷)标记一次输出一次雷盘1,执行5.7(详见下面代码)
5.7转到5.1继续执行,结束条件就是(雷盘上m和*的个数之和小于等于雷数并且win == 9 *9 - 雷数),然后输出挑战成功
test.c
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include "game.h" #include <time.h> #include <stdlib.h> void game(){ char set1 = ‘0‘; char set2 = ‘*‘; //棋盘 char Mine[Rows][Cols] = { 0 }; //新建棋盘 char Show[Rows][Cols] = { 0 }; //初始化棋盘 Init(Mine, Rows, Cols,set1); Init(Show, Rows, Cols,set2); //展示 Display(Mine, Row, Col); Display(Show, Row, Col); //放雷 printf("请选择难度:>\n"); printf("1.简单——10\n"); printf("2.一般——20\n"); printf("3.困难——30\n"); int input = 0; int countMine = 0; scanf("%d", &input); switch (input){ case 1: countMine = EASY_COUNT; PutMine(Mine, Row, Col, countMine); //检查Mine printf("检查\n"); ShowMine(Mine, Row, Col); //扫雷 ClearMine(Mine,Show, Row, Col, countMine); break; case 2: countMine = GENERAL_COUNT; PutMine(Mine, Row, Col, countMine); //检查Mine printf("检查\n"); ShowMine(Mine, Row, Col); //扫雷 ClearMine(Mine, Show, Row, Col, countMine); break; case 3: countMine = HARD_COUNT; PutMine(Mine, Row, Col, countMine); //检查Mine printf("检查\n"); ShowMine(Mine, Row, Col); //扫雷 ClearMine(Mine, Show, Row, Col, countMine); break; default: printf("请重新输入:>"); } } void menu(){ printf("1..开始游戏\n"); printf("0..结束\n"); } void test(){ int input = 1; while (input){ printf("请选择:\n"); menu(); scanf("%d", &input); switch (input){ case 1: game(); break; case 0: break; default: printf("请重新输入"); break; } } } int main(){ srand((unsigned int)time(0)); test(); system("pause"); return 0; }
game.c
#define _CRT_SECURE_NO_WARNINGS #include "game.h" #include<string.h> #include <time.h> #include <stdio.h> #include <math.h> #include <stdlib.h> //初始化 void Init(char arr[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++){ arr[i][j] = set; } } printf("----------------------------\n"); } //展示 void Display(char arr[Rows][Cols], int row, int col){ int i = 0; int j = 0; for (int k = 0; k <= col; k++){ printf("%d ", k); } printf("\n"); for (i = 1; i <= row; i++){ printf("%d ", i); for (j = 1; j <= col; j++){ printf("%c ", arr[i][j]); } printf("\n"); } printf("----------------------------\n"); } //摆雷 void PutMine(char mine[Rows][Cols], int row, int col,int count){ int x = 0; int y = 0; while (count >0 ){ x = rand() % row + 1; y = rand() % col + 1; if (mine[x][y] == ‘0‘) { mine[x][y] = 1 + ‘0‘; count--; } } } //检查 void ShowMine(char arr[Rows][Cols], int row, int col){ int i = 0; int j = 0; for (int k = 0; k <= Col; k++){ printf("%d ", k); } printf("\n"); for (i = 1; i <= row; i++){ printf("%d ", i); for (j = 1; j <= col; j++){ printf("%c ", arr[i][j]); } printf("\n"); } } //获取x,y 坐标周围八个格子的雷数 int getMine(char mine[Rows][Cols], int x, int y){ return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * ‘0‘; } void OtherIsHaveMine(char mine[Rows][Cols], char show[Rows][Cols], int arr[8][2], int row, int col){ for (int i = 0; i <8; i++){ int x = arr[i][0]; int y = arr[i][1]; if (x>0 && x <= row&&y > 0 && y <= col&&(show[x][y] == ‘*‘)){ if (getMine(mine, x, y) == 0){ show[x][y] = ‘ ‘; int ch[8][2] = { x - 1, y, x - 1, y - 1, x, y - 1, x + 1, y - 1, x + 1, y, x + 1, y + 1, x, y + 1, x - 1, y + 1 }; OtherIsHaveMine(mine, show, ch, row, col); } else{ show[x][y] = getMine(mine, x, y) + ‘0‘; } } } } //是否要标记雷: 1代表标记雷(用字符m表示有雷) 0代表不标记继续扫雷 void SignMine(char show[Rows][Cols], int row, int col){ int input = 1; int x = 0; int y = 0; while (input){ printf("是否标记雷\n"); printf("1.标记\n"); printf("0.不标记\n"); scanf("%d", &input); switch (input){ case 0: printf("继续扫雷:\n"); break; case 1: while (1){ printf("请输入有雷的坐标:>\n"); scanf("%d", &x); scanf("%d", &y); if ((x > 0 && x <= row) && (y > 0 && y <= col) && show[x][y] == ‘*‘){ show[x][y] = ‘m‘; system("cls"); ShowMine(show, row, col); break; } else{ printf("请输入正确的坐标\n"); } } break; default: printf("请正确输入;>\n"); } } } //统计标记雷的个数,如果小于等于雷数,返回1;大于雷数返回0 int TotalSignMine(char show[Rows][Cols], int row, int col, int count){ int SignMineCount = 0; for (int i = 1; i <= row; i++){ for (int j = 1; j <= col; j++){ if (show[i][j] == ‘m‘){ SignMineCount++; } } } if (SignMineCount > count) return 0; return 1; } //扫雷 void ClearMine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col, int count){ int x = 0; int y = 0; while (1){ int win = 0; printf("请输入坐标:\n"); scanf("%d", &x); scanf("%d", &y); if ((x > 0 && x <= row) && (y > 0 && y <= col)&&show[x][y]==‘*‘){ //判断是否有雷 if(mine[x][y] == ‘0‘){ //没有雷统计该格子周围八个格子的雷数 show[x][y] = getMine(mine, x, y) + ‘0‘; //如果雷数等于0的话,递归判断其周围格子 if (show[x][y] == ‘0‘){ show[x][y] = ‘ ‘; //递归判断周围格子 int arr[8][2] = { x - 1, y, x - 1, y - 1, x, y - 1, x + 1, y - 1, x + 1, y, x + 1, y + 1, x, y + 1, x - 1, y + 1 }; OtherIsHaveMine(mine, show, arr, row, col); } for (int i = 1; i <= row; i++){ for (int j = 1; j <= col; j++){ //if (show[i][j] == ‘ ‘||(show[i][j]-‘0‘ <9&&show[i][j]-‘0‘>0)) if (show[i][j] != ‘*‘ || show[i][j] != ‘m‘ && show[i][j] != ‘*‘){ win++; } } } //标记雷数大于真正的雷数时,返回0 if (TotalSignMine(show, row, col, count)){ if (win == row*col - count){ printf("挑战成功sayoulala\n"); ShowMine(mine, row, col); break; } } else{ printf("你标记的雷有可能不是雷,请你在标记的地方找出不是雷的位置"); } system("cls"); ShowMine(show, row, col); //是否标记雷 SignMine(show, row, col); } else{ printf("很遗憾你被炸死了...........菜鸡\n"); ShowMine(mine, row, col); break; } } else{ printf("坐标输入不正确,"); } } }
game.h
#ifndef _GAME_H_ #define Row 9 #define Col 9 #define Rows Row+2 #define Cols Col+2 #define EASY_COUNT 10 #define GENERAL_COUNT 20 #define HARD_COUNT 30 //初始化 void Init(char arr[Rows][Cols], int rows, int cols,char Set); //展示 void Display(char arr[Rows][Cols], int row, int col); //摆雷 void PutMine(char arr[Rows][Cols], int row, int col, int count); //检查 void ShowMine(char arr[Rows][Cols], int row, int col); //扫雷 void ClearMine(char mine[Rows][Cols], char show[Rows][Cols], int row, int col, int count); //是否标记雷 void SignMine(char show[Rows][Cols], int row, int col); //周围没雷置空 void OtherIsHaveMine(char mine[Rows][Cols], char show[Rows][Cols], int arr[8][2], int row, int col); //统计标记雷的个数,如果小于等于雷数,返回1;大于雷数返回0 int TotalSignMine(char show[Rows][Cols], int row, int col, int count); #endif
在控制台操作的扫雷代码就是上面所写的,我试着玩了几局没有发现错误,如果被那位读者发现错误,请在评论区指出,我会第一时间修改,谢谢!
标签:scan 检验 get 并且 随机数 数组 The 设计 set
原文地址:https://www.cnblogs.com/duy666/p/10193289.html