您好,登錄后才能下訂單哦!
本篇內容介紹了“如何用C語言代碼實現掃雷游戲”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
代碼如下:
#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//函數聲明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("請輸入:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戲\n\n"); break; default: printf("輸入錯誤,重新輸入\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("請輸入要排查的坐標(格式: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("輸入的坐標非法\n"); } } if (win == row*col - EASY_COUNT) { printf(" 恭喜你,你排雷成功了!!!\n"); }}
“如何用C語言代碼實現掃雷游戲”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。