C語(yǔ)言實(shí)現(xiàn)掃雷小程序
前言
《掃雷》是一款大眾類的益智小游戲,于1992年發(fā)行。游戲目標(biāo)是在最短的時(shí)間內(nèi)根據(jù)點(diǎn)擊格子出現(xiàn)的數(shù)字找出所有非雷格子,同時(shí)避免踩雷,踩到一個(gè)雷即全盤(pán)皆輸。
多文件形式
在實(shí)現(xiàn)游戲的首先,需要?jiǎng)?chuàng)建test.c game.c game.h三個(gè)文件。
test.c主要進(jìn)行游戲的測(cè)試。
game.c主要進(jìn)行游戲內(nèi)部一些函數(shù)的具體實(shí)現(xiàn)。
game.h主要是一些聲明,宏定義。
游戲邏輯
1、打印簡(jiǎn)易菜單
2、定義及初始化數(shù)組
3、隨機(jī)生成布置雷
4、玩家排雷
游戲?qū)崿F(xiàn)

打印簡(jiǎn)易菜單
打印菜單讓玩家選擇,輸入1為開(kāi)始游戲,輸入0位退出游戲
void meun()
{
printf("*********************************\n");
printf("*********************************\n");
printf("*********** 1. play ***********\n");
printf("*********** 0. exit ***********\n");
printf("*********************************\n");
printf("*********************************\n");
}
初始化數(shù)組
程序生成兩個(gè)99的方塊矩陣,mine數(shù)組用來(lái)保存雷的布局,show數(shù)組用來(lái)展示玩家實(shí)時(shí)的排雷進(jìn)度。
mine數(shù)組中,‘1'代表雷,'0‘代表無(wú)雷。
show數(shù)組中,'‘代表雷,'#'代表無(wú)雷。
//初始化數(shù)組
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
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;
}
}
}
打印方塊矩陣
玩家每確定一次排雷坐標(biāo),就要在屏幕上顯示排雷情況。
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < 10; 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");
}
}
布置雷
利用C語(yǔ)言庫(kù)中的rand函數(shù)生成隨機(jī)數(shù)來(lái)布置雷。
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
玩家排雷
玩家輸入排雷的坐標(biāo),程序會(huì)在屏幕顯示當(dāng)前坐標(biāo)周?chē)?個(gè)方塊存在的雷的數(shù)量。
int MineCount(char mine[ROWS][COLS], int x, int y)
{
return (mine[x + 1][y + 1] +
mine[x + 1][y] +
mine[x + 1][y - 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] +
mine[x - 1][y] +
mine[x - 1][x - 1]) - 8 * '0';
}
void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
int x = 0;
int y = 0;
while (win < row * col - EASY_COUNT)
{
printf("請(qǐng)輸入要排查的坐標(biāo):>");
scanf("%d %d", &x, &y);
if (x <= row && x >= 1 && y <= col && y >= 1)
{
if (mine[x][y] == '0')
{
int count = MineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
else
{
printf("很遺憾,你被炸死了\n");
break;
}
}
else
{
printf("下標(biāo)錯(cuò)誤,請(qǐng)重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
完整代碼
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void meun()
{
printf("*********************************\n");
printf("*********************************\n");
printf("*********** 1. play ***********\n");
printf("*********** 0. exit ***********\n");
printf("*********************************\n");
printf("*********************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化數(shù)組
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排雷
FindBoard(mine, show, ROW, COL);
DisplayBoard(show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
meun();
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;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < 10; 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");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int MineCount(char mine[ROWS][COLS], int x, int y)
{
return (mine[x + 1][y + 1] +
mine[x + 1][y] +
mine[x + 1][y - 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] +
mine[x - 1][y] +
mine[x - 1][x - 1]) - 8 * '0';
}
void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
int x = 0;
int y = 0;
while (win < row * col - EASY_COUNT)
{
printf("請(qǐng)輸入要排查的坐標(biāo):>");
scanf("%d %d", &x, &y);
if (x <= row && x >= 1 && y <= col && y >= 1)
{
if (mine[x][y] == '0')
{
int count = MineCount(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
win++;
}
else
{
printf("很遺憾,你被炸死了\n");
break;
}
}
else
{
printf("下標(biāo)錯(cuò)誤,請(qǐng)重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
game.h
#pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define EASY_COUNT 10 #define ROWS ROW+2 #define COLS COL+2 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col); void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中vector的模擬實(shí)現(xiàn)實(shí)例詳解
vector是表示可變大小數(shù)組的序列容器,它也采用連續(xù)存儲(chǔ)空間來(lái)存儲(chǔ)元素,因此可以采用下標(biāo)對(duì)vector的元素進(jìn)行訪問(wèn),這篇文章主要給大家介紹了關(guān)于C++中vector模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-11-11
C語(yǔ)言之字符串模糊查詢方法的實(shí)現(xiàn)
本篇文章主要為大家介紹字符串模糊查詢的C語(yǔ)言程序編寫(xiě)方法,有需要的朋友可以參考下2015-07-07
C++設(shè)計(jì)模式之享元模式(Flyweight)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之享元模式Flyweight,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Visual?Studio?Code?配置C、C++?文件debug調(diào)試環(huán)境的詳細(xì)過(guò)程
這篇文章主要介紹了Visual?Studio?Code?配置C、C++?文件debug調(diào)試環(huán)境,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
C++基于單鏈表實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++基于單鏈表實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
C++實(shí)現(xiàn)訪問(wèn)者模式的基礎(chǔ)介紹
訪問(wèn)者模式表示一個(gè)作用于某對(duì)象結(jié)構(gòu)中各元素的操作,它使我們可以在不改變各元素的類的前提下定義作用于這些元素的新操作。對(duì)C++訪問(wèn)者模式相關(guān)知識(shí)感興趣的朋友一起看看吧2021-09-09
Cocos2d-x學(xué)習(xí)筆記之開(kāi)發(fā)環(huán)境搭建
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)筆記之開(kāi)發(fā)環(huán)境搭建,本文使用Visual Studio作為開(kāi)發(fā)IDE,是不同于其它教程的,需要的朋友可以參考下2014-09-09

