目錄
1 -> test.c
2 -> game.c
3 -> game.h
1 -> test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("************************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("************************************\n");
}
void game()
{
//show數(shù)組為排查出的雷的信息
char show[ROWS][COLS] = { 0 };
//mine數(shù)組為布置好的雷的信息
char mine[ROWS][COLS] = { 0 };
//初始化棋盤(pán)
InitBoard(show, ROWS, COLS, '*');
InitBoard(mine, ROWS, COLS, '0');
//打印棋盤(pán)
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排查雷
FineMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
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;
}
2 -> game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//初始化棋盤(pán)
void InitBoard(char board[ROWS][ROWS], int rows, int cols, char set)
{
memset(&board[0][0], set, rows * cols * sizeof(board[0][0]));
}
//打印棋盤(pán)
void DisplayBoard(char board[ROWS][ROWS], int row, int col)
{
printf("****** 掃雷 ******\n");
for (int j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int cnt = EASY_COUNT;
while (cnt)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
cnt--;
}
}
}
//計(jì)算周?chē)椎臄?shù)量
int GetMineCount(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][y + 1] - 8 * '0');
}
//排查雷
void FineMine(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):>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遺憾,排雷失敗。\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int cnt = GetMineCount(mine, x, y);
show[x][y] = cnt + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐標(biāo)輸入錯(cuò)誤,請(qǐng)重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你排雷成功!!!\n");
}
}
3 -> game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//棋盤(pán)大小
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
//雷的個(gè)數(shù)
#define EASY_COUNT 10
//初始化棋盤(pán)
void InitBoard(char board[ROWS][ROWS], int rows, int cols, char set);
//打印棋盤(pán)
void DisplayBoard(char board[ROWS][ROWS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//計(jì)算周?chē)椎臄?shù)量
int GetMineCount(char mine[ROWS][COLS], int x, int y);
//排查雷
void FineMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
代碼里的注釋感覺(jué)已經(jīng)很清楚啦,就不多講解啦文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-814673.html
感謝各位大佬的支持?。。?/span>文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-814673.html
到了這里,關(guān)于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!