您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“如何用C語言代碼實(shí)現(xiàn)掃雷游戲”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
代碼如下:
#include<stdio.h>#include<stdlib.h>#include<time.h>#define ROW 9#define COL 9#define ROWS ROW+2#define COLS COL+2#define EASY_COUNT 10//函數(shù)聲明void ChushiBoard(char board[ROWS][COLS],int rows,int cols,char set);void Dayinboard(char board[ROWS][COLS], int row, int col);void BuzhiBoard(char mine[ROWS][COLS], int row, int col);void CaoleiBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);void menu(){ printf("*****************************************\n"); printf("*****************************************\n"); printf("**** 輸入1開始游戲 輸入0退出游戲 *****\n"); printf("*****************************************\n"); printf("*****************************************\n\n\n");}void game(){ char mine[ROWS][COLS];//存放雷 char show[ROWS][COLS];//顯示排查出來的雷 //初始化 ChushiBoard(mine, ROWS, COLS,'0');//'0' ChushiBoard(show, ROWS, COLS,'*');//'*' //布置雷 BuzhiBoard(mine,ROW,COL); //Dayinboard(mine, ROW, COL); //打印棋盤 Dayinboard(show, ROW, COL); //掃雷 CaoleiBoard(mine,show,ROW,COL);}void test(){ int input = 0; do { menu(); printf("請(qǐng)輸入:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戲\n\n"); break; default: printf("輸入錯(cuò)誤,重新輸入\n\n"); break; } } while (input);}int main(){ test(); system("pause"); return 0;}void ChushiBoard(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 Dayinboard(char board[ROWS][COLS], int row, int col){ int i = 0; int 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 BuzhiBoard(char mine[ROWS][COLS], int row, int col){ int count = EASY_COUNT; while (count) { int x = rand()%row+1; int y = rand()%col+1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } }}static int GetMineCount(char mine[ROWS][COLS], int x, int y){ return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0';}void CaoleiBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col){ int x = 0; int y = 0; int win = 0; while (win<row*col-EASY_COUNT) { printf("請(qǐng)輸入要排查的坐標(biāo)(格式:X空格X回車):>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= 9 && y >= 1 && y <= 9) { if (mine[x][y] == '1') { printf(" 很遺憾,你被炸死了!?。n"); Dayinboard(mine, row, col); break; } else { int count = GetMineCount(mine,x,y); show[x][y] = count+'0'; Dayinboard(show, row, col); win++; } } else { printf("輸入的坐標(biāo)非法\n"); } } if (win == row*col - EASY_COUNT) { printf(" 恭喜你,你排雷成功了?。?!\n"); }}
“如何用C語言代碼實(shí)現(xiàn)掃雷游戲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。