您好,登錄后才能下訂單哦!
這篇“C語言怎么通過遞歸實現(xiàn)掃雷游戲”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“C語言怎么通過遞歸實現(xiàn)掃雷游戲”文章吧。
void menu() { printf("****************************\n"); printf("******** 1.play **********\n"); printf("******** 0.exit **********\n"); printf("****************************\n"); }
用戶按1進(jìn)入游戲
void Itnboard(char board[ROWS][COLS], int rows, int cols,char c) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j <cols; j++) { board[i][j] = c; } } }
創(chuàng)建數(shù)組,并對其進(jìn)行初始化
void Setboard(char board[ROWS][COLS], int row, int col) { int count = Easy_count; while (count) { int x = rand() % row+1; int y = rand() % col+1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } }
用time函數(shù)產(chǎn)生隨機值
void Displayboard(char board[ROWS][COLS], int row, int col) { int i, j; 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"); } }
打印棋盤
void Player(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y; int count = 0; while (1) { printf("請排雷:\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= 9 && y >= 1 && y <= 9) { if (board[x][y] == '0') { Openboard(show, board, x, y); Displayboard(show, ROW, COL); } else if (board[x][y] == '1') { printf("你死了\n"); break; } } else { printf("請重新輸入"); } int i, j; for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { if (show[i][j] == '*') { count++; } } } if (count == Easy_count) { printf("成功\n"); //這里的判斷條件是遍歷整個數(shù)組,統(tǒng)計雷的個數(shù),如果雷的個數(shù)等于所剩余未排的*,說明排雷成功 break; } } }
用戶輸入值,并進(jìn)行判斷,如果該位置沒有雷,我們進(jìn)入展開函數(shù)
void Openboard(char show[ROWS][COLS], char board[ROWS][COLS], int row, int col) { if (row >= 1 && row <= ROW && col >= 1 && col <= COL) { int count=sum(board, row, col); if (count != 0) { show[row][col] = count + '0'; } else if (show[row][col] != '_') { show[row][col] = '_'; int i = 0, j = 0; for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { Openboard(show, board, i,j); } } } else { return; } } }
如果用戶輸入的這個位置沒有雷,我們對其周圍8個位置進(jìn)行判斷是否有雷,若有雷,我們把雷的個數(shù)顯示在該位置上,若其周圍8個位置沒有雷并且不是下劃線,我們把這個位置賦值為下劃線,然后并對其8個位置進(jìn)行同樣的判斷,如果周圍沒雷,而且周圍的棋子也不是下劃線,我們對其進(jìn)行返回。
這里我們show棋盤有三種情況,
1.該位置是*
2.該位置是下劃線
3.該位置是雷的個數(shù)
else
return;
這里是作用:如果是下劃線,我們就返回上一層函數(shù)。因為如果這里不是下劃線,我們會在else return 之前的語句中進(jìn)行判斷,并對其周圍8個位置進(jìn)行操作,然后再對這8個棋子各個周圍8個位置進(jìn)行判斷并操作,如果這里是下劃線,就說明由這個位置為中心的周圍8個棋子已經(jīng)判斷過了,并且以這8個位置為中心,已經(jīng)遞歸過了,我們不需要再進(jìn)行判斷,所以直接返回就行
int sum(char board[ROWS][COLS], int x, int y) { return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0'); }
test.c
#include"game.h" void menu() { printf("****************************\n"); printf("******** 1.play **********\n"); printf("******** 0.exit **********\n"); printf("****************************\n"); } void game() { char board[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; Itnboard(board, ROWS, COLS,'0'); //初始化棋盤 Itnboard(show, ROWS, COLS, '*'); Setboard(board, ROW, COL); Displayboard(board, ROW, COL); Player(board, show, ROW, COL); //玩家輸入 } int main() { int input=1; srand((unsigned int)time(NULL)); do{ menu(); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("輸入錯誤請重新輸入:\n "); } } while (input); }
game.c
#include"game.h" void Itnboard(char board[ROWS][COLS], int rows, int cols,char c) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j <cols; j++) { board[i][j] = c; } } } void Displayboard(char board[ROWS][COLS], int row, int col) { int i, j; 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"); } } void Setboard(char board[ROWS][COLS], int row, int col) { int count = Easy_count; while (count) { int x = rand() % row+1; int y = rand() % col+1; if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } int sum(char board[ROWS][COLS], int x, int y) { return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0'); } void Player(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x, y; int count = 0; while (1) { printf("請排雷:\n"); scanf("%d %d", &x, &y); if (x >= 1 && x <= 9 && y >= 1 && y <= 9) { if (board[x][y] == '0') { Openboard(show, board, x, y); Displayboard(show, ROW, COL); } else if (board[x][y] == '1') { printf("你死了\n"); break; } } else { printf("請重新輸入"); } int i, j; for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { if (show[i][j] == '*') { count++; } } } if (count == Easy_count) { printf("成功\n"); break; } } } void Openboard(char show[ROWS][COLS], char board[ROWS][COLS], int row, int col) { if (row >= 1 && row <= ROW && col >= 1 && col <= COL) { int count=sum(board, row, col); if (count != 0) { show[row][col] = count + '0'; } else if (show[row][col] != '_') { show[row][col] = '_'; int i = 0, j = 0; for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { Openboard(show, board, i,j); } } } else { return; } } }
game.h
#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 Itnboard(char board[ROWS][COLS], int rows, int cols,char c); void Displayboard(char board[ROWS][COLS], int row, int col); void Setboard(char board[ROWS][COLS], int row, int col); void Player(char board[ROWS][COLS], char show[ROWS][COLS], int row, int col); void Openboard(char show[ROWS][COLS], char board[ROWS][COLS], int row, int col);
以上就是關(guān)于“C語言怎么通過遞歸實現(xiàn)掃雷游戲”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。