溫馨提示×

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

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

C語(yǔ)言怎樣實(shí)現(xiàn)掃雷游戲

發(fā)布時(shí)間:2021-06-09 09:46:52 來(lái)源:億速云 閱讀:123 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了C語(yǔ)言怎樣實(shí)現(xiàn)掃雷游戲,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

概述

掃雷是一款大眾類的益智小游戲。游戲目標(biāo)是根據(jù)點(diǎn)擊格子出現(xiàn)的數(shù)字找出所有非雷格子,同時(shí)避免踩雷,踩到一個(gè)雷即全盤皆輸。

實(shí)現(xiàn)過(guò)程

1、創(chuàng)建一個(gè)用戶交互菜單
2、布雷函數(shù)
3、顯示掃雷矩陣
4、玩家自定義坐標(biāo)
5、計(jì)算排雷數(shù)

多文件實(shí)現(xiàn)

頭文件  clear_mine.h

#pragma once  //防止頭文件被重復(fù)包含
 
#define _CRT_SECURE_NO_WARNINGS 1   //實(shí)現(xiàn) scanf 編譯通過(guò)
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
 
#define ROW 8              
#define COL 8           
 
#define STYLE '?'        //初始化
#define NUM 20           //埋雷數(shù)
 
extern void Game();

源文件main.c

向玩家展示菜單欄

#include "clear_mine.h"
 
static void Menu()          //用戶交互菜單
{
 printf("########################\n");
 printf("#  1. Play      0.Exit #\n");
 printf("########################\n");
}
 
int main()
{
 int quit = 0;
 int select = 0;
 while (!quit)
 {
  Menu();
  printf("Please Enter# ");
  scanf("%d", &select);
  switch (select)
  {
  case 1:
   Game();
   break;
  case 0:
   quit = 1;
   break;
  default:
   printf("Position Error, Try Again!\n");
   break;
  }
 }
 
 printf("byebye!\n");
 
 system("pause");
 return 0;
}

C語(yǔ)言怎樣實(shí)現(xiàn)掃雷游戲

源文件clear_mine.c

#include "clear_mine.h"
 
static void SetMines(char board[][COL], int row, int col)     //布雷
{
 int count = NUM;
 while (count)
 {
  int x = rand() % (row - 2) + 1;
  int y = rand() % (col - 2) + 1;   //隨機(jī)數(shù)生成 矩陣長(zhǎng)寬分別-2 的隨機(jī)數(shù)
  if (board[x][y] == '0')     //非法判斷 只能同一個(gè)位置生成一個(gè)隨機(jī)數(shù)
  {
   board[x][y] = '1';
   count--;
  }
 }
}
 
static void ShowLine(int col)        
{
 for (int i = 0; i <= (col - 2); i++)
 {
  printf("----");
 }
 printf("\n");
}
static void ShowBoard(char board[][COL], int row, int col)    //顯示掃雷矩陣
{
 printf("     ");
 for (int i = 1; i <= (col - 2); i++)      //表頭數(shù)字打印
 {
  printf("%d   ", i);
 }
 printf("\n");
 ShowLine(col);        
 for (int i = 1; i <= (row - 2); i++)
 {
  printf("%-3d|", i);
  for (int j = 1; j <= (col - 2); j++)
  {
   printf(" %c |", board[i][j]);
  }
  printf("\n");
  ShowLine(col);
 }
}
 
static char CountMines(char board[][COL], int x, int y)      //計(jì)算某點(diǎn)周圍8個(gè)位置雷總數(shù)
{
 return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + \
  board[x][y + 1] + board[x + 1][y + 1] + board[x + 1][y] + \
  board[x + 1][y - 1] + board[x][y - 1] - 7 * '0';
}
 
void Game()
{
 srand((unsigned long)time(NULL));     //生成隨機(jī)數(shù)種子
 
 char show_board[ROW][COL];
 char mine_board[ROW][COL];
 
 memset(show_board, STYLE, sizeof(show_board));  //生成用戶顯示矩陣   
 memset(mine_board, '0', sizeof(mine_board));    //生成掃雷矩陣
 
 SetMines(mine_board, ROW, COL);            //布雷
 
 int count = (ROW - 2)*(COL - 2) - NUM;        //排雷
 
 while (count)
 {
  system("cls");                //清屏
  ShowBoard(show_board, ROW, COL);
  printf("Please Enter Your Position <x,y># ");
  int x = 0;
  int y = 0;
  scanf("%d %d", &x, &y);
  if (x < 1 || x > 10 || y < 1 || y > 10)    //非法性判斷
  {
   printf("Postion Error!\n");
   continue;
  }
  if (show_board[x][y] != STYLE){
   printf("Postion Is not *\n");
   continue;
  }
  if (mine_board[x][y] == '1'){
   printf("game over!\n");
   ShowBoard(mine_board, ROW, COL);
   break;
  }
  
  show_board[x][y] = CountMines(mine_board, x, y);
  count--;
 }
 
}

初始化好的掃雷矩陣 

C語(yǔ)言怎樣實(shí)現(xiàn)掃雷游戲

游戲體驗(yàn)結(jié)果

C語(yǔ)言怎樣實(shí)現(xiàn)掃雷游戲

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C語(yǔ)言怎樣實(shí)現(xiàn)掃雷游戲”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

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

AI