C語言模擬實現掃雷游戲
更新時間:2021年01月26日 11:23:18 作者:Skyline-sjc
這篇文章主要為大家詳細介紹了C語言模擬實現掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
掃雷是Windows系統的經典游戲,下文將利用c語言實現這個經典的小游戲。本版本程序添加了炸彈標記功能。但由于作者水平實現較為死板,此處留坑待以后學習后改進。
Part 1主函數部分:
此部分主要提供用戶界面,不同程序均可利用:
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("請選擇:>\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戲開始!\n");
game();
break;
case 0:
printf("退出游戲!\n");
break;
default:
printf("輸入錯誤,請重新輸入!\n");
break;
}
} while (input);
return 0;
}
Part 2 函數部分:
我們來思考一下函數的組成:
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);//尋找雷區(qū)(用戶尋找雷) 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);//檢查是否獲勝
Part 3 整體實現:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#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("請選擇:>\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戲開始!\n");
game();
break;
case 0:
printf("退出游戲!\n");
break;
default:
printf("輸入錯誤,請重新輸入!\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)//打印棋盤時 注意人性化設計便于讀取坐標
{
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設計,將字符串轉化為整型輸出
}
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);//遞歸實現掃雷的擴展
}
}
}
}
}
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 標記雷區(qū) *****\n");//實現掃雷中的標記功能
printf("******2 輸入坐標 *****\n");//實現掃雷中的輸入功能
scanf("%d", &input);
if(input==1){
printf("請輸入要選擇的坐標:>\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("非法輸入!請重新輸入!\n");
}
}
else
{
printf("請重新輸入!\n");
}
}
else if(input==2)
{
printf("請輸入要選擇的坐標:>\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("重復輸入!請重新輸入!\n");
}
}
else
{
printf("請重新輸入!\n");
}
}else{
printf("輸入錯誤!\n");
}
}
}
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
// DisplayBoard(mine, ROW, COL); //此處代碼為測試用
DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL, EASY_COUNT);
// DisplayBoard(mine, ROW, COL); //此處代碼為測試用
// DisplayBoard(show, ROW, COL);//此處代碼為測試用
FindMine(show, mine, ROW, COL, EASY_COUNT);
}
Part 4 總結:
掃雷及三子棋 為C語言數組知識、條件語句、函數知識的綜合應用,更多的體現了設計程序的合理性和嚴謹性,即合理設計搭建函數。相比于五子棋需要更多算法知識,掃雷相對較為容易,但依然有很多細節(jié)有待優(yōu)化。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
VC List Control控件如何刪除選中的記錄實例詳解
這篇文章主要介紹了VC List Control控件如何刪除選中的記錄實例詳解的相關資料,需要的朋友可以參考下2017-06-06
Visual Studio 如何創(chuàng)建C/C++項目問題
這篇文章主要介紹了Visual Studio 如何創(chuàng)建C/C++項目問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

