Jump to content

random number, if on the edge of grid, generate another?


joecooper

Recommended Posts

This might be a bit hard to explain!

 

I have a grid, 32x32 which is actually 1024 mySQL rows in a table, you can see the table here: http://interbitlotto.com/grid

each cell is a row in the database, top left is ID 1, bottom right is id 1024.

 

get it?

 

right well its a battle ship game, and im stuck on the automatic battle ship placement script. I have displayed the generated boats as misses so we can see them.

 

The problem i am having, is that the random number generater chooses a cell too close to the side so the boat ends up wrapping to the other side. I cant think of a way to detect if its going to wrap, if it is then choose another cell...

 

The code below chooses the orientation of the boat, then chooses a random cell... but as you can see, no code for "across" to stop it choosing one too close to the side.

 

Any ideas?

 

                             $orientation = rand(1,2); //get random orientation  - 1 = down, 2 = across.

                             if ($orientation == 1){
                                $multiply = 32;  //cells till next row
                                $max = 1024 - (32 * $boatlength); //prevents it from choosing a cell too close to the bottom 
                                $startcell = rand(1,$max);
                             }else{

                                $multiply = 1;  //across, so next cell
                                $max = 1024;
                                $startcell = rand(1,$max);
                             }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.