用C語(yǔ)言實(shí)現(xiàn)掃雷小程序
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)掃雷小程序的具體代碼,供大家參考,具體內(nèi)容如下
掃雷程序的編寫(xiě)需要有清晰的思路,所以我們先要清楚掃雷的實(shí)現(xiàn)有幾個(gè)功能模塊讓我們編寫(xiě),再用主函數(shù)將功能結(jié)合在一起:
//菜單函數(shù)
//初始化數(shù)組函數(shù)
//布雷函數(shù)
//統(tǒng)計(jì)周?chē)椎膫€(gè)數(shù)
//打印玩家棋盤(pán)
//打印設(shè)計(jì)者棋盤(pán)
//掃雷函數(shù)
//避免第一次被雷炸死的函數(shù)
//展開(kāi)函數(shù)
//判斷玩家棋盤(pán)剩余未知區(qū)域的個(gè)數(shù)
根據(jù)這幾點(diǎn)可以寫(xiě)出如下的頭文件:
#ifndef __GAME_H__ #define __GAME__H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #define row 12 #define col 12 #define COUNT 10//棋盤(pán)中雷的總數(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)計(jì)周?chē)椎膫€(gè)數(shù) void print_player();//打印玩家棋盤(pán) void print_mine();//打印設(shè)計(jì)者棋盤(pán)? int ?sweep_mine();//掃雷函數(shù) void safe_mine();//避免第一次被雷炸死的函數(shù) void open_mine(int x, int y);//展開(kāi)函數(shù) int count_show_mine(); ///判斷玩家棋盤(pán)剩余未知區(qū)域的個(gè)數(shù) #endif ?//__GAME_H__
* 接下來(lái)要做的就是將主函數(shù)的大體框架程序?qū)懗鰜?lái),在依次向各個(gè)函數(shù)塊里面充填程序,以下為主函數(shù):*
#include"lei.h" void game() { ? ? int ret = 0; ? ? init_mine();//初始化玩家棋盤(pán)和設(shè)計(jì)者棋盤(pán) ? ? set_mine();//給設(shè)計(jì)者棋盤(pán)布雷 ? ? print_mine();//打印設(shè)計(jì)者棋盤(pán)(可不打?。? ? ? printf("\n"); ? ? print_player();//打印玩家棋盤(pán) ? ? safe_mine();//避免第一次被炸死 ? ? if (count_show_mine() == COUNT)//一步就贏的情況 ? ? { ? ? ? ? print_mine(); ? ? ? ? printf("玩家贏!\n\n"); ? ? ? ? return; ? ? }print_player();打印玩家棋盤(pán) ? ? while (1)//循環(huán)掃雷 ? ? { ? ? ? ? int ret = sweep_mine();//掃雷,踩到雷返回1,沒(méi)有踩到雷返回0 ? ? ? ? if (count_show_mine() == COUNT)//若玩家棋盤(pán)的'*'個(gè)數(shù)為雷數(shù)時(shí),掃雷完成,游戲勝利 ? ? ? ? { ? ? ? ? ? ? print_mine();//打印設(shè)計(jì)者棋盤(pán) ? ? ? ? ? ? printf("玩家贏!\n\n"); ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? if (ret)//判斷是否踩到雷 ? ? ? ? { ? ? ? ? ? ? printf("被雷炸死\n"); ? ? ? ? ? ? print_mine();//打印設(shè)計(jì)者雷陣查看雷的分布 ? ? ? ? ? ? break; ? ? ? ? }print_player();//打印玩家棋盤(pán) ? ? } } int main() { ? ? srand((unsigned int)time(NULL));//產(chǎn)生隨機(jī)數(shù)生成器 ? ? int input = 0; ? ? muen();//菜單 ? ? do ? ? { ? ? ? ? scanf_s("%d", &input); ? ? ? ? switch (input) ? ? ? ? { ? ? ? ? case 1:game(); ? ? ? ? ? ? break; ? ? ? ? case 0:exit(1);//退出游戲 ? ? ? ? ? ? break; ? ? ? ? default: ? ? ? ? ? ? printf("輸入錯(cuò)誤,重新輸入\n"); ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? muen(); ? ? ? ? printf("contiue?\n"); ? ? } while (1);//循環(huán)玩游戲 ? ? system("pause"); ? ? return 0; }
接下來(lái)再依次實(shí)現(xiàn)主函數(shù)里面的各個(gè)函數(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()//初始化兩個(gè)棋盤(pán) { ? ? 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()//打印玩家棋盤(pán) { ? ? 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]);//玩家棋盤(pán)數(shù)組 ? ? ? ? } ? ? ? ? printf("\n"); ? ? } ? ? printf("10 ");//開(kāi)始打印最后一行 ? ? for (i = 1; i < row - 1; i++) ? ? { ? ? ? ? printf("%c ", show_mine[10][i]); ? ? } ? ? printf("\n"); } void print_mine()//打印設(shè)計(jì)者棋盤(pán) { ? ? 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 ", real_mine[i][j]); ? ? ? ? } ? ? ? ? printf("\n"); ? ? } ? ? printf("10 ");//開(kāi)始打印最后一行 ? ? for (i = 1; i < row - 1; i++) ? ? { ? ? ? ? printf("%c ", real_mine[10][i]); ? ? } ? ? printf("\n"); } void set_mine()//給設(shè)計(jì)者棋盤(pán)布雷 { ? ? 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 count_mine(int x, int y)//檢測(cè)周?chē)?個(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 safe_mine()//避免第一次炸死 { ? ? int x = 0; ? ? int y = 0; ? ? char ch = 0; ? ? int count = 0; ? ? int ret = 1; ? ? printf("輸入坐標(biāo)掃雷\n"); ? ? while (1) ? ? { ? ? ? ? scanf_s("%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 sweep_mine()//掃雷函數(shù),踩到雷返回1,沒(méi)有踩到雷返回0 { ? ? int x = 0; ? ? int y = 0; ? ? int count = 0; ? ? printf("輸入坐標(biāo)掃雷\n"); ? ? scanf_s("%d%d", &x, &y);//只能輸入1到10 ? ? if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判斷輸入坐標(biāo)是否有誤,輸入錯(cuò)誤重新輸入 ? ? { ? ? ? ? if (real_mine[x][y] == '0')//沒(méi)踩到雷 ? ? ? ? { ? ? ? ? ? ? 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í)玩家贏 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? print_mine(); ? ? ? ? ? ? ? ? printf("玩家贏!\n\n"); ? ? ? ? ? ? ? ? return 0; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? else if (real_mine[x][y] == '1')//踩到雷 ? ? ? ? { ? ? ? ? ? ? return 1; ? ? ? ? } ? ? } ? ? else ? ? { ? ? ? ? printf("輸入錯(cuò)誤重新輸入\n"); ? ? } ? ? return 0;//沒(méi)踩到雷 } void open_mine(int x, int y)//坐標(biāo)周?chē)归_(kāi)函數(shù) { ? ? if (real_mine[x - 1][y - 1] == '0') ? ? { ? ? ? ? show_mine[x - 1][y - 1] = count_mine(x - 1, y - 1) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x - 1][y] == '0') ? ? { ? ? ? ? show_mine[x - 1][y] = count_mine(x - 1, y) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x - 1][y + 1] == '0') ? ? { ? ? ? ? show_mine[x - 1][y + 1] = count_mine(x - 1, y + 1) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x][y - 1] == '0') ? ? { ? ? ? ? show_mine[x][y - 1] = count_mine(x, y - 1) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x][y + 1] == '0') ? ? { ? ? ? ? show_mine[x][y + 1] = count_mine(x, y + 1) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x + 1][y - 1] == '0') ? ? { ? ? ? ? show_mine[x + 1][y - 1] = count_mine(x + 1, y - 1) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x + 1][y] == '0') ? ? { ? ? ? ? show_mine[x + 1][y] = count_mine(x + 1, y) + '0';//顯示該坐標(biāo)周?chē)讛?shù) ? ? } ? ? if (real_mine[x + 1][y + 1] == '0') ? ? { ? ? ? ? show_mine[x + 1][y + 1] = count_mine(x + 1, y + 1) + '0';//顯示該坐標(biāo)周?chē)讛?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ī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于C語(yǔ)言實(shí)現(xiàn)的掃雷游戲代碼
- C語(yǔ)言開(kāi)發(fā)簡(jiǎn)易版掃雷小游戲
- C語(yǔ)言代碼實(shí)現(xiàn)掃雷游戲
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲
- C語(yǔ)言快速實(shí)現(xiàn)掃雷小游戲
- C語(yǔ)言 掃雷程序的實(shí)現(xiàn)
- C語(yǔ)言實(shí)現(xiàn)掃雷小游戲(適合初學(xué)者)
- C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單掃雷小程序
- C語(yǔ)言實(shí)現(xiàn)掃雷程序
相關(guān)文章
C++11 移動(dòng)構(gòu)造函數(shù)的使用
本文主要介紹了C++11 移動(dòng)構(gòu)造函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C++右值引用與move和forward函數(shù)的使用詳解
為了支持移動(dòng)操作,新標(biāo)準(zhǔn)引入了一種新的引用類(lèi)型——右值引用(rvalue reference)。所謂右值引用就是必須綁定到右值的引用,這篇文章主要介紹了C++右值引用與move和forward的使用2022-08-08C語(yǔ)言實(shí)現(xiàn)折半查找法(二分法)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)折半查找法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼
大家好,本篇文章主要講的是C++實(shí)現(xiàn)關(guān)機(jī)功能詳細(xì)代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2022-01-01C基礎(chǔ) 尋找隨機(jī)函數(shù)的G點(diǎn)詳解
下面小編就為大家?guī)?lái)一篇C基礎(chǔ) 尋找隨機(jī)函數(shù)的G點(diǎn)詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06Dev C++編譯時(shí)運(yùn)行報(bào)錯(cuò)source file not compile問(wèn)題
這篇文章主要介紹了Dev C++編譯時(shí)運(yùn)行報(bào)錯(cuò)source file not compile問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01