Question

I am writing a cost path script for use on hexagonal maps. Currently I am in the "learning how to walk" phase of development.

In general terms the code works. I can reliably get from A to B. However, consider the following map:

enter image description here

Both the sequence 0406 -> 0405 -> 0505 and 0406 -> 0506 -> 0505 are valid. I would like to traverse and output BOTH paths.

My code follows:

public function walkMap($origCol, $origRow, $destCol, $destRow) {
    $curRow = $origRow;
    $curCol = $origCol;
    while (($curRow != $destRow) || ($curCol != $destCol)) {
        $newRow = self::getNextMove($curRow,$destRow);
        if ($newRow == $curRow) {
            $curCol = self::getNextMove($curCol,$destCol);
        } else {
            $curRow = $newRow;
        }
    }
}

private function getNextMove($cur,$dest) {
    if ($cur < $dest) {
        ++$cur;
    } else if ($cur > $dest) {
        --$cur;
    }
    return $cur;
}

My desired result is a numeric array of step => hexCoord showing the path taken. But I'm not sure how to adopt the above working code to intelligently branch, and having done that, how best to shape the output data structure...

Thanks in advance.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top