C語言實現簡易掃雷


 首先,寫代碼之前要將整體思路寫出來:

  掃雷游戲:1.需要兩個二維數組,一個用來展示,一個用來放雷;

                  2.整體骨架在代碼中都有注釋說明;

                  3.游戲難度比較簡單,適合初學者觀看,如果有大佬看明白,可以指點一二.


//使用二維數組來表示地圖,此處需要2個二維數組,第一個二維數組表示地雷的雷陣,第二個二維數組表示用戶看到的地圖
//掃雷地圖大小9*9;但是二維數組11*11
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MINE_COUNT 10
#define ROW 9
#define COL 9
char show_map[ROW + 2][COL + 2];
char mine_map[ROW + 2][COL + 2];
int Menu(){
 int choice = -1;
 printf("************************\n");
 printf("*   歡迎來到掃雷游戲   *\n");
 printf("*       請您選擇       *\n");
 printf("*      1.開始游戲      *\n");
 printf("*      2.離開游戲      *\n");
 printf("************************\n");
 while (1){
  scanf("%d", &choice);
  if (choice == 1)
  {
   return 1;
   break;
  }
  else if (choice == 2)
  {
   exit(2);
  }
  else
  {
   printf("輸入非法,請重新輸入!\n");
   continue;
  }
 }
}
void Init(){     //初始化 布雷       
 srand(time(0));
 memset(mine_map, '0', (ROW + 2)*(COL + 2));
 memset(show_map, '*', (ROW + 2)*(COL + 2));
 int count = MINE_COUNT;
 int row = -1;
 int col = -1;
 while (count>=0){
  row = rand() % ROW + 1;
  col = rand() % COL + 1;
  if (show_map[row][col] == '*')
  {
   mine_map[row][col] = '1';
   count--;
   continue;
  }
 }
}
void Print(){                     //    1 2 3 4 5 6 7 8 9
 printf("    ");                    //    -----------------
 for (int col = 1; col <= COL; col++)              // 01 | | | | | | | | |
 {                       //   ----------------- 
  printf(" %d ", col);
 }
 printf("\n    ---------------------------\n");
 for (int row = 1; row <= ROW; row++)
 {
  printf("%02d ", row);
  printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", show_map[row][1], show_map[row][2], show_map[row][3], show_map[row][4], show_map[row][5],
   show_map[row][6], show_map[row][7], show_map[row][8], show_map[row][9]);
  printf("    ---------------------------\n");
 }
}
char MineBoom(){
 printf("    ");
 for (int col = 1; col <= COL; col++)
 {
  printf(" %d ", col);
 }
 printf("\n    ---------------------------\n");
 for (int row = 1; row <= ROW; row++)
 {
  printf("%02d ", row);
  printf(" |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n", mine_map[row][1], mine_map[row][2], mine_map[row][3], mine_map[row][4], mine_map[row][5],
   mine_map[row][6], mine_map[row][7], mine_map[row][8], mine_map[row][9]);
  printf("    ---------------------------\n");
 }
}
char IsFull(){
 int ful = COL*ROW - MINE_COUNT;
 for (int row = 1; row <= ROW; row++){
  for (int col = 1; col <= COL; col++){
   {
    if (show_map[row][col] == '1' || show_map[row][col] == '0')
    {
     ful--;
    }
   }
  }
 }
 if (ful == 0)
 {
  return 'p';
 }
}
char PlayerMove(char mine_map[ROW + 2][COL + 2], char show_map[ROW + 2][COL + 2]){
 int row = -1;
 int col = -1;
 while (1)
 {
  printf("請玩家選的位置(輸入格式:坐標 坐標):");
  scanf("%d %d", &row, &col);
  if (row < 1 || row>ROW || col>COL || col < 1)
  {
   printf("輸入越界,請重新輸入!\n");
   continue;
  }
  if (row > 0 && row<10 && col>0 && col < 10)
  {
   if (show_map[row][col] == '*')
   {
    //1.有雷 顯示雷區,結束游戲
    if (mine_map[row][col] == '1')
    {
     MineBoom();
     return 'n';
     break;
    }
    //3.顯示此位的周圍一圈 是否有雷
    else if (mine_map[row][col] == '0')
    {
     int count = '0';
     if (mine_map[row - 1][col - 1] == '1')
     {
      count++;
     }
     if (mine_map[row - 1][col ] == '1')
     {
      count++;
     }
     if (mine_map[row - 1][col + 1] == '1')
     {
      count++;
     }
     if (mine_map[row ][col - 1] == '1')
     {
      count++;
     }
     if (mine_map[row][col + 1] == '1')
     {
      count++;
     }
     if (mine_map[row + 1][col - 1] == '1')
     {
      count++;
     }
     if (mine_map[row + 1][col ] == '1')
     {
      count++;
     }
     if (mine_map[row + 1][col + 1] == '1')
     {
      count++;
     }
     {
      show_map[row][col] = count;
      Print();
      return 'k';
     }
     
    }
    else{
     printf("已經選過,請重新輸入!\n");
     continue;
    }
   }
   else{
    printf("輸入非法,請重新輸入!\n");
    continue;
   }
  }
 }
}
void Game(){
 if (Menu() == 1)//1.選擇菜單
 {
  Init();//2.初始化,布雷
  Print(); //3.打印棋盤
  while (1){
   if (PlayerMove(mine_map,show_map) == 'n'){
    printf("踩到雷啦,游戲結束!\n");
    break;
   }
   else if (IsFull() == 'p'){
    printf("恭喜玩家勝利!\n");
    break;
   }
   else {
    continue;
   }
  }
 }
 system("pause");
}
int main(){
 Game();
 
 return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM