用C語言實現(xiàn)掃雷小程序
本文實例為大家分享了C語言實現(xiàn)掃雷小程序的具體代碼,供大家參考,具體內(nèi)容如下
掃雷程序的編寫需要有清晰的思路,所以我們先要清楚掃雷的實現(xiàn)有幾個功能模塊讓我們編寫,再用主函數(shù)將功能結(jié)合在一起:
//菜單函數(shù)
//初始化數(shù)組函數(shù)
//布雷函數(shù)
//統(tǒng)計周圍雷的個數(shù)
//打印玩家棋盤
//打印設(shè)計者棋盤
//掃雷函數(shù)
//避免第一次被雷炸死的函數(shù)
//展開函數(shù)
//判斷玩家棋盤剩余未知區(qū)域的個數(shù)
根據(jù)這幾點可以寫出如下的頭文件:
#ifndef __GAME_H__ #define __GAME__H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #define row 12 #define col 12 #define COUNT 10//棋盤中雷的總數(shù) extern char show_mine[row][col];//展示數(shù)組 extern char real_mine[row][col];//布雷數(shù)組 void muen();//菜單函數(shù) void init_mine();//初始化數(shù)組函數(shù) void set_mine();//布雷函數(shù) int count_mine();//統(tǒng)計周圍雷的個數(shù) void print_player();//打印玩家棋盤 void print_mine();//打印設(shè)計者棋盤? int ?sweep_mine();//掃雷函數(shù) void safe_mine();//避免第一次被雷炸死的函數(shù) void open_mine(int x, int y);//展開函數(shù) int count_show_mine(); ///判斷玩家棋盤剩余未知區(qū)域的個數(shù) #endif ?//__GAME_H__
* 接下來要做的就是將主函數(shù)的大體框架程序?qū)懗鰜?,在依次向各個函數(shù)塊里面充填程序,以下為主函數(shù):*
#include"lei.h"
void game()
{
? ? int ret = 0;
? ? init_mine();//初始化玩家棋盤和設(shè)計者棋盤
? ? set_mine();//給設(shè)計者棋盤布雷
? ? print_mine();//打印設(shè)計者棋盤(可不打?。?
? ? printf("\n");
? ? print_player();//打印玩家棋盤
? ? 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)//若玩家棋盤的'*'個數(shù)為雷數(shù)時,掃雷完成,游戲勝利
? ? ? ? {
? ? ? ? ? ? print_mine();//打印設(shè)計者棋盤
? ? ? ? ? ? printf("玩家贏!\n\n");
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if (ret)//判斷是否踩到雷
? ? ? ? {
? ? ? ? ? ? printf("被雷炸死\n");
? ? ? ? ? ? print_mine();//打印設(shè)計者雷陣查看雷的分布
? ? ? ? ? ? break;
? ? ? ? }print_player();//打印玩家棋盤
? ? }
}
int main()
{
? ? srand((unsigned int)time(NULL));//產(chǎn)生隨機數(shù)生成器
? ? int input = 0;
? ? muen();//菜單
? ? do
? ? {
? ? ? ? scanf_s("%d", &input);
? ? ? ? switch (input)
? ? ? ? {
? ? ? ? case 1:game();
? ? ? ? ? ? break;
? ? ? ? case 0:exit(1);//退出游戲
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? printf("輸入錯誤,重新輸入\n");
? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? muen();
? ? ? ? printf("contiue?\n");
? ? } while (1);//循環(huán)玩游戲
? ? system("pause");
? ? return 0;
}接下來再依次實現(xiàn)主函數(shù)里面的各個函數(shù)塊:
#include"lei.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()//初始化兩個棋盤
{
? ? 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);//打印橫標(0--10)
? ? }
? ? printf("\n");
? ? for (i = 1; i <row - 2; i++)//打印豎標(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è)計者棋盤
{
? ? int i = 0;
? ? int j = 0;
? ? printf("0 ?");
? ? for (i = 1; i <row - 1; i++)
? ? {
? ? ? ? printf("%d ", i);//打印橫標(0--10)
? ? }
? ? printf("\n");
? ? for (i = 1; i <row - 2; i++)//打印豎標(1--10)
? ? {
? ? ? ? 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è)計者棋盤布雷
{
? ? int x = 0;
? ? int y = 0;
? ? int count = COUNT;//雷總數(shù)
? ? while (count)//雷布完后跳出循環(huán)
? ? {
? ? ? ? int x = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為1到10的范圍內(nèi)布雷
? ? ? ? int y = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為1到10的范圍內(nèi)布雷
? ? ? ? if (real_mine[x][y] == '0')//找不是雷的地方布雷
? ? ? ? {
? ? ? ? ? ? real_mine[x][y] = '1';
? ? ? ? ? ? count--;
? ? ? ? }
? ? }
}
int count_mine(int x, int y)//檢測周圍8個區(qū)域雷的個數(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 safe_mine()//避免第一次炸死
{
? ? int x = 0;
? ? int y = 0;
? ? char ch = 0;
? ? int count = 0;
? ? int ret = 1;
? ? printf("輸入坐標掃雷\n");
? ? while (1)
? ? {
? ? ? ? scanf_s("%d%d", &x, &y);//只能輸入1到10,輸入錯誤重新輸入
? ? ? ? if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標是否有誤
? ? ? ? {
? ? ? ? ? ? if (real_mine[x][y] == '1')//第一次踩到雷后補救
? ? ? ? ? ? {
? ? ? ? ? ? ? ? real_mine[x][y] = '0';
? ? ? ? ? ? ? ? char ch = count_mine(x, y);
? ? ? ? ? ? ? ? show_mine[x][y] = ch + '0';//數(shù)字對應(yīng)的ASCII值和數(shù)字字符對應(yīng)的ASCII值相差48,即'0'的ASCII值
? ? ? ? ? ? ? ? open_mine(x, y);
? ? ? ? ? ? ? ? while (ret)//在其余有空的地方設(shè)置一個雷
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int x = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為1到10的范圍內(nèi)布雷
? ? ? ? ? ? ? ? ? ? int y = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為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ù)字對應(yīng)的ASCII值和數(shù)字字符對應(yīng)的ASCII值相差48,即'0'的ASCII值
? ? ? ? ? ? ? ? open_mine(x, y);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else//坐標錯誤
? ? ? ? {
? ? ? ? ? ? printf("輸入錯誤重新輸入\n");
? ? ? ? }
? ? }
}
int sweep_mine()//掃雷函數(shù),踩到雷返回1,沒有踩到雷返回0
{
? ? int x = 0;
? ? int y = 0;
? ? int count = 0;
? ? printf("輸入坐標掃雷\n");
? ? scanf_s("%d%d", &x, &y);//只能輸入1到10
? ? if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標是否有誤,輸入錯誤重新輸入
? ? {
? ? ? ? if (real_mine[x][y] == '0')//沒踩到雷
? ? ? ? {
? ? ? ? ? ? char ch = count_mine(x, y);
? ? ? ? ? ? show_mine[x][y] = ch + '0';//數(shù)字對應(yīng)的ASCII值和數(shù)字字符對應(yīng)的ASCII值相差48,即'0'的ASCII值
? ? ? ? ? ? open_mine(x, y);
? ? ? ? ? ? if (count_show_mine() == COUNT)//判斷剩余未知區(qū)域的個數(shù),個數(shù)為雷數(shù)時玩家贏
? ? ? ? ? ? {
? ? ? ? ? ? ? ? print_mine();
? ? ? ? ? ? ? ? printf("玩家贏!\n\n");
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else if (real_mine[x][y] == '1')//踩到雷
? ? ? ? {
? ? ? ? ? ? return 1;
? ? ? ? }
? ? }
? ? else
? ? {
? ? ? ? printf("輸入錯誤重新輸入\n");
? ? }
? ? return 0;//沒踩到雷
}
void open_mine(int x, int y)//坐標周圍展開函數(shù)
{
? ? if (real_mine[x - 1][y - 1] == '0')
? ? {
? ? ? ? show_mine[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x - 1][y] == '0')
? ? {
? ? ? ? show_mine[x - 1][y] = count_mine(x - 1, y) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x - 1][y + 1] == '0')
? ? {
? ? ? ? show_mine[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x][y - 1] == '0')
? ? {
? ? ? ? show_mine[x][y - 1] = count_mine(x, y - 1) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x][y + 1] == '0')
? ? {
? ? ? ? show_mine[x][y + 1] = count_mine(x, y + 1) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x + 1][y - 1] == '0')
? ? {
? ? ? ? show_mine[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x + 1][y] == '0')
? ? {
? ? ? ? show_mine[x + 1][y] = count_mine(x + 1, y) + '0';//顯示該坐標周圍雷數(shù)
? ? }
? ? if (real_mine[x + 1][y + 1] == '0')
? ? {
? ? ? ? show_mine[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';//顯示該坐標周圍雷數(shù)
? ? }
}
int count_show_mine()//判斷剩余未知區(qū)域的個數(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;
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++右值引用與move和forward函數(shù)的使用詳解
為了支持移動操作,新標準引入了一種新的引用類型——右值引用(rvalue reference)。所謂右值引用就是必須綁定到右值的引用,這篇文章主要介紹了C++右值引用與move和forward的使用2022-08-08
Dev C++編譯時運行報錯source file not compile問題
這篇文章主要介紹了Dev C++編譯時運行報錯source file not compile問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

