用C語言實現掃雷遊戲

2021-08-09 15:07:21 字數 1965 閱讀 1033

本人能力不足,**能力有待提公升,在敲**的過程中遇到了很多問題,在此不再一一贅述,現將**以及分析展示如下:

#include"stdio.h"

#include"stdlib.h"

#include"time.h"

#include"string.h"

#pragma warning(disable:4996)

#define rows 11

#define cols 11

#define count 10

//選單函式

void menu()

//初始化雷區

void init_game(char mine[rows][cols], char show[rows][cols])

//}memset(mine, ' 0 ', rows*cols*sizeof(char));

memset(show, ' * ', rows*cols*sizeof(char));

}//隨機布局雷陣,即布好十個雷的任意位置

void set_mine(char mine[rows][cols]) }}

//顯示雷區

void display(char show[rows][cols])

printf("\n");

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

printf("\n"); }}

//計算雷的個數,主要是對其周圍八個方格進行判斷

int get_num(char mine[rows][cols], int x, int y)

if (mine[x - 1][y] == '1')//左邊

if (mine[x - 1][y + 1] == '1')//左下方

if (mine[x][y - 1] == '1')//上方

if (mine[x][y + 1] == '1')//下方

if (mine[x + 1][y - 1] == '1')//右上方

if (mine[x + 1][y] == '1')//右方

if (mine[x + 1][y + 1] == '1')//右下方

return count;

}//進行掃雷

int sweep(char mine[rows][cols], char show[rows][cols])

else

}printf("恭喜你贏了!\n");

display(mine);

return 0;

}//進行遊戲

int game(char mine[rows][cols], char show[rows][cols])

//主函式

int main()

} menu();

while (1)

else if (input == 0)

else

}system("pause");

return 0;

}

函式:主要用於遊戲開始介面的實現,以供玩家選擇遊戲的進或退,這裡主要是通過幾個簡單的printf()函式來實現列印的;

函式:主要用於列陣的初始化,在這裡定義了兩個陣列,乙個是掃雷的介面,乙個是用於顯示雷的分布來用於計數,在這裡主要用到了乙個memset()函式來加以實現;

函式:主要用於雷的布局,總共有十個雷,在這裡主要是通過rand()函式來實現雷的隨機分布的;同時呼叫了乙個srand()函式,以及time()函式;

函式:主要用於實現雷的分布情況,顯示各個雷所在的對應位置;

函式:計算雷的個數, 主要是對其周圍八個方格進行判斷 ,來統計周圍共有多少雷分布,這裡通過用八個if語句來實現統計;

函式:主要是進行掃雷操作,通過while語句以及if語句加以實現;

函式:用於實現各個函式的功能;

//8.主函式:實現全部**的相應功能,在這裡主要是通過if語句來加以判斷的。

用C語言實現(掃雷遊戲)

include include include include pragma warning disable 4996 define rows 8 define cols 8 define mines 62 void menu 列印選單 void init mine char mine cols 2...

用c語言實現掃雷遊戲

test.c define crt secure no warnings include game.h void menu void play print player 列印玩家棋盤 while 1 迴圈掃雷 if ret 判斷是否踩到雷 print player 列印玩家棋盤 intmain wh...

用 C語言 實現掃雷遊戲

要求 1 第一次下子,不炸死。2 座標周圍沒雷,可以實現展開。標頭檔案game.h include include include include include define row 9 define col 9 define rows row 2 define cols col 2 define...