I have a created a grid like this (some gaps has an obstacle, but it is not important for the question):
private function newGrid()
{
$grid = array();
for ($i = 0; $i < 10; $i++) {
for ($j = 0 ; $j < 10; $j++) {
$grid[$i][$j] = ['obstacle' => rand(0,1)];
}
}
return $grid;
}
So, the initial location is an array:
$loc = array('x' => 1, 'y' => 3);
For example, if i'm moving to x direction, I want wrapping from one edge of the grid to another(like an sphere). (Forward and backward)
When i'm going forward I'm using the modulus like this:
$loc['x'] = ($loc['x'] + 1) % 10 ;
But if I want to do the same, but going backwards, which is the better way to do that? when x gets to 0, go to position x = 9
Any suggestion?
array_reverse()? The wording in your question is kind of confusing to mne