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

C語言代碼實(shí)現(xiàn)掃雷游戲

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

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

該游戲我們也是利用數(shù)組完成,設(shè)置倆個(gè)數(shù)組一個(gè)mine數(shù)組,一個(gè)show數(shù)組。

mine數(shù)組存放雷,show數(shù)組顯示棋盤并且顯示該位置是否有雷或者顯示該位置周圍有幾個(gè)雷。

數(shù)組大小有講究,我們宏定義變量 ROW COL 為9 定義ROWS COLS為11,我們顯示的是9X9的棋盤,也是將雷設(shè)置在9X9的位置內(nèi),但是我們?cè)O(shè)置數(shù)組是設(shè)置11X11,因?yàn)檫@樣方便我們遍歷9X9棋盤四邊位置上某位置四周雷的數(shù)目,不然的話會(huì)發(fā)生越界錯(cuò)誤。

對(duì)于雷的符號(hào),我們?cè)O(shè)置空位置為 0,有雷的位置為1,這樣易于我們統(tǒng)計(jì)某一位置周圍有多少雷的數(shù)目。

重點(diǎn)在于掃雷函數(shù),玩家輸入x y位置,我們判斷該位置是否有雷,否的話判斷該位置周圍有多少雷并在存于show數(shù)組display給玩家。并且我們輸入一個(gè)位置當(dāng)該位置不是雷的時(shí)候,計(jì)數(shù)器count++;若果該計(jì)數(shù)器count==col*row-EAXY_COUNT;我們就判斷玩家贏。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

#define EASY_COUNT 10

//函數(shù)聲明
void ChushiBoard(char board[ROWS][COLS],int rows,int cols,char set);
void Dayinboard(char board[ROWS][COLS], int row, int col);
void BuzhiBoard(char mine[ROWS][COLS], int row, int col);
void CaoleiBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);

void menu()
{
 printf("*****************************************\n");
 printf("*****************************************\n");
 printf("**** 輸入1開始游戲 輸入0退出游戲 *****\n");
 printf("*****************************************\n");
 printf("*****************************************\n\n\n");
}

void game()
{
 char mine[ROWS][COLS];//存放雷
 char show[ROWS][COLS];//顯示排查出來的雷
 //初始化
 ChushiBoard(mine, ROWS, COLS,'0');//'0'
 ChushiBoard(show, ROWS, COLS,'*');//'*'
 //布置雷
 BuzhiBoard(mine,ROW,COL);
 //Dayinboard(mine, ROW, COL);
 //打印棋盤
 Dayinboard(show, ROW, COL);
 //掃雷
 CaoleiBoard(mine,show,ROW,COL);
}



void test()
{
 int input = 0;
 do
 {
 menu();
 printf("請(qǐng)輸入:>");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 printf("退出游戲\n\n");
 break;
 default:
 printf("輸入錯(cuò)誤,重新輸入\n\n");
 break;
 }
 } while (input);
}


int main()
{
 test();
 system("pause");
 return 0;
}


void ChushiBoard(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 Dayinboard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int 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 BuzhiBoard(char mine[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;
 while (count)
 {
 int x = rand()%row+1;
 int y = rand()%col+1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 count--;
 }
 }
}

static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
 return mine[x - 1][y] +
 mine[x - 1][y - 1] +
 mine[x][y - 1] +
 mine[x + 1][y - 1] +
 mine[x + 1][y] +
 mine[x + 1][y + 1] +
 mine[x][y + 1] +
 mine[x - 1][y + 1] - 8 * '0';
}



void CaoleiBoard(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-EASY_COUNT)
 {
 printf("請(qǐng)輸入要排查的坐標(biāo)(格式:X空格X回車):>");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
 {
 if (mine[x][y] == '1')
 {
 printf(" 很遺憾,你被炸死了?。?!\n");
 Dayinboard(mine, row, col);
 break;
 }
 else
 {
 int count = GetMineCount(mine,x,y);
 show[x][y] = count+'0';
 Dayinboard(show, row, col);
 win++;
 }

 }
 else
 {
 printf("輸入的坐標(biāo)非法\n");
 }
 }
 if (win == row*col - EASY_COUNT)
 {
 printf(" 恭喜你,你排雷成功了?。?!\n");
 }
}

運(yùn)行結(jié)果:

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

相關(guān)文章

最新評(píng)論