溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用C語言怎么編寫一個掃雷游戲

發(fā)布時間:2021-01-26 13:47:00 來源:億速云 閱讀:144 作者:Leah 欄目:開發(fā)技術(shù)

使用C語言怎么編寫一個掃雷游戲?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

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 函數(shù)部分:

我們來思考一下函數(shù)的組成:

void menu();//實現(xiàn)主函數(shù)中菜單
void game();//游戲的核心函數(shù)
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);//設(shè)置雷盤
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);//統(tǒng)計周圍雷的數(shù)量
void show_recusion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);//實現(xiàn)點擊一下的擴大化應(yīng)用
int checkwin(char mine[ROWS][COLS], int row, int col);//檢查是否獲勝

Part 3 整體實現(xiàn):

#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)//打印棋盤時 注意人性化設(shè)計便于讀取坐標(biāo)
{
 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設(shè)計,將字符串轉(zhuǎn)化為整型輸出
}
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);//遞歸實現(xiàn)掃雷的擴展
  }
  }
 }
 }
}
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 標(biāo)記雷區(qū) *****\n");//實現(xiàn)掃雷中的標(biāo)記功能
 printf("******2 輸入坐標(biāo) *****\n");//實現(xiàn)掃雷中的輸入功能
 scanf("%d", &input);
 if(input==1){
  printf("請輸入要選擇的坐標(biāo):>\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("請輸入要選擇的坐標(biāo):>\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("重復(fù)輸入!請重新輸入!\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);
}

看完上述內(nèi)容,你們掌握使用C語言怎么編寫一個掃雷游戲的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI