How can you assign the x-axis and y-axis to an array in PHP to determine the positions of ships?
To assign the x-axis and y-axis positions to an array in PHP to determine the positions of ships, you can create a multidimensional array where each element represents a coordinate on the grid. You can then populate the array with the positions of the ships by assigning specific values to the corresponding coordinates. This allows you to easily track the positions of the ships on the grid and check for collisions.
// Define the grid size
$gridSize = 10;
// Initialize the grid with all positions set to 0
$grid = array_fill(0, $gridSize, array_fill(0, $gridSize, 0));
// Assign positions of ships on the grid
$ship1X = 3;
$ship1Y = 4;
$grid[$ship1X][$ship1Y] = 1; // Ship 1
$ship2X = 7;
$ship2Y = 2;
$grid[$ship2X][$ship2Y] = 1; // Ship 2
// Print the grid to visualize ship positions
for ($i = 0; $i < $gridSize; $i++) {
for ($j = 0; $j < $gridSize; $j++) {
echo $grid[$i][$j] . " ";
}
echo "\n";
}