掃雷是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);//尋找雷區(用戶尋找雷)
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--;//遍歷檢查是否全部非雷區已被判斷
}
}
}
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 標記雷區 *****\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語言陣列知識、條件陳述句、函式知識的綜合應用,更多的體現了設計程式的合理性和嚴謹性,即合理設計搭建函式,相比于五子棋需要更多演算法知識,掃雷相對較為容易,但依然有很多細節有待優化,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/252587.html
標籤:其他
上一篇:女朋友為我寫了一個防猝死插件
下一篇:C語言實作掃雷游戲
