预备知识:
0x1游戏的运行:
1.随机布置雷
2.排雷
基本规则:
点开一个格子后,显示1,对于9*9,代表以1为中心的去心九宫格内有一个雷,找到后标记
,直到标完所有的雷,游戏结束;如果中途点中雷,游戏结束
0x2.编写过程
现创建9*9的雷区,编写代码
但这样会程出现问题,边缘的雷需要另外写代码(否则九宫格越界)-->为了方便,扩大一圈(11*11)
定义二维数组:char board[11][11]
两个二维数组:一个布雷,一个显示玩家排雷的状态
game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
void menu();
void game();
void InitBoard(char board[ROWS][COLS], int row, int col, char set);
void SetMine(char board[ROWS][COLS], int row, int col);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int GetMineCount(char mine[ROWS][COLS], int x, int y);
DisplayBoard.c
#include "game.h"
void DisplayBoard(char board[ROWS][COLS],int row,int col)
{
printf("-------99扫雷-------\n");
printf("0 ");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);//打印行
}
printf("y");
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");
}
printf("x");
printf("\n--------------------\n");
}
FineMine.c
#include "game.h"
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)
{
//DisplayBoard(mine, ROW, COL);//作弊模式:)
printf("输入排查雷的坐标 x y :>");
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);
Sleep(3000);
system("cls");
break;
}
else
{
win++;
system("cls");
int sum = GetMineCount(mine, x, y);//t统计去心九宫格雷的个数
show[x][y] = sum + '0';
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("输入有误,重新输入\n");
}
}
if (win== row * col - COUNT)
printf("赢了\n");
}
game.c
#include "game.h"
void game()
{
char mine[ROWS][COLS] = { 0 };//定义布雷数组
char show[ROWS][COLS] = { 0 };//定义显示玩家排雷的状态的数组
InitBoard(mine, ROWS, COLS, '0');//初始化布雷数组
InitBoard(show, ROWS, COLS, '?');//初始化显示玩家排雷的状态的数组
SetMine(mine, ROW,COL);//布雷
DisplayBoard(show, ROW, COL);//打印显示玩家排雷的状态的数组
FindMine(mine,show,ROW,COL);//玩家找雷
}
GetMineCount.c
#include "game.h"
int GetMineCount(char mine[ROWS][COLS],int x,int y)
{
return mine[x][y + 1] + mine[x][y - 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * '0';
}
InitBoard.c
#include "game.h"
//初始化雷区
void InitBoard(char board[ROWS][COLS], int row, int col, char set)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
main.c
#include "game.h"
int main()
{
int tmp = 0;
srand((unsigned int)time(NULL));
do
{
menu();//调用菜单函数
scanf("%d", &tmp);
switch (tmp)
{
case 2:
break;
default:
{
printf("输入错误,重新选择!\n");
Sleep(1000);
system("cls");
break;
}
case 1:
{
system("cls");
game();
break;
}
}
}
while (tmp != 2);
return 0;
}
menu.c
#include "game.h"
void menu()//主菜单函数
{
printf("**********************************************\n");
printf("*******************1.PLAY********************\n");
printf("*******************2.EXIT*********************\n");
printf("**********************************************\n");
printf("请选择:\n");
}
SetMine.c
#include "game.h"
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = 10;
while (count)
{
int x = rand() % row + 1;//x:1~9
int y = rand() % col + 1;//y:1~9
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
标签:ROWS,33,COLS,C语言,char,int,game,扫雷,mine From: https://blog.csdn.net/2401_85828611/article/details/140641664