使用C語言怎么編寫一個(gè)掃雷游戲?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),紅山企業(yè)網(wǎng)站建設(shè),紅山品牌網(wǎng)站建設(shè),網(wǎng)站定制,紅山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,紅山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu(); printf("請(qǐng)選擇:>\n"); scanf("%d", &input); switch (input) { case 1: printf("游戲開始!\n"); game(); break; case 0: printf("退出游戲!\n"); break; default: printf("輸入錯(cuò)誤,請(qǐng)重新輸入!\n"); break; } } while (input); return 0; }
我們來思考一下函數(shù)的組成:
void menu();//實(shí)現(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);//設(shè)置雷盤 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)計(jì)周圍雷的數(shù)量 void show_recusion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);//實(shí)現(xiàn)點(diǎn)擊一下的擴(kuò)大化應(yīng)用 int checkwin(char mine[ROWS][COLS], int row, int col);//檢查是否獲勝
#include#include #include #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("請(qǐng)選擇:>\n"); scanf("%d", &input); switch (input) { case 1: printf("游戲開始!\n"); game(); break; case 0: printf("退出游戲!\n"); break; default: printf("輸入錯(cuò)誤,請(qǐng)重新輸入!\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)//打印棋盤時(shí) 注意人性化設(shè)計(jì)便于讀取坐標(biāo) { 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設(shè)計(jì),將字符串轉(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);//遞歸實(shí)現(xiàn)掃雷的擴(kuò)展 } } } } } 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 標(biāo)記雷區(qū) *****\n");//實(shí)現(xiàn)掃雷中的標(biāo)記功能 printf("******2 輸入坐標(biāo) *****\n");//實(shí)現(xiàn)掃雷中的輸入功能 scanf("%d", &input); if(input==1){ printf("請(qǐng)輸入要選擇的坐標(biāo):>\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("非法輸入!請(qǐng)重新輸入!\n"); } } else { printf("請(qǐng)重新輸入!\n"); } } else if(input==2) { printf("請(qǐng)輸入要選擇的坐標(biāo):>\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("重復(fù)輸入!請(qǐng)重新輸入!\n"); } } else { printf("請(qǐng)重新輸入!\n"); } }else{ printf("輸入錯(cuò)誤!\n"); } } } void game() { char mine[ROWS][COLS]; char show[ROWS][COLS]; InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); // DisplayBoard(mine, ROW, COL); //此處代碼為測(cè)試用 DisplayBoard(show, ROW, COL); SetMine(mine, ROW, COL, EASY_COUNT); // DisplayBoard(mine, ROW, COL); //此處代碼為測(cè)試用 // DisplayBoard(show, ROW, COL);//此處代碼為測(cè)試用 FindMine(show, mine, ROW, COL, EASY_COUNT); }
看完上述內(nèi)容,你們掌握使用C語言怎么編寫一個(gè)掃雷游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!