要求:
1>第一次下子,不炸死。
2>座標周圍沒雷,可以實現展開。
實現
1.用一個二維字符數組mine[ROWS] [COLS]來存儲雷,現在我們用字符1來表示有雷,字符0表示無雷。再用一個二維字符數組show[ROWS][COLS]將所有的元素初始化爲*,並打印作爲展現給玩家的。同時用show數組來表示對應的mine數組中周圍雷即字符0的個數。對於邊緣的格子無法計算雷的個數,因此只需再增加2行2列即可,即ROWS=COLS=12 。
2.第一次下子,不炸死
在設置雷前先讓玩家輸入一個座標(x,y),再設置雷,然後再計算周圍雷的個數,即相當於第一次無效。
3.座標周圍沒雷,可以實現展開。
可用遞歸函數實現。
4.此遊戲還存在一個問題,若確定一個座標不是雷,則可重複輸入此座標直到玩家勝利。因爲計數器自增的條件是隻要滿足x,y對應的位置爲非雷。此時只需增加一個循環即可。
while (show[x][y]!='*')
{
printf("輸入非法,請重新輸入:");
scanf("%d%d", &x, &y);
printf("\n");
}
1 game.h
game.h
#define _CRT_SECURE_NO_WARNINGS
#ifndef __GAME_H__
#define __GAME_H__
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ROW 10
#define COL 10
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
void InitBoard(char arr[ROWS][COLS], int row, int col, char set);
void Display(char arr[ROWS][COLS], int row, int col);
void Setmine(char arr[ROWS][COLS], int row, int col);
void blank(char mine[ROWS][COLS], char show[ROWS][COLS] ,int x, int y);
#endif //__GAME_H__
2 test.c
test.c
#include "game.h"
int GetMineCount(char mine[ROWS][COLS], int x, int y)//求mine[x][y]周圍8個元素中雷的總個數
{
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]-7*'0';
}
void game()
{
int ret = 0;
int count = 0;
int x = 0;
int y = 0;
char show[ROWS][COLS];
char mine[ROWS][COLS];
InitBoard(mine, ROWS, COLS, '0');//初始化數組,全置爲字符0
InitBoard(show, ROWS, COLS, '*');//初始化數組
Display(show, ROWS, COLS);//打印數組,注意打印的行和列都是序號1到10的數組元素
Display(mine, ROWS, COLS);//打印雷的具體分佈,便於調試
flag: printf("請輸入座標(x,y)");
scanf("%d%d", &x,&y);
if (x>=1&&x<=10&&y>=1&&y<=10)//對輸入的合法性進行檢驗
{
Setmine(mine, ROWS, COLS);//隨機設置雷的位置
show[x][y] = GetMineCount(mine, x, y);//將(x,y)處*賦值爲雷的個數
blank(mine, show, x, y);//實現展開的函數,遞歸函數
}
else
{
printf("輸入有誤\n");
goto flag;
}
Display(show, ROWS, COLS);
Display(mine, ROWS, COLS);
while (count<ROW*COL-COUNT)
{
printf("請輸入座標(x,y)");
scanf("%d%d", &x,&y);
while (show[x][y]!='*')
{
printf("輸入非法,請重新輸入:");
scanf("%d%d", &x, &y);
printf("\n");
}
if (x>=1&&x<=10&&y>=1&&y<=10)
{
if (mine[x][y] == '0')
{
blank(mine, show, x, y);
Display(show, ROWS, COLS);
Display(mine, ROWS, COLS);
count++;
}
else
{
printf("踩雷了,失敗\n");
Display(mine, ROWS, COLS);
return ;
}
}
else
{
printf("輸入有誤\n");
}
}
printf("恭喜,掃雷成功!\n");
}
void menu()
{
printf("********************************\n");
printf("*********1.play 0.exit********\n");
printf("********************************\n");
}
void test()
{
int input = 0;
srand((unsigned) time(NULL));
do
{
menu();
printf("請選擇:");
scanf("%d", &input);
switch(input)
{
case 0:
break;
case 1:
game();
break;
default :
printf("輸入非法,請重新輸入\n");
}
}while(input);//可以不斷玩遊戲
}
int main()
{
test();
return 0;
}
3 game.c//函數具體實現
game.c
#include "game.h"
void InitBoard(char arr[ROWS][COLS], int row, int col, char set)//初始化數組函數
{
int i = 0;
int j = 0;
for (i=0; i<ROWS ; i++)
{
for (j=0; j<COLS; j++)
{
arr[i][j] = set;
}
}
}
void Display(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" ");
for (i=1; i<=row-2; i++)
{
printf("%d ", i);
}
printf("\n");
for (i=1; i<=row-2; i++)
{
printf("%2d ", i);
for (j=1; j<=col-2; j++)
{
printf("%c ",arr[i][j]);
}
printf("\n");
}
}
void Setmine(char arr[ROWS][COLS], int row, int col)
{
int count = 0;
int i = 0;
int j = 0;
while (count<COUNT)
{
i = rand()%10+1;
j = rand()%10+1;
if (arr[i][j]=='0')
{
arr[i][j] = '1';
count++;
}
}
}
void blank(char mine[ROWS][COLS], char show[ROWS][COLS] ,int x, int y)//遞歸調用
{
if (mine[x][y]=='0')
{
show[x][y] = GetMineCount(mine, x, y);
if (show[x][y] == '0')
{
show[x][y] = ' ';
if (show[x - 1][y - 1] == '*')
{
blank(mine, show, x - 1, y - 1);
}
if (show[x - 1][y] == '*')
{
blank(mine, show, x - 1, y);
}
if (show[x - 1][y + 1] == '*')
{
blank(mine, show, x - 1, y + 1);
}
if (show[x][y + 1] == '*')
{
blank(mine, show, x, y + 1);
}
if (show[x + 1][y + 1] == '*')
{
blank(mine, show, x + 1, y + 1);
}
if (show[x + 1][y] == '*')
{
blank(mine, show, x + 1, y);
}
if (show[x + 1][y - 1] == '*')
{
blank(mine, show, x + 1, y - 1);
}
if (show[x][y - 1] == '*')
{
blank(mine, show, x, y - 1);
}
}
}
}
)
此圖未運行完全。