本文主要是介绍扫雷代码优化(第一次踩雷不退出并且当前坐标的周围坐标没有雷时展开),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
分析
第一次循环不退出就是把第一次单独拿出来,之后循环次数再减一
而展开就是使用之前的Sum()函数将周围坐标雷的个数计算出来然后展示。这个部分我们再定义一个函数ShowMoreMine()来实现。
接下来继续看代码
test.h
#ifndef _TEST_
#define _TEST_
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<string.h>
#define ROW 12
#define COL 12
#define MINENUM 20
#pragma warning(disable:4996)void Game();void SetMine(char mine[][COL], int row, int col);void ShowBoard(char show[][COL], int row, int col);char Sum(char mine[][COL], int row, int col);int GetRandom(int start, int end);void ShowMoreMine(char show[][COL], char mine[][COL], int x,int y); //多声明一个函数#endif
main.c(没有变化)
#include "test.h"
void Menu()
{printf("********************************\n");printf("******1. play 2.exit ******\n");printf("********************************\n");}int main()
{int select = 0;int quit = 0;while (!quit){Menu();printf("please enter your choose");scanf("%d", &select);switch (select){case(1) :Game();break;case(2):printf("bye bye");quit = 1;break;default:printf("what you enter is wrong ,please enter again!");break;}}system("pause");getchar();return 0;}
test.c
#include "test.h"
int GetRandom(int start,int end){return rand() % (end - start + 1) + start;
}
void SetMine(char mine[][COL], int row, int col)
{srand((unsigned long)time(NULL));int count = 0;while (count <= MINENUM){int x = GetRandom(1, 10);int y = GetRandom(1, 10);if (mine[x][y] == '0'){mine[x][y] = '1';count++;}}}
void ShowBoard(char show[][COL], int row, int col)
{printf(" ");int i = 1;for (i=1; i <= 10; i++){printf("%6d", i);}printf("\n--------------------------------------------------------------\n");for (i = 1; i <= 10; i++){printf("%3d|", i);int j = 1;for (j = 1; j <= 10; j++)printf(" %c | ", show[i][j]);printf("\n");printf("---------------------------------------------------------------\n");}}
char Sum(char mine[][COL], int row, int col)
{return mine[row - 1][col - 1] + mine[row - 1][col] + mine[row - 1][col + 1] + mine[row][col - 1] \+mine[row][col + 1] + mine[row + 1][col - 1] + mine[row + 1][col] + mine[row + 1][col + 1] - 7 * '0' ;
}
**void ShowMoreMine(char show[][COL], char mine[][COL], int row, int col)
{if (mine[row - 1][col - 1] == '0'){show[row - 1][col - 1] = Sum(mine, row - 1, col - 1);}if (mine[row - 1][col] == '0'){show[row - 1][col] = Sum(mine, row - 1, col );}if (mine[row - 1][col + 1] == '0'){show[row - 1][col + 1] = Sum(mine, row - 1, col + 1);}if (mine[row][col - 1] == '0'){show[row][col - 1] = Sum(mine, row , col - 1);}if(mine[row][col + 1] == '0'){show[row][col + 1] = Sum(mine, row , col + 1);}if(mine[row + 1][col - 1] == '0'){show[row + 1][col - 1] = Sum(mine, row + 1, col - 1);}if (mine[row + 1][col] == '0'){show[row + 1][col] = Sum(mine, row +1, col );}if (mine[row + 1][col + 1] == '0'){show[row + 1][col + 1] = Sum(mine, row + 1, col +1);}}** // 将周围坐标展开void Game()
{char mine[ROW][COL];char show[ROW][COL];memset(mine, '0', sizeof(mine));memset(show, '*', sizeof(show));SetMine(mine, ROW, COL);int x = 0;int y = 0;int count = 80;int n=0;// 判断扫雷时的循环次数**while (1){printf("please enter what you choose first:");scanf("%d %d", &x, &y);if (x >= 1 && x <= ROW - 2 && y <= COL - 2 && y >= 1){if (mine[x][y] == '1'){show[x][y] = '#';printf("you meet mine first ,please follow me!\n"); //第一次踩雷将雷展示为#break;}else{char number = Sum(mine, x, y);show[x][y] = number;n=1; //标志第一次没有踩雷printf("what you choose is right,keep going!\n");break;}}else{printf("what you enter is wrong ,please enter again\n");}}**while (1){ShowBoard(show, ROW, COL);printf("please enter what you choose:");scanf("%d %d", &x, &y);if (x >= 1 && x <= ROW-2 && y <= COL-2 &&y >= 1){if (mine[x][y] =='1'){printf("you fail");break;}else{char num = Sum(mine, x, y);show[x][y] = num;if (num == '0'){ShowMoreMine(show, mine, x, y);}count--;if (n == 1) //如果n=1,表示第一次没有踩雷,循环次数减一{if (count < 0){printf("you win");break;}}else{if (count <= 0) // 否则第一次踩雷,循环次数不减一{printf("you win");break;}}}}else{printf("what you enter is wrong ,please enter again");}}
}
这篇关于扫雷代码优化(第一次踩雷不退出并且当前坐标的周围坐标没有雷时展开)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!