C語言實(shí)現(xiàn)簡單的掃雷游戲
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)簡單掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
前言
掃雷游戲規(guī)則:
1、踩過所有非雷格子即掃雷成功,踩到雷即游戲結(jié)束。
2、點(diǎn)擊方格,如果出現(xiàn)數(shù)字,數(shù)字表示這個(gè)格子周圍八個(gè)格子的雷的個(gè)數(shù)。
一、如何實(shí)現(xiàn)?
1.如何實(shí)現(xiàn)雷與雷周圍的信息不沖突?
如果采用一個(gè)二維數(shù)組,1表示雷,0表示非雷,那么某一坐標(biāo)周圍如果雷的個(gè)數(shù)是1,就會(huì)與前面沖突,所以設(shè)定兩個(gè)字符型(char)數(shù)組,數(shù)組mine用來存儲(chǔ)雷的信息,數(shù)組show用來存放排查出來的雷的信息(周圍八個(gè)格子中雷的個(gè)數(shù))
2.如何避免使用數(shù)組時(shí)越界?
如果設(shè)置格子的大小為9×9,在查找邊界格子(如下圖紅框內(nèi)的格子)周圍的雷的個(gè)數(shù)時(shí)會(huì)越界查找,為避免越界查找,將格子擴(kuò)大一圈,但是只使用中間9×9部分的格子。

3.如何實(shí)現(xiàn)點(diǎn)擊一個(gè)格子展開一片的效果?
1、首先,要了解展開一片的條件,當(dāng)選擇的格子不是雷,并且格子周圍沒有雷時(shí),才會(huì)有展開一片的效果。
2、采用遞歸的方式實(shí)現(xiàn),按照一定的方向依次尋找,直到某個(gè)格子周圍有雷時(shí)跳出,返回上一次遞歸。
遞歸要注意遞歸的截至條件,并且要注意數(shù)組的邊界。
二、具體代碼及實(shí)現(xiàn)過程
1.初始化棋盤
兩個(gè)字符數(shù)組大小相同,只是初始化的字符不同,所用采用同一個(gè)初始化函數(shù),通過傳參來確定初始化的字符。
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)//set為初始化的字符
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
2.打印棋盤
代碼如下(示例):打印棋盤也是通過傳參的方式,為了在選擇格子時(shí)方便,打印出行號列號
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col ; i++)
{
printf("%d ", i);//打印出列號
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印出行號
for (j = 1; j <= col; j++)
{
printf("%c ",arr[i][j]);
}
printf("\n");
}
}
3.放置雷
生成1-10間的隨機(jī)數(shù),利用隨機(jī)數(shù)確定放置雷的行和列,放置雷之前要判斷此位置是否被放置過,如果被放置過就換一個(gè)隨機(jī)數(shù),直至放滿指定的雷的個(gè)數(shù)。
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;//雷的總數(shù)
while (count)
{
int x = rand() % row + 1;//隨機(jī)產(chǎn)生行號
int y = rand() % col + 1;//隨機(jī)產(chǎn)生列號
if (mine[x][y] == '0')//沒被放置過雷
{
mine[x][y] = '1';
count--;
}
}
}
4.排雷
1、排雷首先要確定輸入的坐標(biāo)是否有效,無效坐標(biāo)需要重新輸入。
2、如果選擇的格子是雷,則游戲結(jié)束。
3、如果選擇的格子不是雷,并且格子周圍沒有雷,將show數(shù)組中對應(yīng)的格子置為空格,遞歸按照某一順序查找其他方向,遞歸的截止條件為格子周圍有雷存在。遞歸時(shí)要注意判斷格子沒有被查找過,并且要注意坐標(biāo)的范圍不能超過0-9,否則會(huì)展開出錯(cuò)。
void SearchMore(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y,int * win)//不是雷
{
while (mine[x][y] == '0' && show[x][y] == '*' && x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (Get_Mine_Count(mine, x, y) == 0)
{
show[x][y] = ' ';
(*win)++;
SearchMore(show, mine, x - 1, y, win);
SearchMore(show, mine, x - 1, y - 1, win);
SearchMore(show, mine, x, y - 1, win);
SearchMore(show, mine, x + 1, y - 1, win);
SearchMore(show, mine, x + 1, y, win);
SearchMore(show, mine, x + 1, y + 1, win);
SearchMore(show, mine, x, y + 1, win);
SearchMore(show, mine, x - 1, y + 1, win);
}
else
{
(*win)++;
show[x][y] = Get_Mine_Count(mine, x, y) + '0';
break;
}
}
}
4、判斷所選格子周圍的雷的個(gè)數(shù),采用坐標(biāo)相加的方法,因?yàn)閿?shù)組mine為字符數(shù)組,字符在內(nèi)存中存儲(chǔ)的是它的ASCII碼值,所以相加后減去8個(gè)字符‘0',即減去字符‘0'的ASCII碼值,就得到周圍雷的個(gè)數(shù)。代碼如下:
int Get_Mine_Count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
}
5、設(shè)置一個(gè)win,對非雷格子進(jìn)行計(jì)數(shù),如果win與非雷個(gè)數(shù)相等,則排雷成功。
5.運(yùn)行結(jié)果

6.源代碼
game.h
#include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //雷的數(shù)量 //初始化棋盤 void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set); //打印棋盤 void DisplayBoard(char arr[ROWS][COLS], int row, int col); //布置雷 void SetMine(char mine[ROWS][COLS], int row, int col); //排查雷 void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col);
main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("********************************************\n");
printf("************* 1. play **************\n");
printf("************* 0. exit **************\n");
printf("********************************************\n");
}
void game()
{
char mine[ROWS][COLS];//存放雷的信息
char show[ROWS][COLS];//存放排查出雷的信息
InitBoard(mine, ROWS, COLS,'0');//
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL);//布置雷
DisplayBoard(show, ROW, COL);
FindMine(show,mine, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("請選擇->");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("選擇錯(cuò)誤,請重新選擇?。?!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)//set為初始化的字符
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col ; i++)
{
printf("%d ", i);//打印出列號
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印出行號
for (j = 1; j <= col; j++)
{
printf("%c ",arr[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;//雷的總數(shù)
while (count)
{
int x = rand() % row + 1;//隨機(jī)產(chǎn)生行號
int y = rand() % col + 1;//隨機(jī)產(chǎn)生列號
if (mine[x][y] == '0')//沒被放置過雷
{
mine[x][y] = '1';
count--;
}
}
}
int Get_Mine_Count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
}
void SearchMore(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y,int * win)//不是雷
{
while (mine[x][y] == '0' && show[x][y] == '*' && x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (Get_Mine_Count(mine, x, y) == 0)
{
show[x][y] = ' ';
(*win)++;
SearchMore(show, mine, x - 1, y, win);
SearchMore(show, mine, x - 1, y - 1, win);
SearchMore(show, mine, x, y - 1, win);
SearchMore(show, mine, x + 1, y - 1, win);
SearchMore(show, mine, x + 1, y, win);
SearchMore(show, mine, x + 1, y + 1, win);
SearchMore(show, mine, x, y + 1, win);
SearchMore(show, mine, x - 1, y + 1, win);
}
else
{
(*win)++;
show[x][y] = Get_Mine_Count(mine, x, y) + '0';
break;
}
}
}
void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("請輸入要排查的坐標(biāo)->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遺憾,你被炸死了......\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
SearchMore(show, mine, x, y, &win);
DisplayBoard(show,ROW,COL);
}
}
else
{
printf("坐標(biāo)非法,請重新輸入->");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功?。?!");
DisplayBoard(mine, ROW, COL);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實(shí)現(xiàn)簡單的抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
C++使用printf語句實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的示例代碼
在C語言中,printf 函數(shù)可以直接實(shí)現(xiàn)部分進(jìn)制轉(zhuǎn)換功能,通過格式說明符(format specifier)快速輸出不同進(jìn)制的數(shù)值,下面給大家分享C++使用printf語句實(shí)現(xiàn)進(jìn)制轉(zhuǎn)換的示例代碼,感興趣的朋友一起看看吧2025-04-04

