C語言詳細講解通過遞歸實現(xiàn)掃雷的展開
用戶選擇菜單
void menu() { printf("****************************\n"); printf("******** 1.play **********\n"); printf("******** 0.exit **********\n"); printf("****************************\n"); }
用戶按1進入游戲
棋盤初始化
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ù)組,并對其進行初始化
布置雷(隨機布置)
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; } } }
用戶輸入值,并進行判斷,如果該位置沒有雷,我們進入展開函數(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個位置進行判斷是否有雷,若有雷,我們把雷的個數(shù)顯示在該位置上,若其周圍8個位置沒有雷并且不是下劃線,我們把這個位置賦值為下劃線,然后并對其8個位置進行同樣的判斷,如果周圍沒雷,而且周圍的棋子也不是下劃線,我們對其進行返回。
展開部分思維導(dǎo)圖
展開函數(shù)最后一個else return 作用
這里我們show棋盤有三種情況,
1.該位置是*
2.該位置是下劃線
3.該位置是雷的個數(shù)
else
return;
這里是作用:如果是下劃線,我們就返回上一層函數(shù)。因為如果這里不是下劃線,我們會在else return 之前的語句中進行判斷,并對其周圍8個位置進行操作,然后再對這8個棋子各個周圍8個位置進行判斷并操作,如果這里是下劃線,就說明由這個位置為中心的周圍8個棋子已經(jīng)判斷過了,并且以這8個位置為中心,已經(jīng)遞歸過了,我們不需要再進行判斷,所以直接返回就行
周圍雷個數(shù)判斷
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)掃雷的展開的文章就介紹到這了,更多相關(guān)C語言掃雷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于C++中由于字節(jié)對齊引起內(nèi)存問題定位分析
前幾天遇到一個稀奇古怪的問題,在創(chuàng)建對象的時候程序異常退出,查找代碼發(fā)現(xiàn)結(jié)構(gòu)體數(shù)組問題,最終把問題簡化得到解決方法,下面小編把我的問題及解決方案分享到腳本之家平臺供大家參考下2021-06-06C++動態(tài)規(guī)劃算法實現(xiàn)矩陣鏈乘法
動態(tài)規(guī)劃算法通常用于求解具有某種最優(yōu)性質(zhì)的問題。在這類問題中,可能會有許多可行解。每一個解都對應(yīng)于一個值,我們希望找到具有最優(yōu)值的解2022-06-06C++中求旋轉(zhuǎn)數(shù)組中的最小數(shù)字(經(jīng)典面試題)
這篇文章主要介紹了C++中求旋轉(zhuǎn)數(shù)組中的最小數(shù)字(經(jīng)典面試題)的相關(guān)資料,需要的朋友可以參考下2017-03-03