C語言練習之掃雷小遊戲

2021-08-18 15:41:47 字數 3833 閱讀 8916

1.掃雷的基本功能

2.第一次掃,不被炸死

3.當周圍沒有雷的時候可以向外延伸擴充套件。

1、首先建立乙個標頭檔案

# ifndef __game_h_

# define __game_h_

# define row 10

# define col 10

# define rows (row + 2)

# define cols (col + 2)

//初始化雷

void initboard(char board[rows][cols], int row, int col, char type);

//設定雷

void setmine(char board[rows][cols], int num);

//顯示

void printboard(char board[rows][cols], int row, int col);

//統計周圍的字元之和

int sumaround(char board[rows][cols], int i, int j);

//第一次踩中雷,更改雷的位置,將此處置換為非雷空

void boomcountmine1(char board[rows][cols], char board1[rows][cols]);

//判斷是否踩中雷

int boomcountmine2(char board[rows][cols], char board1[rows][cols]);

//如果周圍沒有雷,向四周擴充套件,如果周圍有雷,統計雷的個數

void expendblock(char board[rows][cols], char board1[rows][cols], int i, int l);

//判斷輸贏

int iswin(char board1[rows][cols], int i, int j, int num);

# endif

2、然後建立乙個原始檔,寫入各個函式

#define _crt_secure_no_warnings 1

#include "game.h"

#include

#include

#include

#include

void initboard(char board[rows][cols], int row, int col, char type)//初始化棋盤資料

void setmine(char board[rows][cols], int num)//布置一定數目的雷

}}void printboard(char board[rows][cols], int row, int col)//列印棋盤

printf("\n");

for (i = 1; i < row - 1; i++)

printf("\n");

}}void boomcountmine1(char board[rows][cols], char board1[rows][cols])//第一次踩中雷,更改雷的位置,將此處置換為非雷空

}board[i][j] = '0';

sum = (board[i - 1][j - 1] + board[i][j - 1] + board[i + 1][j - 1]

+ board[i + 1][j] + board[i + 1][j + 1] + board[i][j + 1]

+ board[i - 1][j + 1] + board[i - 1][j] - 7 * '0');

board1[i][j] = sum;

expendblock(board, board1, i, j);

}else

}int boomcountmine2(char board[rows][cols], char board1[rows][cols])//判斷是否踩中雷

else

}if (board[i][j] == '1')}}

board1[i][j] = 'x';

return

1; }

else

return0;}

int sumaround(char board[rows][cols], int i, int j)//統計周圍的字元之和

void expendblock(char board[rows][cols], char board1[rows][cols], int i, int j)//如果周圍沒有雷,向四周擴充套件,如果周圍有雷,統計雷的個數

else

if (sum > '0')

if ((sum

<= '0') && (i>0) && (i1) && (j>0) && (j1))

if ((board1[i][j - 1] == '*'))//左方

if ((board1[i + 1][j - 1] == '*'))//左下

if ((board1[i + 1][j] == '*'))//下方

if ((board1[i + 1][j + 1] == '*'))//右下

if ((board1[i][j + 1] == '*'))//右方

if ((board1[i - 1][j + 1] == '*'))//右上

if ((board1[i - 1][j] == '*'))//上方

if ((board1[i - 1][j] == '*'))//上方

}}int iswin(char board1[rows][cols], int i, int j, int num)}}

if (count == num)}}

return

1; }

return

0;}

3、最後寫乙個測試** main.c

#define _crt_secure_no_warnings 1

#include "game.h"

#include

#include

#include

#include

void menu()

void game()

; char show[rows][cols] = ;

initboard(mine, rows, cols, '0');

initboard(show, rows, cols, '*');

setmine(mine, num);

printboard(show, rows, cols);

while (1)

if (ret == 1)

}else

input++;

}printf("\n");

printboard(show, rows, cols);

}}//主函式

int main()

} while (input);

system("pause");

return

0;}

1、開始遊戲

2、輸入座標

3、踩到雷

4、呃呃,老是輸,這裡就不給大家展示贏的截圖啦!

C語言小遊戲 掃雷

1.這個小遊戲由兩個原始檔,乙個標頭檔案分工合作完成。test.c game.c game.h 2.確定基本框架,在test.c中寫主函式以及遊戲所需的基本框架 例如 menum switch 3.先確定這個遊戲需要的函式功能,在game.h中進行函式宣告,game.c中進行函式的定義,test.c...

C語言 掃雷小遊戲

第一次下子,不炸死 座標周圍沒雷,可以實現展開 遊戲結束後展示玩家用時 game.h ifndef game h define game h include include include include define row 12 define col 12 define count 10 棋盤中...

C語言小遊戲 掃雷

這個小遊戲也分為三個部分的 分別是標頭檔案,測試 和遊戲 1.注意初始化以及使用getwincount函式的時候字元1和字元0的使用。2.為了玩家的體驗,第一步不可以被炸死。3.要擴充套件,這時會用到遞迴,注意遞迴的使用。4.可以新增標記,優化遊戲。標頭檔案 game.h ifndef game h...