C語言版掃雷

2021-07-25 08:55:37 字數 2532 閱讀 7988

用c語言實現掃雷,首先應該有基本的思路,建立兩個相同大小的陣列,乙個用於布雷(初始化為全0,布雷時將雷置為1),另乙個給使用者展示(初始化為星號,使用者輸入座標後統計周圍雷的分布並列印雷的個數)。由於需要統計每個座標周圍類的個數,所以將陣列建立大一圈,以方便統計。若輸了,列印出雷陣告訴玩家雷的分布。

具體實現:

game.h檔案

#ifndef __game_h__

#define __game_h__

#define rows 10

#define cols 10

#define mine_count 30

void display(char arr[rows + 2][cols + 2] , int rows, int cols); //列印雷陣陣列

void set_mine(char mine[rows + 2][cols + 2], int rows, int cols); //布雷

void get_mine_count(char mine[rows + 2][cols + 2], char show[rows + 2][cols + 2], int x, int y); //統計每格雷的個數

void get_around_mine_count(char mine[rows + 2][cols + 2], char show[rows + 2][cols + 2], int x, int y); //統計周圍雷的個數

void first_remove(char mine[rows + 2][cols + 2], int x, int y); //第乙個是雷移走

#endif //__game_h__

game.c檔案

#define _crt_secure_no_warnings 1

#include#include#include#include "game.h"

void display(char arr[rows + 2][cols + 2], int rows, int cols) //列印雷陣陣列

printf("\n");

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

printf("\n"); }}

void set_mine(char mine[rows + 2][cols + 2], int rows, int cols) //布雷 }}

void get_mine_count(char mine[rows + 2][cols + 2], char show[rows + 2][cols + 2], int x, int y) //統計每格雷的個數

void get_around_mine_count(char mine[rows + 2][cols + 2], char show[rows + 2][cols + 2], int x, int y) //統計周圍雷的個數

void first_remove(char mine[rows + 2][cols + 2], int x, int y) //第乙個是雷移走

}if (mine[x][y] == '0')}}}

test.c檔案

#define _crt_secure_no_warnings 1

#include#include#include "game.h"

void main_menu() //主介面

void game()

; //埋雷的陣列

char show[rows + 2][cols + 2] = ; //展示的陣列

memset(mine, '0', sizeof(char)* (rows+2) *(cols + 2)); //陣列初始化

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

display(show, rows, cols); //列印展示的雷陣

set_mine(mine, rows, cols); //布雷

//display(mine, rows, cols); //除錯的時候可以取消注釋列印出雷的分布

while (win= rows*cols - mine_count) }

enum option //列舉

;int main()

} while (x);

return 0;

}

執行結果:

C語言版簡單掃雷遊戲

我們在設計的時候,首先將其分為三個部分,分別為標頭檔案game.h遊戲介面及主要實現的功能函式部分test.c,還有就是最重要的遊戲實現部分game.c。我們通過game.h將所有用到的標頭檔案引用進去,目的是更加直觀的讓讀者看懂我們的結構,另外我們思路也會通暢一些。include include ...

雙截棍 C語言版

雙截棍c 語言版 軟考室的菸味瀰漫坐滿了程式設計師 室裡面的監考官係分已三年 出上午試題的老師練cpu 耍微控制器 硬體功夫最擅長還會邏輯門三極體 他們學生我習慣從小就耳濡目染 什麼軟體跟網路我都耍的有摸有樣 什麼語言最喜歡c 物件導向 想要去英倫美帝學圖靈諾伊曼 怎麼編怎麼編離散數學是關鍵 怎麼編...

棧 C語言版

棧 lifo 運算所限的線性表,限制它的插入和刪除操作僅在表的一段進行。棧頂 top 插入 刪除。另一端為棧底。n 0稱為空棧,插入新元素稱為入棧 進棧。刪除稱為出棧 退棧。特點 先進後出。基本運算 初始化棧 判斷空 入棧 出棧 讀棧頂元素。順序棧儲存結構 初始化棧 stack init 判斷空 入...