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);
                             }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.