文章目錄
- 前言
- 一、掃雷小游戲整體思路講解,
- 二、game.c各游戲功能函式的講解
- 1.InitBoard 初始化陣列函式講解
- 2.DisplayBoard 列印格子函式講解
- 3.Setmine 電腦生成雷 函式講解
- 4.GetMineCount 格子表示周圍雷的個數 函式講解
- 5.FindMine 排雷函式實作
- game.c源代碼展示:
- 三.game.h 頭檔案代碼展示
- 總結
前言
在完成三子棋小游戲后(三子棋具體實作參考博主上一篇代碼),相信我們一定對分模塊完成代碼,分別實作不用的功能的編程思想有了稍加深刻的理解與認識,接下來為了鞏固與練習自己的思維,我們可以試著完成相對三子棋更加難一丟丟的掃雷小游戲,下面就和博主一起來完成掃雷游戲的代碼實作把~附上gitee地址(完整代碼都已上傳,需要的話可自行查閱):
https://gitee.com/yang-xinshang
一、掃雷小游戲整體思路講解,
和三子棋小游戲一樣,這次我們的代碼依舊書寫在 test.c (游戲整體實作思路), game.c(游戲功能函式實作),以及 game.h (代碼用到的所有頭文集,函式宣告,符號定義存放位置)來書寫我們的掃雷代碼,
在進行掃雷小游戲整體思路講解之前,我們先來看一看test.c 源代碼:
在游戲代碼實作開始前我們先要明確一個點:如果玩家選擇的點在邊界上,堅持周圍是否有雷的時候陣列就會出現越界,所以我們初始化棋盤(二維陣列時應該多出一行一列),我們的掃雷不寫太難,棋盤就定義為9x9二維陣列,所以初始化時候就初始化成11x11的二位陣列,(定義內容放在 game.h)檔案中
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void meum()
{
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,'*' );
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
Setmine(mine, ROW, COL);
FindMine(mine,show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do{
meum();
printf("請選擇:\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("輸入錯誤,請重新輸入\n");
break;
}
} while (input);
return 0;
}
這里我們可以看到游戲整個流程大致是這樣的:
先用do…while陳述句實作游戲功能的選擇回圈實作,并定義出選單meum()函式將其列印,接下來就是重點游戲函式的實作,放在 game.c檔案中,
二、game.c各游戲功能函式的講解
1.InitBoard 初始化陣列函式講解
代碼如下:
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
在這里我們將游戲陣列(mine)初始化全為 0 ,展現陣列(show)初始化全為 * ,
2.DisplayBoard 列印格子函式講解
代碼如下(示例):
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("-----------------------------\n");
for (i = 0; i <= 9 ; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------------------------\n");
}
在這里將上面初始化好的陣列(格子)列印出來,并且列印上每行每列的坐標,注意橫縱坐標從1開始,方便玩家輸入坐標,并且列印上分割線,
3.Setmine 電腦生成雷 函式講解
看代碼:
void Setmine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1; // 1-9
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
EASY_COUNT是我們定義出來的雷的個數,為10個,這里亂數生成方面和三子棋類似(方法具體講解參考上文),注意的是我們要的是1-9之間的數字,所以在亂數子生成后加1, 雷以 ‘1‘ (字符1)的形式放在格子中,
4.GetMineCount 格子表示周圍雷的個數 函式講解
代碼如下:
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return (mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x - 1][y - 1] + mine[x + 1][y + 1] + mine[x + 1][y - 1]) + mine[x + 1][y + 1] - 8 * '0';
}
這里把周圍坐標正確表示即可計算,需要注意一個知識點: eg. ‘3’-‘0’=3 字符3-字符0=數字0,以此類推,
5.FindMine 排雷函式實作
代碼如下:
void FindMine(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) //一共10個雷,71個空格子,排出即獲勝,
{
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); //列印出看結果
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0'; //空格子計數周圍雷
DisplayBoard(show, ROW, COL);
win++; //排出一個win自增1
}
}
else
{
printf("坐標非法,請重新輸入\n");
}
}
if (win == row*col - EASY_COUNT)
{
printf("恭喜你!排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
需要注意的點在代碼中注釋展示,
game.c源代碼展示:
下面放上整個game.c源代碼:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("-----------------------------\n");
for (i = 0; i <= 9 ; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----------------------------\n");
}
void Setmine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1; // 1-9
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return (mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x - 1][y - 1] + mine[x + 1][y + 1] + mine[x + 1][y - 1]) + mine[x + 1][y + 1] - 8 * '0';
}
void FindMine(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);
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐標非法,請重新輸入\n");
}
}
if (win == row*col - EASY_COUNT)
{
printf("恭喜你!排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
三.game.h 頭檔案代碼展示
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int row, int col, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void Setmine(char board[ROWS][COLS], int row,int col);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
int GetMineCount(char board[ROWS][COLS], int x, int y);
這里放上整個專案需要用到的頭文集以及函式宣告,符號定義即可,(分開書寫能使代碼風格整體良好)
總結
以上就是整個掃雷游戲的實作思路以及源代碼,歡迎大家指正~
以上就是今天要講的掃雷小游戲實作,本文僅僅簡單介紹了簡單的掃雷小游戲,然而依舊還有更多的功能等待大家去實作,歡迎大家補充討論!
如果覺得文章對自己有幫助,歡迎大家多多點贊評論,跪謝!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290973.html
標籤:其他
上一篇:C語言三子棋 +(看完包你會)
下一篇:C語言實作井字棋小游戲
