但是現在自己來編寫一個簡易版的掃雷,發現不是那麼簡單的,經過了幾天的編寫,終於實現了屬於我的簡易版的掃雷,過程簡單,對於我這菜鳥來說,難的部分就是怎麼去實現點一次一大片就空了(這裏用了遞歸)。接下來就是我的掃雷程序和截圖
先看看電腦的掃雷界面,和我的掃雷界面(發現自己的好醜)
玩過掃雷的都知道,有時候點一次會出現一大片空格,看看電腦的吧
看了上圖是不是覺得很簡單?但是我因爲實現這個功能,花了好久的時間,最後還是實現了
接下來是玩遊戲過程
踩到了雷後
下面就是代碼了(簡單易懂)
頭文件“game.h”
#ifndef __GAME_H__
#define __GAME_H__
#define COLS 11
#define ROWS 11
#define COL (COLS-2)
#define ROW (ROWS-2)
#define MAX 10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <windows.h>
void init_board(char mine[ROWS][COLS],char set, int row, int col);
void mine_set(char mine[ROWS][COLS]);
void display_board(char mine[ROWS][COLS], int row, int col);
void no_mine_display(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int *count);
#endif//__GAME_H__
test.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "game.h"
#include <windows.h>
//*********
//**********game begining**********
// **********
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
char set = '0';
int x = 0;
int y = 0;
int ret = 0;
int count =ROW*COL-MAX;
init_board(mine, '0', ROWS, COLS); //棋盤初始化
init_board(show, '\1', ROWS, COLS);
display_board(show, ROWS, COLS); //打印棋盤,別多想,雷隱藏了
mine_set(mine); //設置雷
// display_board(mine, ROWS, COLS);
while(count)
{
printf("請輸入你選擇的座標> ");
scanf("%d%d", &y, &x);
if(((x>=1)&&(x<=9)) && ((y>=1)&&(y<=9)))
{
if((count == COL*ROW-MAX) && (mine[x][y] == '1')) //第一次踩雷了,再給你次機會
{
init_board(mine, '0', ROWS, COLS);
mine_set(mine);
//display_board(mine, ROWS, COLS);
}
else if(mine[x][y] == '1') //又踩雷了?炸死你沒商量
{
printf("炸死你,沒理由\n");
display_board(mine, x, y);
break;
}
else // 多個連續地方沒雷咋辦,顯示空格,
{
no_mine_display(mine, show, x, y, &count);
display_board(show, x, y);
}
}
else
{
printf("座標超出範圍\n");
}
}
if(count == 0)
{
printf("恭喜您,掃雷任務完畢\1\1\1\1\1\n");
display_board(show, x, y);
}
}
enum test //枚舉,使程序更加靈活
{
EXIT,
PLAY
};
void menu()
{
printf("**************************\n");
printf("****1 play 0 exit******\n");
printf("**************************\n");
}
void test()
{
int input = 0;
do
{
menu();
printf("請選擇> ");
scanf("%d", &input);
switch(input)
{
case PLAY:
game();
break;
case EXIT:
break;
default:
printf("輸入有誤,請重新輸入> ");
}
}while(input);
}
int main()
{
test();
return 0;
}
game.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void init_board(char mine[ROWS][COLS], char set, int row, int col) //棋盤初始化
{
memset(mine, set, row*col*sizeof(char));
}
void display_board(char mine[ROWS][COLS], int row, int col) //打印棋盤
{
int i = 0;
int j = 0;
printf(" ");
for(i=1; i<=ROW; i++)
{
printf(" %d ", i);
}
printf("\n");
for(j=1; j<=COL; j++)
{
printf("%d ", j);
for(i=1; i<=ROW; i++)
{
printf(" %c ", mine[i][j]);
}
printf("\n");
}
}
void mine_set(char mine[ROWS][COLS]) //設置雷
{
int count = MAX;
srand((unsigned int)time(NULL));
while(count)
{
int x = rand()%9+1;
int y = rand()%9+1;
if(mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void no_mine_display(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int *count) //遞歸排雷由此開始
{
(*count)--;
if((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]+mine[x-1][y]-7*'0') == '0')
{
show[x][y] = ' ';
if(((x-1>0)&&(x-1<10))&&((y-1>0)&&(y-1<10))&&(show[x-1][y-1]=='\1'))
no_mine_display(mine,show, x-1, y-1,count);
if(((x>0)&&(x<10))&&((y-1>0)&&(y-1<10))&&(show[x][y-1]=='\1'))
no_mine_display(mine,show, x, y-1,count);
if(((x+1>0)&&(x+1<10))&&((y-1>0)&&(y-1<10))&&(show[x+1][y-1]=='\1'))
no_mine_display(mine,show, x+1, y-1,count);
if(((x+1>0)&&(x+1<10))&&((y>0)&&(y<10))&&(show[x+1][y]=='\1'))
no_mine_display(mine,show, x+1, y,count);
if(((x+1>0)&&(x+1<10))&&((y+1>0)&&(y+1<10))&&(show[x+1][y+1]=='\1'))
no_mine_display(mine,show, x+1, y+1,count);
if(((x>0)&&(x<10))&&((y+1>0)&&(y+1<10))&&(show[x][y+1]=='\1'))
no_mine_display(mine,show, x, y+1,count);
if(((x-1>0)&&(x-1<10))&&((y+1>0)&&(y+1<10))&&(show[x-1][y+1]=='\1'))
no_mine_display(mine,show, x-1, y+1,count);
if(((x-1>0)&&(x-1<10))&&((y>0)&&(y<10))&&(show[x-1][y]=='\1'))
no_mine_display(mine,show, x-1, y,count);
}
else show[x][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]+mine[x-1][y]-7*'0');
}