完整程序是这样的
#include <stdio.h>
#include <stdlib.h>
// Possible square states.
#define VISIBLE_SAFE 0
#define HIDDEN_SAFE 1
#define HIDDEN_MINE 2
// The size of the starting grid.
#define SIZE 8
// Place your function prototyes here.
typedef struct _point
{
int x;
int y;
} Point;
//function.
void first_is_2(int b,int c,int d,Point point[],int number);
void first_is_3(int b,int c,Point point[],int i,int minefield[][SIZE]);
void init_field(int minefield[][SIZE], Point point[], int number);
void print_minefield(int minefield[][SIZE]);
void init_field(int minefield[][SIZE], Point point[], int number)
{
int i = 0;
while (i < SIZE)
{
int j = 0;
while (j < SIZE)
{
minefield[i][j] = HIDDEN_SAFE;
j++;
}
i++;
}
i = 0;
while (i < number)
{
if (point[i].y < SIZE && point[i].x < SIZE && point[i].x >= 0 && point[i].y >= 0) {
minefield[point[i].y][point[i].x] = HIDDEN_MINE;}
i++;
}
}
void print_minefield(int minefield[][SIZE])
{
int i = 0;
while (i < SIZE)
{
int j = 0;
while (j < SIZE)
{
printf("%d ", minefield[i][j]);
j++;
}
i++;
printf("\n");
}
}
int main(void)
{
int minefield[SIZE][SIZE];
int number;
printf("Welcome to minesweeper!\n");
printf("How many mines? ");
scanf("%d", &number);
Point point[SIZE];
printf("Enter pairs:\n");
int i = 0;
while (number > i)
{
scanf("%d%d", &point[i].y, &point[i].x);
i++;
}
init_field(minefield, point, number);
printf("Game Started\n");
print_minefield(minefield);
int a,b,c,d;
while (scanf("%d%d%d%d\n",&a,&b,&c,&d) != EOF || scanf("%d%d%d\n",&a,&b,&c) != EOF) {
int e = 0;
int f = 0;
while (f < number) {
if (point[f].y == b && point[f].x <= c+d-1 && point[f].x >= c) {
e++;
}
f++;
}
if (a == 1 && e > 0){
printf("There are %d mine(s) in row %d, from column %d to %d\n",e,b,c,c+d-1);
}
if (a == 2 && e > 0){
first_is_2(b,c,d,point,number);
}
if (a == 3){
first_is_3(b,c,point,i,minefield);
}
if (a == 1 && e == 0) {
printf("Coordinates not on map\n");
}
print_minefield(minefield);
}
return 0;
}
void first_is_2(int b,int c,int d,Point point[],int number) {
int g = 0;
int e_2 = 0;
while (g < number) {
if (b+d >= point[g].y && b-d <= point[g].y && c+d >= point[g].x && c-d <= point[g].x){
e_2++;
}
g++;
}
printf("There are %d mine(s) in the square centered at row %d, column %d of size %d\n",e_2,b,c,d);
}
void first_is_3(int b,int c,Point point[],int i,int minefield[][SIZE]){
if (b == point[i].y && c == point[i].x) {
printf("Game over\n");
system("pause");
} else {
minefield[point[b].y][point[c].x] = VISIBLE_SAFE;
}
if (b+1 != point[i].y && c != point[i].x && b+1 <= SIZE) {
minefield[point[b+1].y][point[c].x] = VISIBLE_SAFE;
}
if (b-1 != point[i].y && c != point[i].x && b-1 >= 0) {
minefield[point[b-1].y][point[c].x] = VISIBLE_SAFE;
}
if (b != point[i].y && c+1 != point[i].x && c+1 <= SIZE) {
minefield[point[b].y][point[c+1].x] = VISIBLE_SAFE;
}
if (b != point[i].y && c-1 != point[i].x && c-1 >= 0) {
minefield[point[b].y][point[c-1].x] = VISIBLE_SAFE;
}
}
主要是first_is_3这个有问题。
运行起来应该是2周围的一圈都不会显示0的。
能不能帮我改改