整体思路

这个就是扫雷的99的界面,我们知道扫雷的规律就是点一个点就会有出现雷或者安全的点。当周围有几个雷时还会告诉你有几个雷。 这么一想,我们可能就必须要遍历周围的8个点,但是我们遍历边界等等时候就会越界,所以不妨我们可以多设置两行加两列,里面什么也不放,方便我们找周围有几个雷。 一般99 的棋盘只有十个雷,所以我们先将棋盘的基本属性定义一下。
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
1.主函数体的游戏体简单设置
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("********** 1.play *********\n");
printf("********** 2.exit *********\n");
printf("**************************************\n");
}
void game()
{
;
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("Please select!\n");
scanf("%d", &input);
switch (input)
{
case 0:
printf("exit successful!\n");
break;
case 1:
game();
break;
default :
printf("Select error,please input again\n");
}
} while (input);
return 0;
}
2.void game()的实现
怎么去实现这个游戏呢,首先我们的想法就是在棋盘中操作,没有出现的设置为*号以视为神秘感,雷又用重新一种,那是不是棋盘显得太乱了呢? 所以我们的想法就是设置两个棋盘,一个用来存放雷的信息,一个就是用来打印,让玩家看到。 第一步:定义两个棋盘
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
第二步:初始化棋盘
Initboard(mine, ROWS, COLS, '0');
Initboard(show, ROWS, COLS, '*');
那我们应该怎么去初始化棋盘呢?这个就很简单了只需要把棋盘全部设为 0 或者 * 就可以了。
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
第三步:打印棋盘
void Displayboard(char board[ROWS][COLS], int row, int col);
void Displayboard(char board[ROWS][COLS], int row, int col)
{
printf("-------------------\n");
int i, j;
for (i = 0; i < row + 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i < row + 1; i++)
{
printf("%d ", i);
for (j = 1; j < col + 1; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-------------------\n");
}
第四步:布置雷(很重要)
直接看代码里面的注释,其实不难,但是需要理解。
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = 'X';
count--;
}
}
}
第五步:搜索周围有几个雷  如图,我们要知道黑色的点周围有几个雷就需要遍历周围的点有几个雷。 我们前面将随机生成的雷设置成了’X’,所以我们就需要循环遍历。然后再返回遍历到了有几个雷。
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int i, j, count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == 'X')
count++;
}
}
return count;
}
第六步:排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int win = 0;
while (win < row*col - EASY_COUNT)
{
printf("Please enter the coordinates->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
if (mine[x][y] == 'X')
{
printf("I am so sorry you got blown up!\n");
Displayboard(mine, row, col);
break;
}
else
{
int* p = &win;
int count = GetMineCount(mine, x, y);
if (count == 0)
show[x][y] = count + '0';
win++;
Displayboard(show, row, col);
}
}
else
{
printf("Beyond the border,please input again\n");
}
}
if (win == row*col - EASY_COUNT)
{
printf("You may be a pig , but you win!\n");
Displayboard(mine, row, col);
}
}
3.完整代码
1.game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
2.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;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
printf("-------------------\n");
int i, j;
for (i = 0; i < row + 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i < row + 1; i++)
{
printf("%d ", i);
for (j = 1; j < col + 1; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = 'X';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int i, j, count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == 'X')
count++;
}
}
return count;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int win = 0;
while (win < row*col - EASY_COUNT)
{
printf("Please enter the coordinates->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
if (mine[x][y] == 'X')
{
printf("I am so sorry you got blown up!\n");
Displayboard(mine, row, col);
break;
}
else
{
int* p = &win;
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
win++;
Displayboard(show, row, col);
}
}
else
{
printf("Beyond the border,please input again\n");
}
}
if (win == row*col - EASY_COUNT)
{
printf("You may be a pig , but you win!\n");
Displayboard(mine, row, col);
}
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("********** 1.play *********\n");
printf("********** 2.exit *********\n");
printf("**************************************\n");
}
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(mine, ROW, COL);
Displayboard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("Please select!\n");
scanf("%d", &input);
switch (input)
{
case 0:
printf("exit successful!\n");
break;
case 1:
game();
break;
default :
printf("Select error,please input again\n");
}
} while (input);
return 0;
}
|