在9*9的矩阵中随机生成10个“雷”,输出“雷”的分布矩阵(扫雷游戏的初始状态)。要求“雷”的位置输出字符(ASCII码15),非“雷”的位置输出附近有多少颗“雷”,附近无雷不输出。
提示:
① 声明一个9×9的二维数组,并将每个元素初始化为0;
② 随机生成10个“雷”:可使用随机函数生成10对不重复的行列下标,将其值置为-1,之后使用循环累计每个非-1元素周围-1的个数。
③ 计算非“雷”位置附近有多少“雷”,即当数组元素不等于-1时,计算它上下左右8个数组元素中值为-1的个数。可以累加这几个数组元素是否为-1的关系表达式和逻辑表达式之和来完成。例如计算数组元素a[i][j]附近上一行的“雷”个数可以使用如下表达式:
“(i>0&&j>0&&a[i-1][j-1]==-1)+(i>0&&a[i-1][j]==-1)+(i>0&&j<8&&a[i-1][j+1]==-1)”。
#include <iostream>
#include <random>
#include <time.h>
void main()
{
char matrix[9][9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
matrix[i][j] = 0;
}
}
srand(time(NULL));
int n = 10;
while (n--)
{
int line = rand() % 9;
int row = rand() % 9;
if (matrix[line][row] == 0)
{
matrix[line][row] = -1;
}
else
{
n++;
continue;
}
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (matrix[i][j] != -1)
{
int leftIndex = j - 1;
int rightIndex = j + 1;
int topIndex = i - 1;
int bottomIndex = i + 1;
int left = leftIndex;
int top = topIndex;
int leftMoveStep = 0;
int topMoveStep = 0;
int count = 0;
while (true)
{
if (left >= 0 && left < 9 && top >= 0 && top < 9)
{
if (matrix[top][left] == -1)
{
count++;
}
}
if (left == leftIndex && top == i)
{
break;
}
if (left - leftIndex < 2 && top == topIndex)
{
leftMoveStep = 1;
topMoveStep = 0;
}
else if (left == rightIndex && top == topIndex)
{
topMoveStep = 1;
leftMoveStep = 0;
}
else if (left == rightIndex && top == bottomIndex)
{
topMoveStep = 0;
leftMoveStep = -1;
}
else if (left == leftIndex && top == bottomIndex)
{
leftMoveStep = 0;
topMoveStep = -1;
}
else if (left - leftIndex < 2 && top == bottomIndex)
{
leftMoveStep = -1;
topMoveStep = 0;
}
left += leftMoveStep;
top += topMoveStep;
}
matrix[i][j] = count;
count > 0 ? printf("%d ", count) : printf(" ");
}
else
{
printf("%c ", 15);
}
}
printf("\n");
}
getchar();
}
#include <iostream>
#include <random>
#include <time.h>
void main()
{
char matrix[9][9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
matrix[i][j] = 0;
}
}
srand(time(NULL));
int n = 10;
while (n--)
{
int line = rand() % 9;
int row = rand() % 9;
if (matrix[line][row] != -1)
{
matrix[line][row] = -1;
int leftIndex = row - 1;
int rightIndex = row + 1;
int topIndex = line - 1;
int bottomIndex = line + 1;
int left = leftIndex;
int top = topIndex;
int leftMoveStep = 0;
int topMoveStep = 0;
int count = 0;
while (true)
{
if (left >= 0 && left < 9 && top >= 0 && top < 9)
{
if (matrix[top][left] != -1)
{
matrix[top][left] += 1;
}
}
if (left == leftIndex && top == line)
{
break;
}
if (left - leftIndex < 2 && top == topIndex)
{
leftMoveStep = 1;
topMoveStep = 0;
}
else if (left == rightIndex && top == topIndex)
{
topMoveStep = 1;
leftMoveStep = 0;
}
else if (left == rightIndex && top == bottomIndex)
{
topMoveStep = 0;
leftMoveStep = -1;
}
else if (left == leftIndex && top == bottomIndex)
{
leftMoveStep = 0;
topMoveStep = -1;
}
else if (left - leftIndex < 2 && top == bottomIndex)
{
leftMoveStep = -1;
topMoveStep = 0;
}
left += leftMoveStep;
top += topMoveStep;
}
}
else
{
n++;
continue;
}
}
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (matrix[i][j] != -1)
{
matrix[i][j] > 0 ? printf("%d ", matrix[i][j]) : printf(" ");
}
else
{
printf("%c ", 15);
}
}
printf("\n");
}
getchar();
}