最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用C語言實現(xiàn)掃雷小程序

 更新時間:2022年06月07日 10:34:10   作者:一零 柒  
這篇文章主要為大家詳細介紹了用C語言實現(xiàn)掃雷小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言實現(xiàn)掃雷小程序的具體代碼,供大家參考,具體內容如下

掃雷程序的編寫需要有清晰的思路,所以我們先要清楚掃雷的實現(xiàn)有幾個功能模塊讓我們編寫,再用主函數(shù)將功能結合在一起:

//菜單函數(shù)
//初始化數(shù)組函數(shù)
//布雷函數(shù)
//統(tǒng)計周圍雷的個數(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();//打印設計者棋盤?
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ù)的大體框架程序寫出來,在依次向各個函數(shù)塊里面充填程序,以下為主函數(shù):*

#include"lei.h"

void game()
{

? ? int ret = 0;
? ? init_mine();//初始化玩家棋盤和設計者棋盤
? ? set_mine();//給設計者棋盤布雷
? ? print_mine();//打印設計者棋盤(可不打印)
? ? 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();//打印設計者棋盤
? ? ? ? ? ? printf("玩家贏!\n\n");

? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? if (ret)//判斷是否踩到雷
? ? ? ? {
? ? ? ? ? ? printf("被雷炸死\n");
? ? ? ? ? ? print_mine();//打印設計者雷陣查看雷的分布
? ? ? ? ? ? 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()//打印設計者棋盤
{
? ? 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()//給設計者棋盤布雷
{
? ? 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的范圍內布雷
? ? ? ? int y = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為1到10的范圍內布雷
? ? ? ? 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ù)字對應的ASCII值和數(shù)字字符對應的ASCII值相差48,即'0'的ASCII值
? ? ? ? ? ? ? ? open_mine(x, y);
? ? ? ? ? ? ? ? while (ret)//在其余有空的地方設置一個雷
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? int x = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為1到10的范圍內布雷
? ? ? ? ? ? ? ? ? ? int y = rand() % 10 + 1;//產(chǎn)生1到10的隨機數(shù),在數(shù)組下標為1到10的范圍內布雷
? ? ? ? ? ? ? ? ? ? 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ù)字對應的ASCII值和數(shù)字字符對應的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ù)字對應的ASCII值和數(shù)字字符對應的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;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C++中類的構造函數(shù)初始值列表解讀

    C++中類的構造函數(shù)初始值列表解讀

    這篇文章主要介紹了C++中類的構造函數(shù)初始值列表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C/C++ 獲取自身IP與域名片段的示例代碼

    C/C++ 獲取自身IP與域名片段的示例代碼

    這篇文章主要介紹了C/C++ 獲取自身IP與域名片段的示例代碼,幫助大家更好的理解和學習C/C++編程,感興趣的朋友可以了解下
    2020-10-10
  • 一文詳解C++中隱含的this指針

    一文詳解C++中隱含的this指針

    這篇文章主要帶大家詳細了解一下C++中隱含的this指針,文中通過代碼示例和圖文介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • 使用C語言編寫圣誕表白程序

    使用C語言編寫圣誕表白程序

    圣誕節(jié)快到了,讓我們用C語言制作一個圣誕表白程序吧,下面通過本文學習下實現(xiàn)代碼
    2016-12-12
  • C++?分割字符串數(shù)據(jù)的實現(xiàn)方法

    C++?分割字符串數(shù)據(jù)的實現(xiàn)方法

    這篇文章主要介紹了C++?分割字符串數(shù)據(jù)的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • Cocos2d-x UI開發(fā)之CCControlSlider控件類使用實例

    Cocos2d-x UI開發(fā)之CCControlSlider控件類使用實例

    這篇文章主要介紹了Cocos2d-x UI開發(fā)之CCControlSlider控件類使用實例,本文代碼中包含大量注釋講解了CCControlSlider控件類的使用,需要的朋友可以參考下
    2014-09-09
  • 超詳細解析C++實現(xiàn)歸并排序算法

    超詳細解析C++實現(xiàn)歸并排序算法

    歸并排序是比較穩(wěn)定的排序方法。它的基本思想是把待排序的元素分解成兩個規(guī)模大致相等的子序列。本文將用C++實現(xiàn)這一排序算法,需要的可以參考一下
    2022-09-09
  • Qt數(shù)據(jù)庫應用之實現(xiàn)通用數(shù)據(jù)生成器

    Qt數(shù)據(jù)庫應用之實現(xiàn)通用數(shù)據(jù)生成器

    有兩種應用場景需要用到數(shù)據(jù)生成器,一種是需要測試數(shù)據(jù)庫性能,一種是隨機模擬生成一堆數(shù)據(jù),用來測試程序的性能。本文將利用Qt實現(xiàn)通用數(shù)據(jù)生成器,需要的可以參考一下
    2022-02-02
  • C++中棧結構建立與操作詳細解析

    C++中棧結構建立與操作詳細解析

    我們可以把棧理解成一個大倉庫,放在倉庫門口(棧頂)的貨物會優(yōu)先被取出,然后再取出里面的貨物。而從數(shù)據(jù)的邏輯結構來看,棧結構起始就是一種線性結構
    2013-10-10
  • 一文搞懂Codec2解碼組件

    一文搞懂Codec2解碼組件

    這篇文章主要介紹了Codec2解碼組件,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09

最新評論

陆良县| 定兴县| 利津县| 赤壁市| 关岭| 高州市| 嘉禾县| 博爱县| 修武县| 依安县| 石林| 泸水县| 无棣县| 华安县| 马边| 通海县| 光山县| 涞水县| 织金县| 丹寨县| 马山县| 视频| 宣威市| 东乌珠穆沁旗| 广饶县| 弥渡县| 宜兴市| 古蔺县| 同德县| 南平市| 临武县| 潼南县| 嘉峪关市| 隆安县| 简阳市| 皋兰县| 门头沟区| 泸州市| 宜宾市| 双桥区| 兴宁市|