欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用C語(yǔ)言實(shí)現(xiàn)掃雷小游戲

 更新時(shí)間:2021年02月23日 14:26:33   作者:yuelinghou  
這篇文章主要為大家詳細(xì)介紹了利用C語(yǔ)言實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)掃雷小游戲的具體代碼,供大家參考,具體內(nèi)容如下

說(shuō)明:該游戲的實(shí)現(xiàn)需要建立三個(gè)文件

  • test2.c:整個(gè)游戲,開(kāi)始游戲/退出游戲的大體執(zhí)行流程
  • game2.c:具體實(shí)現(xiàn)掃雷游戲功能的函數(shù)定義
  • game.h:宏定義,函數(shù)聲明,引用相關(guān)C庫(kù)函數(shù)的頭文件

test2.c

游戲的大體執(zhí)行流程

#include"game2.h"

//菜單函數(shù)
void menu()
{
 printf("**************************\n");
 printf("******** 1.play ********\n");
 printf("******** 0.exit ********\n");
 printf("**************************\n");
}

//游戲函數(shù)
void game()
{
 //雷的信息儲(chǔ)存
 //1.雷分布的棋盤(pán)
 char mine[ROWS][COLS] = { 0 };//玩家不能看
 //2.排查雷信息的棋盤(pán)
 char show[ROWS][COLS] = { 0 };//玩家進(jìn)行游戲時(shí)候的棋盤(pán)

 //初始化棋盤(pán)
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(show, ROWS, COLS, '*');

 //打印棋盤(pán)
 DisplayBoard(show, ROW, COL);
 //布置雷
 SetMine(mine, ROW, COL);

 //掃雷
 FindMine(mine,show, ROW, COL);
}

int main()
{
 srand((unsigned int)time(NULL));//設(shè)置隨機(jī)數(shù)的生成起點(diǎn)
 int input = 0;
 do
 {
 menu();
 printf("請(qǐng)選擇:>");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 printf("退出游戲\n");
 break;
 default:
 printf("選擇錯(cuò)誤,請(qǐng)重新選擇\n");
 break;
 }
 } while (input);
 return 0;
}

game2.c

具體實(shí)現(xiàn)掃雷游戲功能的函數(shù)定義

#include"game2.h"

//對(duì)mine棋盤(pán),一開(kāi)始全初始化為'0',后面放雷的坐標(biāo)位置改為'1'
//對(duì)show棋盤(pán),一開(kāi)始全初始化為'*',后面掃雷時(shí)玩家選擇的坐標(biāo)位置改為周圍雷的個(gè)數(shù)
void InitBoard(char(*p)[COLS], int row, int col, char set)//set為需要初始化的字符
{
 int i = 0;
 int j = 0;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 *(*(p + i) + j) = set;
 }
 }
}

void DisplayBoard(char(*ps)[COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 //打印最上面一行的數(shù)字坐標(biāo)和分隔行
 printf(" ");
 for (i = 1; i <= 9; i++)
 {
 printf("%d ", i);
 }
 printf("\n");
 printf(" ");
 for (i = 1; i <= 9; i++)
 {
 printf("--");
 }
 printf("\n");
 //打印最左邊的數(shù)字坐標(biāo)和分隔行以及相應(yīng)的棋盤(pán)內(nèi)容
 for (i = 1; i <= row; i++)
 {
 printf("%d |", i);
 for (j = 1; j <= col; j++)
 {
 printf("%c ", *(*(ps + i) + j));
 }
 printf("\n");
 }
}

void SetMine(char(*pm)[COLS], int row, int col)
{
 int count = MAX_MINE;//count記錄需要放的雷的個(gè)數(shù)
 while (count)
 {
 int x = rand() % row + 1;//1-9的數(shù)字
 int y = rand() % col + 1;//1-9的數(shù)字
 //判斷隨機(jī)生成坐標(biāo)是已經(jīng)放雷
 if (*(*(pm + x) + y) == '0')
 {
 *(*(pm + x) + y) = '1';
 count--;
 }
 }
}

//把坐標(biāo)周圍8個(gè)位置的字符相加再減去8 * '0',ASCLL編碼的差值就是周圍雷的個(gè)數(shù)
static int Find_Mine_Count(char(*pm)[COLS], int x, int y)
{
 return
 (*(*(pm+x + 1) - 1+y) +
 *(*(pm+x + 1)+y) +
 *(*(pm+x + 1) + 1+y) +
 *(*(pm+x)- 1+y) +
 *(*(pm+x)+1+y) +
 *(*(pm+x - 1) - 1+y) +
 *(*(pm+x - 1)+y) +
 *(*(pm+x - 1) + 1+y)) - 8 * '0';
}

void FindMine(char(*pm)[COLS], char(*ps)[COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win < row*col - MAX_MINE)//當(dāng)所有非雷位置都被確認(rèn)時(shí),win=row*col - MAX_MINE
 {
 printf("請(qǐng)輸入坐標(biāo)(用空格分隔):>");
 scanf("%d%d", &x, &y);
 //1.判斷輸入坐標(biāo)是否正確
 if (x >= 1 && x <= row && y >= 1 && y <= col)//坐標(biāo)正確的情況
 {
 //2.判斷輸入坐標(biāo)是否重復(fù)
 if (*(*(ps + x) + y) != '*')//坐標(biāo)重復(fù)的情況
 {
 printf("該坐標(biāo)已被輸入\n");
 }
 else//坐標(biāo)不重復(fù)的情況
 {
 //3.判斷是否踩雷
 if (*(*(pm + x) + y) == '1')//踩雷的情況
 {
  printf("很遺憾,你被炸死了\n");
  DisplayBoard(pm, row, col);
  break;
 }
 else//沒(méi)踩雷的情況
 {
  int count = Find_Mine_Count(pm, x, y);
  *(*(ps + x) + y) = count + '0';
  DisplayBoard(ps, row, col);
 }
 }
 }
 else//坐標(biāo)錯(cuò)誤的情況
 {
 printf("輸入錯(cuò)誤\n");
 }
 }
 //最后判斷循環(huán)結(jié)束是因?yàn)椴壤字骲reak跳出的還是掃雷成功結(jié)束循環(huán)的
 
 if(win == row * col - MAX_MINE)
 {
 printf("恭喜你,掃雷成功!\n");
 }
}

game2.h

宏定義,函數(shù)聲明,引用相關(guān)C庫(kù)函數(shù)的頭文件

//引用庫(kù)函數(shù)的頭文件
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//聲明表示棋盤(pán)大小的量
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
#define MAX_MINE 10

//聲明函數(shù)
void InitBoard(char(*p)[COLS], int row, int col, char set);
void DisplayBoard(char(*ps)[COLS], int row, int col);
void SetMine(char(*pm)[COLS], int row, int col);
void FindMine(char(*pm)[COLS], char(*ps)[COLS], int row, int col);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論