C語言實現簡單的掃雷功能

這是我跟著學習視頻完成的第一個小遊戲,運用到的知識不多都是數組相關的知識,重要的是思路,在設計的時候要先繪制出大概的框圖,要知道遊戲的根本,這樣會讓你寫程序的時候更加方便。

下面看代碼:

test.c

#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
 
void test();
void menu();
void game();
 
 
int main()
{
 
 
 test();
 return 0;
}
void menu()
{
 printf("*************************\n");
 printf("******    1.play   ******\n");
 printf("******    0.exit   ******\n");
 printf("*************************\n");
}
void test()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 do
 {
  menu();
  printf("請選擇: ");
  scanf("%d", &input);
  switch (input)
  {
  case 1:
   printf("掃雷遊戲!\n");
   game();
   break;
  case 0:
   printf("已退出遊戲!\n");
   break;
  default:
   printf("輸入錯誤,請重新輸入!\n");
   break;
  }
 } while (input);
}
void game()
{
 //雷的信息存儲
 //1.佈置雷的信息
 char mine[ROWS][COLS] = { 0 }; //11*11
 //2.排查出的雷的信息
 char show[ROWS][COLS] = { 0 };
 //初始化
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(show, ROWS, COLS, '*');
 //打印棋盤
 DisplyBoard(mine, ROW, COL);
 //DisplyBoard(show, ROW, COL);
 //佈置雷
 SetMine(mine, ROW, COL);
 DisplyBoard(mine, ROW, COL);
 //掃雷
 FindMine(mine, show, ROW, COL);
 
}

game.c

#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
 
//'1'-'0'=1
//'3'-'0'=3
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
 return mine[x - 1][y - 1] - '0' +
  mine[x][y - 1] - '0' +
  mine[x + 1][y - 1] - '0' +
  mine[x + 1][y] - '0' +
  mine[x + 1][y + 1] - '0' +
  mine[x][y + 1] - '0' +
  mine[x - 1][y + 1] - '0' +
  mine[x - 1][y] - '0';
}
 
 
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < rows; i++)
 {
  for (j = 0; j < cols; j++)
  {
   board[i][j] = set;
  }
 }
}
 
 
void DisplyBoard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 //打印列號
 for (i = 0; i <= row; 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 SetMine(char board[ROWS][COLS], int row, int col)
{
 int count = COUNT;
 while (count)
 {
  int x = rand() % row + 1;
  int y = rand() % col + 1;
  if (board[x][y] == '0')
  {
   board[x][y] = '1';
   count--;
  }
 
 }
}
 
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win < ROW * COL - COUNT)
 {
  printf("請輸入坐標: ");
  scanf("%d%d", &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
   //坐標合法
   //1.踩雷
   if (mine[x][y] == '1')
   {
    printf("你被炸死瞭!!!\n");
    DisplyBoard(mine, row, col);
    break;
   }
   //不是雷
   else
   {
    //計算x,y坐標周圍有幾個雷
    int count = get_mine_count(mine, x, y);
    show[x][y] = count + '0';
    DisplyBoard(show, row, col);
    win++;
   }
 
  }
  else
  {
   printf("坐標非法,請重新輸入!");
  }
 }
 if (win ==  ROW * COL - COUNT)
 {
  printf("恭喜你,排雷成功!!!\n");
  DisplyBoard(mine, row, col);
 }
}

game.h

#define ROW 9
#define COL 9
 
#define ROWS ROW+2
#define COLS COL+2
 
#define COUNT 80
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplyBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: