Hello
I am trying to create emulation of "Mine game"
suppose we have matrix
0 0 0 0
0 0 -1 0
0 0 0 -1
0 -1 0 -1
now each cell that not equal -1 should be represent numbers of mines
here code
for(int i=0;i
for(int j=0;j<N;j++)
if(box[i][j]!=-1)
{
switch (i)
{
case 0:
iLEFT=0;
iRIGHT=1;break;
case 3:
iRIGHT=0;
iLEFT=1;
break;
case 1:
iLEFT=1;
iRIGHT=1;
break;
case 2:
iLEFT=1;
iRIGHT=1;
break;
default:
iLEFT=1;
iRIGHT=1;
break;
}
switch (j)
{
case 0:
jLEFT=0;`
jRIGHT=1;
break;
case 3:
jRIGHT=0;
jLEFT=1;
break;
case 1:
jLEFT=1;
jRIGHT=1;
break;
case 2:
jLEFT=1;
jRIGHT=1;
break;
default:
jLEFT=1;
jRIGHT=1;
break;
}
// checking neighbor
if(box[i][j+jRIGHT]==-1)
count+=1;
if(box[i][j-jLEFT]==-1)
count+=1;
if(box[i+iRIGHT][j]==-1)
count+=1;
if (box[i-iLEFT][j]==-1)
count+=1;
if(box[i-iLEFT][j-jLEFT]==-1)
{
if(i-iLEFT!=i) // trying to avoid double checking
count+=1;
}
if(box[i+iRIGHT][j-jLEFT]==-1)
{
if(i+iRIGHT!=i) //trying to avoid double checking
count+=1;
}
if (box[i-iLEFT][j+jRIGHT]==-1)
{
if(i!=iLEFT) //trying to avoid double checking
count+=1;
}
if (box[i+iRIGHT][j+jRIGHT]==-1)
{
if(i!=iRIGHT) //trying to avoid double checking
count+=1;
}
box[i][j]=count;
count=0;
}
My algorithm
iLEFT present row step left.
iRIGHT present row step right.
jLEFT and JRIGHT same for column
Suppose i=0 so we can step only one step right(down)
if i=1 we can step up and done ..same for j
"Case statement " update iLEFT/iRIGTH and jLEFT/jRIGHT for for enable side steps
now "if" statement checking left/right up/done 2 diagonals for box[i][j](only one step always)
count counting performance of -1 values neighbor for box[i][j]
you can see I still have double checking for same cells
0 1 1 1
0 1 -1 2
1 2 4 -1
2 -1 4 -1