Jump to content

Coordinate Grid Mapping (Battleship)


Go to solution Solved by requinix,

Recommended Posts

Hello All!

I am struggling to build a script that is generating random images. I want to place a bunch of circles on a rectangular image. However I want those circles to appear in random locations.

I have been using the range($min,$max) function to keep track of "used" spaces on the X axis, based on the circle size, but I was wondering if there is a class somewhere that could simplify this.

Ideally, something that takes a given width and height, and breaks that into a group of "cells". 

IDK if any of you have played the game battle ship, but something like that. Id like to be able to do placeCircle("A1"); 

 

Does anyone have any suggestions? 

 

Thanks for reading!

JF

Link to comment
https://forums.phpfreaks.com/topic/314550-coordinate-grid-mapping-battleship/
Share on other sites

  • Solution

I'm not really sure what the complexity is here, but I'm also not sure quite what it is you're trying to get.

So you have an image with pixel dimensions WxH. You want to place circles on there, but I guess you don't want to place them on individual pixels but rather on grid "cells" to reduce the number of locations. So like a 100x100 image could be split into 10x5 cells (each is 10x20 pixels), reducing the number of locations from 100*100=10000 center points to 10*5=50 center points.

image.png image.png

Easy answer? Generate the set of cells you want to choose from, shuffle the set, then go through the first <?> of them and draw the circle appropriately.

$cells = [];
for ($x = 1; $x <= CELL_COUNT_X; $x++) {
	for ($y = 1; $y <= CELL_COUNT_Y; $y++) {
		$cells[] = [$x, $y];
	}
}

shuffle($cells);

for ($i = 0; $i < NUMBER_OF_CIRCLES; $i++) {
	list($x, $y) = $cells[$i];
	// draw circle on the cell at ($x,$y)
}

 

Thanks for your reply. This is kinda what I came up with so far. It generates a pixel grid for X & Y coordinates. 

IDK maybe someone someday will find this useful? I figured I would share. it works for what I needed. 

 

GRID FUNCTION

<?php
public static function battleShipGrid($w, $h, $size)
{
  $totX = intval(floor($w / $size));
  $totY = intval(floor($h / $size));

  $xRanges = array();
  $yRanges = array();

  $xLimit = 0;
  while ($xLimit <= $totX) {
     $start = intval($xLimit * $size);
     $end = intval(($start + $size) - 1);

     if ($end > $w)
       $end = $w;

     $xRanges[] = range($start, $end);

     $xLimit++;
  }

  $yLimit = 0;
  while ($yLimit <= $totY) {
     $start = intval($yLimit * $size);
     $end = intval(($start + $size) - 1);

     if ($end > $h)
       $end = $h;

     $yRanges[] = range($start, $end);

     $yLimit++;
  }

  return array("x" => $xRanges, "y" => $yRanges, "xTotal" => $totX, "yTotal" => $totY);
}

 

SEARCH

public static function searchPoint($xTest, $yTest, $xRanges, $yRanges)
{
  $xResult = self::searchSquare($xTest, $xRanges);
  $yResult = self::searchSquare($yTest, $yRanges);

  return array("x" => $xResult, "y" => $yResult);
}

public static function searchSquare($needle, $haystack)
{
  $found = false;
  $current = 0;

  while ($found === false) {
    $x = $haystack[$current];
    if (in_array($needle, $x))
      $found = $current;

    $current++;
    if ($current > count($haystack))
      $found = "not_found";
  }

  if ($found !== "not_found")
    $result = $haystack[$found];
  else
    return false;

  $min = $result[0];
  $max = end($result);

  return array("key" => $found, "min" => $min, "max" => $max, "items" => $result);
}

 

USAGE EXAMPLE

<?php
// Getting example coordinates like this
$examplePoint = "1910,1070";

$points = explode(",", $examplePoint);
$xTest = intval($points[0]);
$yTest = intval($points[1]);

// We can find out which X & Y Square they can be found in.
$xResult = self::searchSquare($xTest, $xRanges);
$yResult = self::searchSquare($yTest, $yRanges);

?>

 

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.