
一、程序環(huán)境配置大家好,相信大家對掃雷小游戲已經不陌生了。
這篇文章將分享利用C語言實現掃雷的全過程講解及其完整代碼。初學C語言的朋友們也能看懂學會哦!覺得有幫助的小伙伴記得點個免費的小贊支持一下哦。
本項目將會用到三個文件存儲代碼,分別為兩個源文件test.c game.c 和一個頭文件 game.h。
二、各種功能的實現以及邏輯關系的整理 2.1 創(chuàng)建游戲初始界面(進入\退出 游戲)test.c文件是整個程序的主題,game.c文件中存放實現游戲功能的代碼,game.h文件中存放整個程序所需要的頭文件。
這樣 有利于后期調試,也提升了代碼整體的可讀性。
打印菜單
代碼如下:
void menu()
{printf("******************************\n");
	printf("******     1. play      ******\n");
	printf("******     0. exit      ******\n");
	printf("******************************\n");
}玩家選擇1進入游戲,選擇0退出游戲菜單。
代碼如下:
void test()
{int input = 0;
	do
	{menu();
		printf("請選擇:>");
		scanf("%d", &input);
		switch (input)
		{case 1:
			game();
			break;
		case 0:
			printf("退出游戲!\n");
			break;
		default:
			printf("輸入錯誤,請重新選擇!\n");
			break;
		}
	} while (input);
}2.2 創(chuàng)建并初始化二維數組board[][] mine[][] (board存放棋盤的信息 mine存放雷的信息)這里我們使用 宏定義 的方式來創(chuàng)建棋盤行和列以及雷的數量,方便我們后期對棋盤以及雷數的修改。
代碼如下:
#define ROW 9  //棋盤行數
#define COL 9  //棋盤列數
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10  //雷的數量創(chuàng)建并初始化二維數組,實現游戲大體框架
char mine[ROWS][COLS] = { 0 }; 存放雷的信息
char show[ROWS][COLS] = { 0 };存放排查出的雷的信息
代碼如下:
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;  //1--9
		if (board[x][y] == '0')
		{	board[x][y] = '1';//布置雷
			count--;
		}
	}
}2.3 初始化棋盤在game.c文件中搭建并初始化棋盤。
代碼如下:
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;
		}
	}
}2.4 打印棋盤之后在test.c文件中調用即可
init_board(mine, ROWS, COLS, ‘0’);
init_board(show, ROWS, COLS, ‘*’);
代碼如下:
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{printf("\n");
	int i, j;
	//打印列號
	for (j = 0; j<= col; j++)
		printf("%d ", j);
	printf("\n");
	for (i = 1; i<= row; i++)
	{printf("%d ", i);
		for (j = 1; j<= col; j++)
		{	printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}2.5 設置雷區(qū)注:在打印棋盤還應考慮打印間隙、行號以及列號,便于觀察結果。
布置雷是一個隨機過程,要用到rand()函數,并在代碼中設置界限,使得隨機值產生的坐標在合法坐標范圍之內
代碼如下:
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;  //1--9
		if (board[x][y] == '0')
		{	board[x][y] = '1';//布置雷
			count--;
		}
	}
}2.6 掃雷掃出所有雷即為勝利,“踩”倒雷即為失敗
若選中不是雷的坐標,顯示周圍8個坐標總共有多少雷(該過程用函數get_mine_count(char mine[ROWS][COLS],int x, int y)實現)
最終利用函數FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row,int col)返回結果,該函數中注意設置所排坐標的合法性,若坐標非法需提示玩家 “輸入坐標非法,請重新輸入!”。
代碼如下:
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{//'0' - '0' = 0
	//'1' - '0' = 1
	//'3' - '0' = 3
	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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{int x = 0;
	int y = 0;
	int win = 0;
	while (winprintf("請輸入要排查的坐標:>");
		scanf("%d%d", &x, &y);
		//判斷坐標合法性
		if (x >= 1 && x<= 9 && y >= 1 && y<= 9)
		{	//1、踩雷
			if (mine[x][y] == '1')
			{		printf("很遺憾,您被炸死了!\n");
				DisplayBoard(mine, row, col);
				break;
			}
			//2、未踩雷
			else
			{		//計算x,y坐標周圍有幾顆雷
				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");
}
三、完整代碼展示 
3.1 game.h板塊代碼如下:
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 70
#include#include#includevoid InitBoard(char board[ROWS][COLS], int rows, int cols,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);   3.2 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;
	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)
{printf("\n");
	int i, j;
	//打印列號
	for (j = 0; j<= col; j++)
		printf("%d ", j);
	printf("\n");
	for (i = 1; i<= row; i++)
	{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;  //1--9
		if (board[x][y] == '0')
		{	board[x][y] = '1';
			count--;
		}
	}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{//'0' - '0' = 0
	//'1' - '0' = 1
	//'3' - '0' = 3
	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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{int x = 0;
	int y = 0;
	int win = 0;
	while (winprintf("請輸入要排查的坐標:>");
		scanf("%d%d", &x, &y);
		//判斷坐標合法性
		if (x >= 1 && x<= 9 && y >= 1 && y<= 9)
		{	//1、踩雷
			if (mine[x][y] == '1')
			{		printf("很遺憾,您被炸死了!\n");
				DisplayBoard(mine, row, col);
				break;
			}
			//2、未踩雷
			else
			{		//計算x,y坐標周圍有幾顆雷
				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");
}
3.3 test.c板塊代碼如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{printf("******************************\n");
	printf("******     1. play      ******\n");
	printf("******     0. exit      ******\n");
	printf("******************************\n");
}
void game()
{//雷的信息存儲
	//1.布置雷的信息
	char mine[ROWS][COLS] = {0 };
	//2.排查出雷的信息
	char show[ROWS][COLS] = {0 };
	//初始化棋盤
	InitBoard(mine, ROWS, COLS, '0');
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盤
	DisplayBoard(show, ROW, COL);
	//布置雷
	SetMine(mine, ROW, COL);
	//掃雷
	FindMine(mine, show, ROW, COL);
}
void test()
{srand((unsigned int)time(NULL));
	int input = 0;
	do
	{menu();
		printf("請選擇:>");
		scanf("%d", &input);
		switch (input)
		{case 1:
			game();
			break;
		case 0:
			printf("退出游戲!\n");
			break;
		default:
			printf("輸入錯誤,請重新選擇!\n");
			break;
		}
	} while (input);
}
int main()
{test();
}四、代碼運行結果展示

這個簡單的小游戲非常適合C語言新手小白操作,我們可以嘗試把它的代碼完整打出來,并且當成功運行后我們將會有一定的成就感并從中得到鼓勵以及接下來對C語言學習的信心。最后也歡迎各位大佬指出其中的錯誤。
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
                分享文章:【C語言】掃雷游戲詳解及完整代碼-創(chuàng)新互聯
                
                文章網址:http://www.chinadenli.net/article12/deiigc.html
            
成都網站建設公司_創(chuàng)新互聯,為您提供企業(yè)網站制作、面包屑導航、手機網站建設、做網站、微信公眾號、網站收錄
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯