C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷功能
這是我跟著學(xué)習(xí)視頻完成的第一個(gè)小游戲,運(yùn)用到的知識(shí)不多都是數(shù)組相關(guān)的知識(shí),重要的是思路,在設(shè)計(jì)的時(shí)候要先繪制出大概的框圖,要知道游戲的根本,這樣會(huì)讓你寫程序的時(shí)候更加方便。
下面看代碼:
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("請(qǐng)選擇: ");
scanf("%d", &input);
switch (input)
{
case 1:
printf("掃雷游戲!\n");
game();
break;
case 0:
printf("已退出游戲!\n");
break;
default:
printf("輸入錯(cuò)誤,請(qǐng)重新輸入!\n");
break;
}
} while (input);
}
void game()
{
//雷的信息存儲(chǔ)
//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;
//打印列號(hào)
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
//打印行號(hào)
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("請(qǐng)輸入坐標(biāo): ");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//坐標(biāo)合法
//1.踩雷
if (mine[x][y] == '1')
{
printf("你被炸死了?。。n");
DisplyBoard(mine, row, col);
break;
}
//不是雷
else
{
//計(jì)算x,y坐標(biāo)周圍有幾個(gè)雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
DisplyBoard(show, row, col);
win++;
}
}
else
{
printf("坐標(biāo)非法,請(qǐng)重新輸入!");
}
}
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);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
OpenSSL動(dòng)態(tài)鏈接庫(kù)源碼安裝教程
Openssl 是一個(gè)開放源代碼的SSL協(xié)議的產(chǎn)品實(shí)現(xiàn),它采用C語(yǔ)言作為開發(fā)語(yǔ)言,具備了跨系統(tǒng)的性能。這篇文章主要介紹了OpenSSL動(dòng)態(tài)鏈接庫(kù)源碼安裝,需要的朋友可以參考下2021-11-11
VSCode遠(yuǎn)程代碼開發(fā)及DNS隧道端口轉(zhuǎn)發(fā)實(shí)現(xiàn)遠(yuǎn)程辦公代碼
這篇文章主要介紹了VSCode遠(yuǎn)程代碼開發(fā)及DNS隧道端口轉(zhuǎn)發(fā)實(shí)現(xiàn)遠(yuǎn)程辦公,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Matlab實(shí)現(xiàn)簡(jiǎn)單擴(kuò)頻語(yǔ)音水印算法詳解
本文主要介紹了通過MATLAB設(shè)計(jì)并實(shí)現(xiàn)一種基于音頻的擴(kuò)頻水印算法,從而了解參數(shù)對(duì)擴(kuò)頻水印算法性能的影響。代碼具有一定的價(jià)值,感興趣的小伙伴可以關(guān)注一下2021-11-11
剖析C++中的常量表達(dá)式與省略號(hào)的相關(guān)作用
這篇文章主要介紹了C++中的常量表達(dá)式與省略號(hào)的相關(guān)作用,以及表達(dá)式中的可變參數(shù)模板示例,需要的朋友可以參考下2016-01-01
C語(yǔ)言實(shí)現(xiàn)魔方陣算法(幻方陣 奇魔方 單偶魔方實(shí)現(xiàn))
魔方陣是指由1,2,3……n2填充的,每一行、每一列、對(duì)角線之和均相等的方陣,階數(shù)n = 3,4,5…。魔方陣也稱為幻方陣,看下面的實(shí)現(xiàn)方法吧2013-11-11
C語(yǔ)言驅(qū)動(dòng)開發(fā)之內(nèi)核通過PEB獲取進(jìn)程參數(shù)
PEB結(jié)構(gòu)(Process Envirorment Block Structure)其中文名是進(jìn)程環(huán)境塊信息。本文將通過PEB實(shí)現(xiàn)獲取進(jìn)程參數(shù),感興趣的小伙伴可以了解一下2022-10-10
C語(yǔ)言循環(huán)語(yǔ)句之重復(fù)執(zhí)行特定的代碼塊
在C語(yǔ)言中分支和循環(huán)語(yǔ)句是實(shí)現(xiàn)條件執(zhí)行和重復(fù)執(zhí)行的重要工具,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言循環(huán)語(yǔ)句之重復(fù)執(zhí)行特定的代碼塊的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01

