joecooper Posted September 5, 2011 Share Posted September 5, 2011 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 https://forums.phpfreaks.com/topic/246502-random-number-if-on-the-edge-of-grid-generate-another/ Share on other sites More sharing options...
joecooper Posted September 5, 2011 Author Share Posted September 5, 2011 done the following to check if the boat has wrapped, if (($orientation == 2) && ($i > 0) && ($nextcell % 32 == 1)) { // ACROSS BOAT IS WRAPPING! Link to comment https://forums.phpfreaks.com/topic/246502-random-number-if-on-the-edge-of-grid-generate-another/#findComment-1265779 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.