基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲
效果展示
開始的界面

輸入0結(jié)束程序

輸入1開始游戲

選擇標(biāo)記地雷或者選擇踩坐標(biāo)
輸入0標(biāo)記地雷模式

輸入坐標(biāo)

輸入1踩坐標(biāo)模式

輸入坐標(biāo)

在輸入坐標(biāo)處輸入0 0結(jié)束游戲

踩到炸彈,出現(xiàn)炸彈位置
(1表示炸彈的位置,0表示沒有炸彈的位置)

輸入0結(jié)束程序

輸入1重新開始游戲

勝利

輸入0結(jié)束程序

輸入1重新開始游戲

代碼
我創(chuàng)建了兩個(gè).c源文件,一個(gè).h頭文件
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
int main()
{
int exi = 0;
srand((unsigned int)time(NULL));
board();
printf("請(qǐng)輸入是否開始游戲:>");
scanf("%d", &exi);
do
{
switch (exi)
{
case 1:
{
game();
printf("是否輸入1重新開始游戲:>");
scanf("%d", &exi);
if (exi == 0)
{
printf("游戲結(jié)束");
}
break;
}
case 0:
{
printf("游戲結(jié)束");
break;
}
default:
{
printf("輸入錯(cuò)誤,請(qǐng)重新輸入:>");
scanf("%d", &exi);
if (exi == 0)
{
printf("游戲結(jié)束\n");
}
break;
}
}
} while (exi);
return 0;
}
game.h
#pragma once #include<stdio.h> #include<time.h> #include<stdlib.h> #define WID 9 #define LON 9 #define WIDS WID+2 #define LONS LON+2 #define RAN 5 void board(); //打印開始的面板 void game(); //游戲運(yùn)行的起點(diǎn) void initialization(char mane[WIDS][LONS], char siz, int x, int y); //把數(shù)組內(nèi)框初始化為siz void display(char mane[WIDS][LONS], int x, int y); //打印數(shù)組內(nèi)框的字符 void random(char mane[WIDS][LONS], int count); //在數(shù)組中隨機(jī)賦予count個(gè)炸彈 int look(char mane[WIDS][LONS], int x, int y); //計(jì)算mane數(shù)組x,y位置周圍有多少炸彈 void judge(char mane[WIDS][LONS], char show[WIDS][LONS],char include[WIDS][LONS]); //判斷輸入是否獲得勝利 void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS], int X, int Y); //判斷周圍沒有雷,會(huì)向外繼續(xù)推,直到出現(xiàn)雷 void change(char show[WIDS][LONS], int x, int y, char siz); //改變數(shù)組show位置(x,y)為字符siz void jishu(); //統(tǒng)計(jì)選擇了幾次的位置,包括類推的位置,實(shí)現(xiàn)一點(diǎn)出現(xiàn)一大片的功能
game掃雷.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
static int a = 0;
void board()
{
printf("****************************\n");
printf("****************************\n");
printf("********* 1.play **********\n");
printf("********* 0.exit **********\n");
printf("****************************\n");
printf("****************************\n");
}
//數(shù)組初始化
void initialization(char mane[WIDS][LONS], char siz, int x, int y)
{
int i = 0;
for (i = 0; i <= x+1; i++)
{
int j = 0;
for (j = 0; j <= y+1; j++)
{
mane[i][j] = siz;
}
}
}
//打印第一個(gè)面板
void display(char mane[WIDS][LONS], int x,int y)
{
int i = 0;
int j = 0;
printf("-----------掃雷-----------\n");
printf("0 | ");
for (j = 1; j <= y; j++)
{
printf("%d ",j);
}
printf("\n");
printf("- - -");
for (j = 1; j <= y; j++)
{
printf(" -");
}
for (i = 1; i <= x; i++)
{
printf("\n");
printf("%d | ",i);
for (j = 1; j <= y; j++)
{
printf("%c ", mane[i][j]);
}
}
printf("\n-----------掃雷-----------\n");
}
void random(char mane[WIDS][LONS],int count)
{
int x = 0;
int y = 0;
while (count)
{
x = rand() % WID + 1;
y = rand() % LON + 1;
if (mane[x][y] == '0')
{
mane[x][y] = '1';
count--;
}
}
}
int look(char mane[WIDS][LONS],int x,int y)
{
return mane[x][y + 1] +
mane[x][y - 1] +
mane[x - 1][y + 1] +
mane[x - 1][y - 1] +
mane[x + 1][y + 1] +
mane[x + 1][y - 1] +
mane[x - 1][y] +
mane[x + 1][y]-8*'0';
}
void jishu()
{
a++;
}
void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS],int X,int Y)
{
if (include[X][Y] != '1')
{
int count = 0;
count = look(mane, X, Y);
show[X][Y] = count + '0';
include[X][Y] = '1';
jishu();
if (count == 0)
{
xunhuan(mane, show, include, X + 1, Y + 1);
xunhuan(mane, show, include, X - 1, Y - 1);
xunhuan(mane, show, include, X + 1, Y);
xunhuan(mane, show, include, X - 1, Y);
xunhuan(mane, show, include, X, Y + 1);
xunhuan(mane, show, include, X, Y - 1);
xunhuan(mane, show, include, X + 1, Y - 1);
xunhuan(mane, show, include, X - 1, Y + 1);
}
}
}
void change(char show[WIDS][LONS], int x, int y,char siz)
{
show[x][y] = siz;
}
void judge(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS])
{
int X = 0;
int Y = 0;
display(show, WID, LON);
do
{
int num = a;
if (num == WID * LON - RAN)
{
printf("恭喜你獲得勝利!\n\n");
display(mane, WID, LON);
break;
}
printf("想要標(biāo)記地雷就輸入0,想要選擇就輸入1):>");
int choose = 0;
scanf("%d", &choose);
printf("\n");
if (choose==1)
{
printf("輸入0 0結(jié)束游戲\n");
printf("請(qǐng)輸入你選擇的坐標(biāo):>");
scanf("%d%d", &X, &Y);
if (X == 0 && Y == 0)
{
printf("\n結(jié)束此次游戲\n\n");
break;
}
if (X >= 1 && X <= 9 && Y >= 1 && Y <= 9)
{
if (mane[X][Y] == '1')
{
printf("\n你吃到炸彈啦,死翹翹了\n\n");
display(mane, WID, LON);
break;
}
else
{
xunhuan(mane, show, include, X, Y);
display(show, WID, LON);
//display(mane, WID, LON);
}
}
else
{
printf("\n你輸?shù)某^范圍啦,");
}
}
else
{
printf("\n輸入0 0結(jié)束游戲\n");
printf("請(qǐng)輸入你選擇的坐標(biāo):>");
scanf("%d%d", &X, &Y);
if (X == 0 && Y == 0)
{
printf("\n結(jié)束此次游戲\n\n");
break;
}
change(show,X,Y,'F');
display(show, WID, LON);
}
} while (1);
}
void chu(char mane[WIDS][LONS], char siz,int x, int y)
{
int i = 0;
for (i = 1; i <= x ; i++)
{
int j = 0;
for (j = 1; j <= y ; j++)
{
mane[i][j] = siz;
}
}
}
void game()
{
char mane[WIDS][LONS];
char show[WIDS][LONS];
char include[WIDS][LONS];
initialization(mane, '0', WID, LON);
initialization(show, '*', WID, LON);
initialization(include, '1', WID, LON);
chu(include, '0', WID, LON);
random(mane,RAN);
//display(mane, WID, LON);
//display(show, WID, LON);
judge(mane,show,include);
}
我寫的這個(gè)小游戲還很粗糙,不過才開始學(xué),進(jìn)步空間還是很大的,代碼就上傳到gitee了
以上就是基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲的詳細(xì)內(nèi)容,更多關(guān)于C語(yǔ)言 掃雷的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C語(yǔ)言掃雷排雷小游戲?qū)崿F(xiàn)全程
- C語(yǔ)言實(shí)現(xiàn)經(jīng)典掃雷小游戲完整代碼(遞歸展開?+?選擇標(biāo)記)
- C語(yǔ)言實(shí)現(xiàn)掃雷小游戲(擴(kuò)展版)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的掃雷小游戲
- C語(yǔ)言實(shí)現(xiàn)掃雷游戲詳細(xì)流程
- C語(yǔ)言實(shí)現(xiàn)自定義掃雷游戲(遞歸版)
- C語(yǔ)言實(shí)現(xiàn)第一次防死版掃雷游戲
- C語(yǔ)言實(shí)現(xiàn)遞歸版掃雷游戲?qū)嵗?/a>
- C語(yǔ)言詳細(xì)講解通過遞歸實(shí)現(xiàn)掃雷的展開
相關(guān)文章
C++如何實(shí)現(xiàn)BCD碼和ASCII碼的相互轉(zhuǎn)換
這篇文章主要介紹了C++實(shí)現(xiàn)BCD碼和ASCII碼互轉(zhuǎn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
C++?MiniZip實(shí)現(xiàn)目錄壓縮與解壓的示例詳解
Zlib是一個(gè)開源的數(shù)據(jù)壓縮庫(kù),提供了一種通用的數(shù)據(jù)壓縮和解壓縮算法,本文主要為大家詳細(xì)介紹了如何利用Zlib實(shí)現(xiàn)目錄壓縮與解壓,需要的小伙伴可以參考下2023-11-11
include包含頭文件的語(yǔ)句中,雙引號(hào)和尖括號(hào)的區(qū)別(詳解)
下面小編就為大家?guī)?lái)一篇include包含頭文件的語(yǔ)句中,雙引號(hào)和尖括號(hào)的區(qū)別(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-07-07
c語(yǔ)言實(shí)現(xiàn)含遞歸清場(chǎng)版掃雷游戲
掃雷大家應(yīng)該都玩過,這是一個(gè)十分經(jīng)典的游戲,下面這篇文章主要給大家介紹了關(guān)于c語(yǔ)言實(shí)現(xiàn)含遞歸清場(chǎng)版掃雷游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11

