C語言模擬實現(xiàn)掃雷游戲
掃雷是Windows系統(tǒng)的經(jīng)典游戲,下文將利用c語言實現(xiàn)這個經(jīng)典的小游戲。本版本程序添加了炸彈標記功能。但由于作者水平實現(xiàn)較為死板,此處留坑待以后學習后改進。
Part 1主函數(shù)部分:
此部分主要提供用戶界面,不同程序均可利用:
int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu(); printf("請選擇:>\n"); scanf("%d", &input); switch (input) { case 1: printf("游戲開始!\n"); game(); break; case 0: printf("退出游戲!\n"); break; default: printf("輸入錯誤,請重新輸入!\n"); break; } } while (input); return 0; }
Part 2 函數(shù)部分:
我們來思考一下函數(shù)的組成:
void menu();//實現(xiàn)主函數(shù)中菜單 void game();//游戲的核心函數(shù) 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 mine[ROWS][COLS], int row, int col, int count);//設置雷盤 void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int count);//尋找雷區(qū)(用戶尋找雷) int CountMine(char board[ROWS][COLS], int x, int y);//統(tǒng)計周圍雷的數(shù)量 void show_recusion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);//實現(xiàn)點擊一下的擴大化應用 int checkwin(char mine[ROWS][COLS], int row, int col);//檢查是否獲勝
Part 3 整體實現(xiàn):
#include <stdio.h> #include <time.h> #include <stdlib.h> #define ROW 9 #define COL 9 #define ROWS ROW + 2 #define COLS COL + 2 #define EASY_COUNT 10 void menu(); void game(); 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 mine[ROWS][COLS], int row, int col, int count); void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int count); int CountMine(char board[ROWS][COLS], int x, int y); void show_recusion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y); int checkwin(char mine[ROWS][COLS], int row, int col); int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu(); printf("請選擇:>\n"); scanf("%d", &input); switch (input) { case 1: printf("游戲開始!\n"); game(); break; case 0: printf("退出游戲!\n"); break; default: printf("輸入錯誤,請重新輸入!\n"); break; } } while (input); return 0; } void menu() { printf("****************\n"); printf("*****1.play*****\n"); printf("*****0.exit*****\n"); printf("****************\n"); } void InitBoard(char board[ROWS][COLS], int row, int col, char set) { int i = 0, j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印棋盤時 注意人性化設計便于讀取坐標 { int i = 0, j = 0; printf("-------------掃雷游戲---------------\n"); for (i = 0; i <= col; i++) { printf("%d ", i); } 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 mine[ROWS][COLS], int row, int col, int count) { while (count >= 1) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } int CountMine(char board[ROWS][COLS], int x, int y) { int i = 0, j = 0; int sum = 0; for (i = x - 1; i <= x + 1; i++) { for (j = y - 1; j <= y + 1; j++) { sum += board[i][j]; } } return sum - 9 * '0';//巧妙利用1 0設計,將字符串轉(zhuǎn)化為整型輸出 } void show_recusion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) { int i = 0, j = 0; for (i = x - 1; i <= x + 1; i++) { for (j = y - 1; j <= y + 1; j++) { if (show[i][j] != ' ' && i >= 1 && i <=ROW && j >= 1 && j <=COL) { int temp = CountMine(mine, i, j); show[i][j] = temp + '0'; if (show[i][j] == '0') { show[x][y] = ' '; show_recusion(mine, show, i, j);//遞歸實現(xiàn)掃雷的擴展 } } } } } int checkwin(char show[ROWS][COLS], int row, int col) { int i = 0, j = 0; int count = row * col - EASY_COUNT; for (i = 1; i <= row; i++) { for (j = 0; j <= col; j++) { if (show[i][j] != '*' && show[i][j] != 'B') { count--;//遍歷檢查是否全部非雷區(qū)已被判斷 } } } if (count == 0) { return 1; } else { return 0; } } void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int count) // { while (1) { int input = 0; printf("******1 標記雷區(qū) *****\n");//實現(xiàn)掃雷中的標記功能 printf("******2 輸入坐標 *****\n");//實現(xiàn)掃雷中的輸入功能 scanf("%d", &input); if(input==1){ printf("請輸入要選擇的坐標:>\n"); int x, y; scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (show[x][y] == '*') { show[x][y] = 'B'; DisplayBoard(show, ROW, COL); } else { printf("非法輸入!請重新輸入!\n"); } } else { printf("請重新輸入!\n"); } } else if(input==2) { printf("請輸入要選擇的坐標:>\n"); int x, y; scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (show[x][y] == '*' || show[x][y]=='B') { if (mine[x][y] == '1') { printf("boom!很遺憾您輸了!\n"); break; } else { show[x][y] = CountMine(mine, x, y) + '0'; if (show[x][y] == '0') { show[x][y] = ' '; show_recusion(mine, show, x, y); } DisplayBoard(show, row, col); if (checkwin(show, ROW, COL) == 1) { printf("恭喜您獲勝了!\n"); break; } } } else { printf("重復輸入!請重新輸入!\n"); } } else { printf("請重新輸入!\n"); } }else{ printf("輸入錯誤!\n"); } } } void game() { char mine[ROWS][COLS]; char show[ROWS][COLS]; InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); // DisplayBoard(mine, ROW, COL); //此處代碼為測試用 DisplayBoard(show, ROW, COL); SetMine(mine, ROW, COL, EASY_COUNT); // DisplayBoard(mine, ROW, COL); //此處代碼為測試用 // DisplayBoard(show, ROW, COL);//此處代碼為測試用 FindMine(show, mine, ROW, COL, EASY_COUNT); }
Part 4 總結(jié):
掃雷及三子棋 為C語言數(shù)組知識、條件語句、函數(shù)知識的綜合應用,更多的體現(xiàn)了設計程序的合理性和嚴謹性,即合理設計搭建函數(shù)。相比于五子棋需要更多算法知識,掃雷相對較為容易,但依然有很多細節(jié)有待優(yōu)化。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實現(xiàn)數(shù)據(jù)結(jié)構(gòu)和雙向鏈表操作
這篇文章主要介紹了C語言實現(xiàn)數(shù)據(jù)結(jié)構(gòu)雙向鏈表操作,需要的朋友可以參考下2017-03-03探討C++中不能聲明為虛函數(shù)的有哪些函數(shù)
下面小編就為大家?guī)硪黄接慍++中不能聲明為虛函數(shù)的有哪些函數(shù)。希望對大家有所幫助。一起跟隨小編過來看看吧,祝大家游戲愉快哦2017-01-01rapidjson解析json代碼實例以及常見的json core dump問題
今天小編就為大家分享一篇關(guān)于rapidjson解析json代碼實例以及常見的json core dump問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04