欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言實現(xiàn)簡單的掃雷游戲

 更新時間:2020年12月15日 15:26:05   作者:驚喜狍子  
這篇文章主要為大家詳細介紹了C語言實現(xiàn)簡單的掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言實現(xiàn)簡單掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下

前言

掃雷游戲規(guī)則:

1、踩過所有非雷格子即掃雷成功,踩到雷即游戲結(jié)束。
2、點擊方格,如果出現(xiàn)數(shù)字,數(shù)字表示這個格子周圍八個格子的雷的個數(shù)。

一、如何實現(xiàn)?

1.如何實現(xiàn)雷與雷周圍的信息不沖突?

如果采用一個二維數(shù)組,1表示雷,0表示非雷,那么某一坐標(biāo)周圍如果雷的個數(shù)是1,就會與前面沖突,所以設(shè)定兩個字符型(char)數(shù)組,數(shù)組mine用來存儲雷的信息,數(shù)組show用來存放排查出來的雷的信息(周圍八個格子中雷的個數(shù))

2.如何避免使用數(shù)組時越界?

如果設(shè)置格子的大小為9×9,在查找邊界格子(如下圖紅框內(nèi)的格子)周圍的雷的個數(shù)時會越界查找,為避免越界查找,將格子擴大一圈,但是只使用中間9×9部分的格子。

3.如何實現(xiàn)點擊一個格子展開一片的效果?

1、首先,要了解展開一片的條件,當(dāng)選擇的格子不是雷,并且格子周圍沒有雷時,才會有展開一片的效果。
2、采用遞歸的方式實現(xiàn),按照一定的方向依次尋找,直到某個格子周圍有雷時跳出,返回上一次遞歸。

遞歸要注意遞歸的截至條件,并且要注意數(shù)組的邊界。

二、具體代碼及實現(xiàn)過程

1.初始化棋盤

兩個字符數(shù)組大小相同,只是初始化的字符不同,所用采用同一個初始化函數(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.打印棋盤

代碼如下(示例):打印棋盤也是通過傳參的方式,為了在選擇格子時方便,打印出行號列號

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間的隨機數(shù),利用隨機數(shù)確定放置雷的行和列,放置雷之前要判斷此位置是否被放置過,如果被放置過就換一個隨機數(shù),直至放滿指定的雷的個數(shù)。

void SetMine(char mine[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;//雷的總數(shù)
 while (count)
 {
 int x = rand() % row + 1;//隨機產(chǎn)生行號
 int y = rand() % col + 1;//隨機產(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)的格子置為空格,遞歸按照某一順序查找其他方向,遞歸的截止條件為格子周圍有雷存在。遞歸時要注意判斷格子沒有被查找過,并且要注意坐標(biāo)的范圍不能超過0-9,否則會展開出錯。

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、判斷所選格子周圍的雷的個數(shù),采用坐標(biāo)相加的方法,因為數(shù)組mine為字符數(shù)組,字符在內(nèi)存中存儲的是它的ASCII碼值,所以相加后減去8個字符‘0',即減去字符‘0'的ASCII碼值,就得到周圍雷的個數(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è)置一個win,對非雷格子進行計數(shù),如果win與非雷個數(shù)相等,則排雷成功。

5.運行結(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("選擇錯誤,請重新選擇?。?!\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;//隨機產(chǎn)生行號
 int y = rand() % col + 1;//隨機產(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)文章

最新評論