C語言代碼實(shí)現(xiàn)簡易掃雷
更新時間:2021年01月19日 08:15:20 作者:幽沫沫
這篇文章主要為大家詳細(xì)介紹了C語言代碼實(shí)現(xiàn)簡易掃雷,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言代碼實(shí)現(xiàn)簡易掃雷的具體代碼,供大家參考,具體內(nèi)容如下
源.c代碼如下:
#define _CRT_SECURE_NO_WARNINGS
#include"Game.h"
void Game()
{
//創(chuàng)建兩個雷區(qū),一個記錄雷,一個展示給玩家
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化兩個雷區(qū)
Init_board(mine, ROWS, COLS, '0');
Init_board(show, ROWS, COLS, '*');
//打印雷區(qū)
Prin_board(show, ROW, COL);
//布置地雷
PlaceMine(mine, ROWS, COLS);
//開始掃雷
FoundMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
//打印菜單
int input = 0;
printf("**********************************\n");
printf("******* 1.play 0.exit *******\n");
printf("**********************************\n");
do
{
printf("請選擇:\n");
scanf("%d", &input);
switch(input)
{
case 1:
Game();
printf("再來一局請輸入1,退出請按0\n");
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("請輸入1或0:\n");
break;
}
} while (input);
return 0;
}
Game.h代碼如下:
#include<stdio.h> #include<time.h> #include<stdlib.h> #define ROWS 11 #define COLS 11 #define ROW ROWS - 2 #define COL COLS - 2 #define EASY 10 void Init_board(char board[ROWS][COLS], int rows, int cols, char set); void Prin_board(char board[ROWS][COLS], int row, int col); void PlaceMine(char board[ROWS][COLS], int row, int col); void FoundMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col);
Game.c代碼如下:
#define _CRT_SECURE_NO_WARNINGS
#include"Game.h"
int Sum(char board[ROWS][COLS], int x, int y)
{
int tem = y;//備份y的值,方便循環(huán)中初始化y值
//(x-1,y-1) (x-1,y) (x-1,y+1)
//(x,y-1) (x,y) (x,y+1)
//(x+1,y-1) (x+1,y) (x+1,y+1)
int i = 0, j = 0;
int sum = 0;
for (i = 0; i < 3; i++, x++)
{
for (j = 0, y = tem; j < 3; j++, y++)
{
sum += board[x - 1][y - 1];
}
}
sum = sum - 8 * (int)'0';
return sum;
}
void Init_board(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0, j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void Prin_board(char board[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
//打印列號
for (i = 0; i < 10; i++)
{
printf("%d ", i);
if (i == 0)
printf(" ");
}
printf("\n\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印行號
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void PlaceMine(char board[ROWS][COLS], int row, int col)
{
int count = 20;
while (count)//控制地雷個數(shù)
{
int x = 0, y = 0;
x = rand() % 9 + 1;
y = rand() % 9 + 1;
board[x][y] = '1';
count--;
}
}
void FoundMine(char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
//判斷輸入坐標(biāo)是否合法
while (1)
{
printf("請輸入排雷的坐標(biāo):\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board1[x][y] == '1')
{
printf("游戲結(jié)束,你踩雷了\n");
break;
}
else
{
board2[x][y] = Sum(board1, x, y);
Prin_board(board2, ROW, COL);
}
}
else
printf("坐標(biāo)非法\n");
}
if (board1[x][y] != '1')
printf("恭喜你完成游戲!\n");
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)馬賽克和毛玻璃濾鏡特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下方法2019-05-05
wchar_t,char,string,wstring之間的相互轉(zhuǎn)換
以下是對wchar_t,char,string,wstring之間的相互轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-09-09

