文章目錄
- 一、游戲說明
- 二、游戲設計步驟
- 三、代碼實作
- 1.創建選單
- 2.創建和初始化游戲掃雷區
- 3.設定雷
- 4.列印掃雷區
- 5.掃雷
- 四、運行效果
一、游戲說明
首先打開游戲界面會出現一個掃雷區,有三種掃雷區可供選擇,各掃雷區的掃雷難度依次遞增
初級:81個方塊,10個雷(這里實作的是初級版的掃雷)
中級:256個方塊,40個雷
高級:480個方塊,99個雷
玩法
掃雷游戲的規則非常簡單:
挖開地雷(即點到設定成為雷的方塊),被炸,游戲結束,
挖開空方塊,可以繼續玩,
挖開數字,則表示在其周圍的八個方塊中共有多少個雷,可以使用該資訊推斷能夠安全單擊附近的哪些方塊,
玩游戲程序中的情況如下:

二、游戲設計步驟
1.創建選單
2.創建和初始化游戲掃雷區
3.設定雷的位置
4.列印出掃雷區
5.開始掃雷
三、代碼實作
1.創建選單
規定輸入 1 為玩游戲,輸入 0 為退出游戲
void menu()
{
printf("****************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("****************************\n");
}
2.創建和初始化游戲掃雷區
首先創建雷區需要兩個二維陣列
- Mine[ROWS][COLS] 用來存盤布置雷區的資訊,不會顯示出來
- Show[ROWS][COLS] 用來顯示出掃雷游戲程序中的資料
- InitBoard(char board[ROWS][COLS], int rows, int cols,char set)函式用來初始化游戲界面
- InitBoard(Mine, ROWS, COLS,‘0’) 函式將Mine陣列中每個元素全部初始為字符 ‘ 0 ’
- InitBoard(Show, ROWS, COLS, ‘*’) 函式將Show陣列中的每個元素全部初始化為 ‘ * ’
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
3.設定雷
利用亂數產生機制(若不明白亂數的產生,可以看:如何生成亂數)在陣列 Mine[ROWS][COLS] 中隨機產生所需個數的雷,規定字符 ‘ 1 ’ 為雷,字符 ‘ 0 ’,不為雷
void SetMine(char Mine[ROWS][COLS], int row, int col)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Mine[x][y] == '0')
{
Mine[x][y] = '1';
count--;
}
}
}
4.列印掃雷區
同理,相應的Mine陣列或者Show陣列傳入
DisplayBoard(char board[ROWS][COLS], int row, int col) 函式中就可實作對應界面的列印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
int j = 0;
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
5.掃雷
FineMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col) 為掃雷函式
首先輸入要排雷的坐標,若坐標合法,則排雷
當被排的坐標為雷是,則被炸;若不是雷,則在此坐標顯示周圍的雷數
顯示雷數的函式為Get_mine_count(char Mine[ROWS][COLS], int x, int y)
當排完所有雷后,則排雷成功
static 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 FineMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("請輸入排查的坐標>:");
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;
}
if (Mine[x][y] == '0')
{
int count = Get_mine_count(Mine, x, y);
Show[x][y] = count + '0';//因為Show陣列為字符陣列,所以數
//字元素在記憶體中以ASCII碼存盤
//count+'0'就可將該位置用字
//符數字的形式表示出來
DisplayBoard(Show, row, col);
win++;
}
}
else
{
printf("坐標不合法,請重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
}
}
以上是各各步驟函式的具體實作,而為了方便,我們將代碼分為三個檔案:
1.game.h------關于游戲相關的函式宣告,符號宣告及頭檔案的包含
2.game.c------游戲相關函式的實作
3.test.c------測驗游戲的邏輯
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//設定雷的數目
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char Mine[ROWS][COLS], int row, int col);
void FineMine(char Mine[ROWS][COLS],char Show[ROWS][COLS], int row, int col);
game.c
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
int j = 0;
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char Mine[ROWS][COLS], int row, int col)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Mine[x][y] == '0')
{
Mine[x][y] = '1';
count--;
}
}
}
static 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 FineMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-EASY_COUNT)
{
printf("請輸入排查的坐標>:");
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;
}
if (Mine[x][y] == '0')
{
int count = Get_mine_count(Mine, x, y);
Show[x][y] = count + '0';
DisplayBoard(Show, row, col);
win++;
}
}
else
{
printf("坐標不合法,請重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
}
}
test.c
#include"game.h"
void menu()
{
printf("****************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("****************************\n");
}
void game()
{
char Mine[ROWS][COLS] = { 0 };
char Show[ROWS][COLS] = { 0 };
InitBoard(Mine, ROWS, COLS,'0');
InitBoard(Show, ROWS, COLS, '*');
SetMine(Mine, ROW, COL);
//DisplayBoard(Mine, ROW, COL);
printf("--------掃雷----------\n");
DisplayBoard(Show, ROW, COL);
printf("--------掃雷----------\n");
FineMine(Mine, Show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("請選擇>:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("掃雷\n");
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("請重新輸入\n");
break;
}
} while (input);
return 0;
}
四、運行效果

簡易版的掃雷游戲就此完成!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/292232.html
標籤:其他
