【发布时间】:2016-10-14 18:46:15
【问题描述】:
我正在尝试打印扫雷板。
目前,它只会打印 0 和炸弹位置,而不是相邻炸弹的数量。我认为这与我的牙套的位置有关,但我不确定。
#include <stdio.h>
#include <stdlib.h>
int main() {
int width, tall, W;
int T=1;
int g;
int c=0;
printf("How wide do you want your board?\n Max 32 squares\n");
scanf("%d",&width);
printf("How tall do you want your board?\n Max 32 squares\n");
scanf("%d",&tall);
printf("How many mines do you want?\n Between 0 and 1000 please\n");
scanf("%d",&W);
int y= tall*width;
if (width>32 || width <=0 || tall>32 || tall <=0)
{
printf("Error, not valid dimensions\n");
T=0;
}
if (W>1000 || W<0 || W>(tall*width))
{
printf("Error, not a valid amount of mines\n");
T=0;
}
if (T==0)
{
printf("Sorry, you cannot play Mine sweeper!\n");
}
char array[tall][width];
int i,j;
if (T==1)
{
for(i=0;i<tall;i++)
{
for(j=0;j<width;j++)
{
g=rand() % y;
if (g<=W)
{
array[i][j]='*';
}
if(g>W)
{
array[i][j]='0';
}
}
}
for(i=0;i<tall;i++)
{
for(j=0;j<width;j++)
{
if(array[i][j]==0)
{
if(array[i-1][j-1]=='*')
{
c=c+1;
}
if(array[i][j-1]=='*')
{
c=c+1;
}
if (array[i+1][j-1]=='*')
{
c=c+1;
}
if (array[i-1][j]=='*')
{
c=c+1;
}
if (array[i+1][j]=='*')
{
c=c+1;
}
if (array[i-1][j+1]=='*')
{
c=c+1;
}
if (array[i][j+1]=='*')
{
c=c+1;
}
if (array[i+1][j+1]=='*')
{
c=c+1;
}
switch(c)
{
case 0:
{
array[i][j]=' ';
break;
}
case 1:
{
array[i][j]='1';
break;
}
case 2:
{
array[i][j]='2';
break;
}
case 3:
{
array[i][j]='3';
break;
}
case 4:
{
array[i][j]='4';
break;
}
case 5:
{
array[i][j]='5';
break;
}
case 6:
{
array[i][j]='6';
break;
}
case 7:
{
array[i][j]='7';
break;
}
case 8:
{
array[i][j]='8';
break;
}
}
}
}
}
for(i=0;i<tall;i++)
{
for(j=0;j<width;j++)
{
printf("%c",array[i][j]);
}
printf("\n");
}
}
return 0;
}
【问题讨论】:
-
您在数组范围之外访问。当
i = 0时,您不应该访问array[i-1]。当j = 0时,你不应该访问array[...][j-1]。当您处于其他极端时,i+1和j+1也存在类似的问题。