The curious incident of a minefield in the afternoon

Hello people of Unity, I am beginner in JS(sorry, UnityScript) coding and Unity, and I have a very weird problem I’d like you to help me figure out.

first, a few comments about the upcoming piece of code-
It is a MineSweeper clone,
when the value of the rows and the columns is 10, and WidthHeight is 50 (since they are the same, I use one variable for both, i like this name better than “size” -.-)
The function doesn’t seem to draw any mines below the 8th row line, than it starts randomly drawing again when it gets somewhere around the 16th row line when I draw it on a 20 by 20 set.

for now, ignore the fact that mines can be drawn upon each-other and some other few minor details-
like the sphere’s position could be saved in a Vector2 and some other shit I’m too tired to optimize before I get the simple logic done.

The code-

function FixedUpdate () {

if(Bool == true)
{

DeployMineField(rows,columns);
Bool=false;

}
}
function DeployMineField(rows,columns)
{
NewPos.x = GameObject.Find("Sphere").transform.position.x ;
NewPos.y = GameObject.Find("Sphere").transform.position.y ;
NewPos.z = GameObject.Find("Sphere").transform.position.z ;
Random.seed = 51449*Random.Range(0,rows*columns);

//for bomb locations Random.Range(0,rows*columns);
for( i =0 ; i<bombs; i++)
{

     BombPlace[i] = new Vector2(0,0);
     BombPlace[i].x =  (Random.Range(0 , columns));
     
	 NewPos.x  =GameObject.Find("Sphere").transform.position.x + WidthHeight * (BombPlace[i].x);
	 BombPlace[i].y = Random.Range(0 , rows);
	 NewPos.y = GameObject.Find("Sphere").transform.position.y +WidthHeight * (BombPlace[i].y);
	Instantiate(Bomb,NewPos,Quaternion.identity);
	
	 
}
for(c=0; c<rows; c++)
{

for(i=0; i<(columns); i++)
{
IsItHotInFrance = false;
for(k=0;k<bombs; k++)
{
 if(c==(BombPlace[k].y)  i==(BombPlace[k].x))
 {IsItHotInFrance = true;}
}

if(IsItHotInFrance == false)
{
NewPos.x =GameObject.Find("Sphere").transform.position.x + (WidthHeight*i);
NewPos.y = GameObject.Find("Sphere").transform.position.y + (WidthHeight*c);
//SetUp the tile's numeral representation
iterator = 0;
for(k=0;k<bombs; k++)
{
// + + 
if((BombPlace[k].x==(((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight+1))(BombPlace[k].y==(NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight+1)))
{iterator++;}
// - -
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight-1)(BombPlace[k].y==(NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight-1)))
{iterator++;}
// - +
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight-1)(BombPlace[k].y==(NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight+1)))
{iterator++;}
// + -
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight)+1)(BombPlace[k].y==(NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight-1))
{iterator++;}
//   +
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight)(BombPlace[k].y==(NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight+1)))
{iterator++;}
// +
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight+1)(BombPlace[k].y==((NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight))))
{iterator++;}
//    -
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight)(BombPlace[k].y==(NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight-1)))
{iterator++;}
// -
if((BombPlace[k].x==((NewPos.x-GameObject.Find("Sphere").transform.position.x)/WidthHeight-1)(BombPlace[k].y==((NewPos.y-GameObject.Find("Sphere").transform.position.y)/WidthHeight))))
{iterator++;}

}
switch(iterator)
{
 case 0:
 Roll =Blank;
 break;
 case 1:
 Roll = Cube1;
 break;
 case 2: 
 Roll = Cube2;
 break;
 case 3:
 Roll = Cube3;
 break;
  case 4:
 Roll = Cube4;
 break;
 case 5:
 Roll = Cube5;
 break;
 case 6:
 Roll = Cube6;
 break;
 case 7:
 Roll = Cube7;
 break;
 case 8:
 Roll = Cube8;
 break;



}
Instantiate(Roll,NewPos,Quaternion.identity);
}
}
}


//Iterate through all the tiles to create the minefield



}

note: full code, including variable declaration, can be found here http://pastebin.com/RTa6UVeM

The non-drawn parts seem to have something to do with the size of each block, when I decrease it to 20, it goes to line 12-ish than stops,
(so it is probably somewhere in the math).

Thanks you in advance,
Adar G. :smile:

Hi, welcome to the forum!

Is the algorithm intended to place the mines at totally random locations or to cluster them together (or do something else with them)? I’m not sure exactly why it is missing some of the positions, but there’s an easier way to choose random locations if that’s what you need. If you number the grid squares from 0 to width * height - 1 (eg, 0 to 99 for a 10x10 grid) you can identify a square by a single numeric index. You can use the random picking/shuffling library in this thread to produce a list of random numbers in a given range (there’s a function called SampleRemove which will not pick the same number twice). Then, you can convert the numeric index to its grid position by using:-

row = Mathf.FloorToInt(index / gridWidth);
col = index % gridWidth;

I’m trying to randomly get 'em in a grid position (i.e. x=3 y=4 is similar to column 3 in row 4), Might use the randomness thing, sounds good.
and I think my problem is better shown than told, but I’m having some issues while uploading a .zip file containing the scene :S
getting a “tried to load an empty file error”, and the file isn’t empty, I triple-checked.

You should be able to attach an image file and see it directly in the post. Alternatively, perhaps you can upload the file somewhere and post a link.

this is how the problem looks, notice it not drawing below the 8th row for some reason, and these are the
scene and the assets in a .zip file →

well, it works perfectly when I split it to different for() loops, so I think I’ll get my way around that problem.
Anyway, thanks :smile: