主要功能有:
成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的清水河網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!1.第一次下子不被炸死;
2.下子后顯示周圍布雷數(shù);
首先可以將掃雷的游戲界面看做一個(gè)二維數(shù)組;然后有限的雷隨機(jī)排布在這個(gè)二維數(shù)組中,玩家通過輸入坐標(biāo)來確定該坐標(biāo)對(duì)應(yīng)的二維數(shù)組坐標(biāo)是否是雷,若不是則顯示周圍布雷數(shù),這時(shí)考慮坐標(biāo)如果在數(shù)組的中間則少顯示周圍8個(gè)坐標(biāo)布雷情況,而在數(shù)組邊緣則小于8。因此我在創(chuàng)建數(shù)組時(shí)創(chuàng)建一個(gè)比所需數(shù)大2行2列。將所需數(shù)組視為中間數(shù)組即可;如圖:
創(chuàng)建test.c,game.c和game.h三個(gè)文件
代碼如下:
int main()
{
srand((unsigned int)time(NULL));//產(chǎn)生隨機(jī)數(shù)生成器
int input = 0;
do
{
muen();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("輸入錯(cuò)誤,重新輸入\n");
break;
}
} while (input);
system("pause");
return 0;
}
代碼如下;
void muen()
{
printf("*******************************\n");
printf("*****1.play 0.exit*******\n");
printf("*******************************\n");
}
void init_mine()//初始化兩個(gè)棋盤
{
int i = 0;
int j = 0;
for (int i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show_mine[i][j] = '*';
real_mine[i][j] = '0';
}
}
}
void print_player()//打印玩家棋盤
{
int i = 0;
int j = 0;
printf("0 ");
for (i = 1; i <row - 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <row - 2; i++)
{
printf("%d ", i);
for (j = 1; j < col - 1; j++)
{
printf("%c ", show_mine[i][j]);//玩家棋盤數(shù)組
}
printf("\n");
}
printf("10 ");//開始打印最后一行
for (i = 1; i < row - 1; i++)
{
printf("%c ", show_mine[10][i]);
}
printf("\n");
}
void print_mine()//打印設(shè)計(jì)者棋盤
{
int i = 0;
int j = 0;
printf("0 ");
for (i = 1; i <row - 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <row - 2; i++)
{
printf("%d ", i);
for (j = 1; j < col - 1; j++)
{
printf("%c ", real_mine[i][j]);
}
printf("\n");
}
printf("10 ");//開始打印最后一行
for (i = 1; i < row - 1; i++)
{
printf("%c ", real_mine[10][i]);
}
printf("\n");
}
效果如圖:
代碼如下:
void set_mine() //給設(shè)計(jì)者棋盤布雷
{
int x = 0;
int y = 0;
int count = COUNT; //雷總數(shù)
while (count) //雷布完后跳出循環(huán)
{
int x = rand() % 10 + 1; //產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
int y = rand() % 10 + 1; //產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
if (real_mine[x][y] == '0') //找不是雷的地方布雷
{
real_mine[x][y] = '1';
count--;
}
}
}
int sweep_mine()//掃雷函數(shù),踩到雷返回1,沒有踩到雷返回0
{
int x = 0;
int y = 0;
int count = 0;
printf("輸入坐標(biāo)掃雷\n");
scanf("%d%d", &x, &y);
if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標(biāo)是否有誤,輸入錯(cuò)誤重新輸入
{
if (real_mine[x][y] == '0')//沒踩到雷
{
char ch = count_mine(x, y);
show_mine[x][y] = ch + '0';//數(shù)字對(duì)應(yīng)的ASCII值和數(shù)字字符對(duì)應(yīng)的ASCII值相差48,即'0'的ASCII值
open_mine(x, y);
if (count_show_mine() == COUNT)//判斷剩余未知區(qū)域的個(gè)數(shù),個(gè)數(shù)為雷數(shù)時(shí)玩家贏
{
return 0;
}
}
else if (real_mine[x][y] == '1')//踩到雷
{
return 1;
}
}
else
{
printf("輸入錯(cuò)誤重新輸入\n");
}
return 0;//沒踩到雷
}
代碼如下:
void safe_mine()//避免第一次炸死
{
int x = 0;
int y = 0;
char ch = 0;
int count = 0;
int ret = 1;
printf("游戲開始\n");
printf("輸入坐標(biāo)掃雷\n");
while (1)
{
scanf("%d%d", &x, &y);//只能輸入1到10,輸入錯(cuò)誤重新輸入
if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標(biāo)是否有誤
{
if (real_mine[x][y] == '1')//第一次踩到雷后補(bǔ)救
{
real_mine[x][y] = '0';
char ch = count_mine(x, y);
show_mine[x][y] = ch + '0';//數(shù)字對(duì)應(yīng)的ASCII值和數(shù)字字符對(duì)應(yīng)的ASCII值相差48,即'0'的ASCII值
open_mine(x, y);
while (ret)//在其余有空的地方設(shè)置一個(gè)雷
{
int x = rand() % 10 + 1;//產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
int y = rand() % 10 + 1;//產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
if (real_mine[x][y] == '0')//找不是雷的地方布雷
{
real_mine[x][y] = '1';
ret--;
break;
}
}break;//跳出此函數(shù)
}
if (real_mine[x][y] == '0')
{
char ch = count_mine(x, y);
show_mine[x][y] = ch + '0';//數(shù)字對(duì)應(yīng)的ASCII值和數(shù)字字符對(duì)應(yīng)的ASCII值相差48,即'0'的ASCII值
open_mine(x, y);
break;
}
}
else//坐標(biāo)錯(cuò)誤
{
printf("輸入錯(cuò)誤重新輸入\n");
}
}
}
int count_mine(int x, int y)//檢測周圍8個(gè)區(qū)域雷的個(gè)數(shù)
{
int count = 0;
if (real_mine[x - 1][y - 1] == '1')
count++;
if (real_mine[x - 1][y] == '1')
count++;
if (real_mine[x - 1][y + 1] == '1')
count++;
if (real_mine[x][y - 1] == '1')
count++;
if (real_mine[x][y + 1] == '1')
count++;
if (real_mine[x + 1][y - 1] == '1')
count++;
if (real_mine[x + 1][y] == '1')
count++;
if (real_mine[x + 1][y + 1] == '1')
count++;
return count;
}
void open_mine(int x, int y)//坐標(biāo)周圍展開函數(shù)
{
if (real_mine[x - 1][y - 1] == '0')
{
show_mine[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x - 1][y] == '0')
{
show_mine[x - 1][y] = count_mine(x - 1, y) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x - 1][y + 1] == '0')
{
show_mine[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x][y - 1] == '0')
{
show_mine[x][y - 1] = count_mine(x, y - 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x][y + 1] == '0')
{
show_mine[x][y + 1] = count_mine(x, y + 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x + 1][y - 1] == '0')
{
show_mine[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x + 1][y] == '0')
{
show_mine[x + 1][y] = count_mine(x + 1, y) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x + 1][y + 1] == '0')
{
show_mine[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
}
代碼如下;
int count_show_mine()//判斷剩余未知區(qū)域的個(gè)數(shù),個(gè)數(shù)為雷數(shù)時(shí)玩家贏
{
int count = 0;
int i = 0;
int j = 0;
for (i = 1; i <= row - 2; i++)
{
for (j = 1; j <= col - 2; j++)
{
if (show_mine[i][j] == '*')
{
count++;
}
}
}
return count;
}
查看第一是否可以炸死(根據(jù)布雷圖,輸入有雷坐標(biāo)未被炸死):
第二步輸入有雷坐標(biāo):
判斷玩家贏;
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
double start, finish;
void game()
{
int ret = 0;
init_mine(); //初始化玩家棋盤和設(shè)計(jì)者棋盤
set_mine(); //給設(shè)計(jì)者棋盤布雷
print_mine(); //打印設(shè)計(jì)者棋盤(可不打?。? printf("\n");
print_player(); //打印玩家棋盤
start = clock();
safe_mine(); //避免第一次被炸死
if (count_show_mine() == COUNT)//一步就贏的情況
{
print_mine();
printf("玩家贏!\n\n");
return;
}
print_player();
while (1)//循環(huán)掃雷
{
int ret = sweep_mine();//掃雷,踩到雷返回1,沒有踩到雷返回0
if (count_show_mine() == COUNT)//若玩家棋盤的'*'個(gè)數(shù)為雷數(shù)時(shí),掃雷完成,游戲勝利
{
print_mine(); //打印設(shè)計(jì)者棋盤
printf("玩家贏!\n\n");
finish = clock();//取結(jié)束時(shí)間
printf("用時(shí)%d 秒\n", (int)(finish - start) / CLOCKS_PER_SEC);
break;
}
if (ret)//判斷是否踩到雷
{
printf("被雷炸死\t");
finish = clock();//取結(jié)束時(shí)間
printf("用時(shí)%d 秒\n", (int)(finish - start) / CLOCKS_PER_SEC);
print_mine();//打印設(shè)計(jì)者雷陣查看雷的分布
break;
}
print_player();//打印玩家棋盤
}
}
int main()
{
srand((unsigned int)time(NULL));//產(chǎn)生隨機(jī)數(shù)生成器
int input = 0;
do
{
muen();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("輸入錯(cuò)誤,重新輸入\n");
break;
}
} while (input);//循環(huán)玩游戲
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
char show_mine[row][col] = { 0 };
char real_mine[row][col] = { 0 };
void muen()
{
printf("*******************************\n");
printf("*****1.play 0.exit*******\n");
printf("*******************************\n");
}
void init_mine()//初始化兩個(gè)棋盤
{
int i = 0;
int j = 0;
for (int i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show_mine[i][j] = '*';
real_mine[i][j] = '0';
}
}
}
void print_player()//打印玩家棋盤
{
int i = 0;
int j = 0;
printf("0 ");
for (i = 1; i <row - 1; i++)
{
printf("%d ", i);//打印橫標(biāo)(0--10)
}
printf("\n");
for (i = 1; i <row - 2; i++)//打印豎標(biāo)(1--10)
{
printf("%d ", i);
for (j = 1; j < col - 1; j++)
{
printf("%c ", show_mine[i][j]);//玩家棋盤數(shù)組
}
printf("\n");
}
printf("10 ");//開始打印最后一行
for (i = 1; i < row - 1; i++)
{
printf("%c ", show_mine[10][i]);
}
printf("\n");
}
void print_mine()//打印設(shè)計(jì)者棋盤
{
int i = 0;
int j = 0;
printf("0 ");
for (i = 1; i <row - 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <row - 2; i++)
{
printf("%d ", i);
for (j = 1; j < col - 1; j++)
{
printf("%c ", real_mine[i][j]);
}
printf("\n");
}
printf("10 ");//開始打印最后一行
for (i = 1; i < row - 1; i++)
{
printf("%c ", real_mine[10][i]);
}
printf("\n");
}
void set_mine() //給設(shè)計(jì)者棋盤布雷
{
int x = 0;
int y = 0;
int count = COUNT; //雷總數(shù)
while (count) //雷布完后跳出循環(huán)
{
int x = rand() % 10 + 1; //產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
int y = rand() % 10 + 1; //產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
if (real_mine[x][y] == '0') //找不是雷的地方布雷
{
real_mine[x][y] = '1';
count--;
}
}
}
int sweep_mine()//掃雷函數(shù),踩到雷返回1,沒有踩到雷返回0
{
int x = 0;
int y = 0;
int count = 0;
printf("輸入坐標(biāo)掃雷\n");
scanf("%d%d", &x, &y);
if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標(biāo)是否有誤,輸入錯(cuò)誤重新輸入
{
if (real_mine[x][y] == '0')//沒踩到雷
{
char ch = count_mine(x, y);
show_mine[x][y] = ch + '0';//數(shù)字對(duì)應(yīng)的ASCII值和數(shù)字字符對(duì)應(yīng)的ASCII值相差48,即'0'的ASCII值
open_mine(x, y);
if (count_show_mine() == COUNT)//判斷剩余未知區(qū)域的個(gè)數(shù),個(gè)數(shù)為雷數(shù)時(shí)玩家贏
{
return 0;
}
}
else if (real_mine[x][y] == '1')//踩到雷
{
return 1;
}
}
else
{
printf("輸入錯(cuò)誤重新輸入\n");
}
return 0;//沒踩到雷
}
void safe_mine()//避免第一次炸死
{
int x = 0;
int y = 0;
char ch = 0;
int count = 0;
int ret = 1;
printf("游戲開始\n");
printf("輸入坐標(biāo)掃雷\n");
while (1)
{
scanf("%d%d", &x, &y);//只能輸入1到10,輸入錯(cuò)誤重新輸入
if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標(biāo)是否有誤
{
if (real_mine[x][y] == '1')//第一次踩到雷后補(bǔ)救
{
real_mine[x][y] = '0';
char ch = count_mine(x, y);
show_mine[x][y] = ch + '0';//數(shù)字對(duì)應(yīng)的ASCII值和數(shù)字字符對(duì)應(yīng)的ASCII值相差48,即'0'的ASCII值
open_mine(x, y);
while (ret)//在其余有空的地方設(shè)置一個(gè)雷
{
int x = rand() % 10 + 1;//產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
int y = rand() % 10 + 1;//產(chǎn)生1到10的隨機(jī)數(shù),在數(shù)組下標(biāo)為1到10的范圍內(nèi)布雷
if (real_mine[x][y] == '0')//找不是雷的地方布雷
{
real_mine[x][y] = '1';
ret--;
break;
}
}break;//跳出此函數(shù)
}
if (real_mine[x][y] == '0')
{
char ch = count_mine(x, y);
show_mine[x][y] = ch + '0';//數(shù)字對(duì)應(yīng)的ASCII值和數(shù)字字符對(duì)應(yīng)的ASCII值相差48,即'0'的ASCII值
open_mine(x, y);
break;
}
}
else//坐標(biāo)錯(cuò)誤
{
printf("輸入錯(cuò)誤重新輸入\n");
}
}
}
int count_mine(int x, int y)//檢測周圍8個(gè)區(qū)域雷的個(gè)數(shù)
{
int count = 0;
if (real_mine[x - 1][y - 1] == '1')
count++;
if (real_mine[x - 1][y] == '1')
count++;
if (real_mine[x - 1][y + 1] == '1')
count++;
if (real_mine[x][y - 1] == '1')
count++;
if (real_mine[x][y + 1] == '1')
count++;
if (real_mine[x + 1][y - 1] == '1')
count++;
if (real_mine[x + 1][y] == '1')
count++;
if (real_mine[x + 1][y + 1] == '1')
count++;
return count;
}
void open_mine(int x, int y)//坐標(biāo)周圍展開函數(shù)
{
if (real_mine[x - 1][y - 1] == '0')
{
show_mine[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x - 1][y] == '0')
{
show_mine[x - 1][y] = count_mine(x - 1, y) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x - 1][y + 1] == '0')
{
show_mine[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x][y - 1] == '0')
{
show_mine[x][y - 1] = count_mine(x, y - 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x][y + 1] == '0')
{
show_mine[x][y + 1] = count_mine(x, y + 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x + 1][y - 1] == '0')
{
show_mine[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x + 1][y] == '0')
{
show_mine[x + 1][y] = count_mine(x + 1, y) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
if (real_mine[x + 1][y + 1] == '0')
{
show_mine[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';//顯示該坐標(biāo)周圍雷數(shù)
}
}
int count_show_mine()//判斷剩余未知區(qū)域的個(gè)數(shù),個(gè)數(shù)為雷數(shù)時(shí)玩家贏
{
int count = 0;
int i = 0;
int j = 0;
for (i = 1; i <= row - 2; i++)
{
for (j = 1; j <= col - 2; j++)
{
if (show_mine[i][j] == '*')
{
count++;
}
}
}
return count;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define row 12
#define col 12
#define COUNT 10//棋盤中雷的總數(shù)
extern char real_mine[row][col];//布雷數(shù)組
void muen(); //菜單函數(shù)
void init_mine(); //初始化數(shù)組函數(shù)
void print_player(); //打印玩家棋盤
void print_mine(); //打印設(shè)計(jì)者棋盤
void set_mine(); //布雷函數(shù)
int count_mine(); //統(tǒng)計(jì)周圍雷的個(gè)數(shù)
void safe_mine(); //避免第一次被雷炸死的函數(shù)
int sweep_mine(); //掃雷函數(shù)
void open_mine(int x, int y);//展開函數(shù)
int count_show_mine(); //判斷玩家棋盤剩余未知區(qū)域的個(gè)數(shù)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁題目:掃雷的分析與實(shí)現(xiàn)——C語言-創(chuàng)新互聯(lián)
標(biāo)題URL:http://jinyejixie.com/article26/cshdcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站營銷、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)公司、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容