kidshenlong
kidshenlong

Reputation: 551

More efficient than an if statement?

I'm making a game in PHP (don't ask lol), and the player has a location which is an integer. There's a travel page and this basically shows a 5x5 tiled map. Each tile is a different part of the player's universe. By clicking on it he can travel there. Just to give you an idea of the integers behind the map:

Let's say the player starts at 33(the middle) and I wanted to charge him different rates depending on how far he traveled. So, for example, 1 tile in any direction is a 100 credits, 2 tiles is 200 and so on.

So what I came up with is this. $ol represents the player's current location and $nl is where they are travelling to...

if($ol-11==$nl || $ol-10==$nl || $ol-9==$nl || $ol+1==$nl || $ol+11==$nl || $ol+10==$nl || $ol+9==$nl || $ol-1==$nl || $ol-11==$nl ){
echo "cost 100 credits!";
}
else if($ol-22==$nl || $ol-21==$nl || $ol-20==$nl || $ol-19==$nl || $ol-18==$nl || $ol-8==$nl || $ol+2==$nl || $ol+12==$nl || $ol+22==$nl 

|| $ol+21==$nl || $ol+20==$nl || $ol+19==$nl || $ol+18==$nl || $ol+8==$nl || $ol-2==$nl || $ol-12==$nl ){
echo "cost 200 credits!";
}

That's the code for 1 and 2 tile travel. As you can see it's a lengthy statement.

I basically worked out a pattern for the grid I'd set up. For example, travelling up 1 tile would always be -10 of the current tile.

Before I type out any more ridiculously long if statements, is there a neater or more efficient way to do this?

Upvotes: 3

Views: 186

Answers (4)

Kermit
Kermit

Reputation: 34054

I would probably try an array for coordinates. This will allow you to set the initial coordinates. You can then pass new coordinates to the function which will move the position and calculate the cost.

<?php

$array = array( );

//populate the array with 0's
for( $i = 1; $i <= 5; $i++ ) {
    for( $j = 1; $j <= 5; $j++ ) {
        $array[$i][$j] = 0;
    }
}

//set beginning position
$array[3][3] = 1;

function newPosition( $array, $newX, $newY ) {
    $oldX = 0;
    $oldY = 0;

    //locate current position
    foreach($array as $key=>$subArray) {
        foreach($subArray as $subKey=>$val) {
            if($val === 1) {
                $oldX = $key;
                $oldY = $subKey;
            }
        }
    }

    //delete old position
    $array[$oldX][$oldY] = 0;

    //set new position
    $array[$newX][$newY] = 1;

    //Calculate x and y difference
    $xTravel = abs($oldX - $newX);
    $yTravel = abs($oldY - $newY);

    //Add x and y difference
    $totalTravel = $xTravel + $yTravel;

    //Calculate the cost
    $totalCost = $totalTravel * 100;

    echo "cost $totalCost credits!\n";

    return $array;
}

$array = newPosition( $array, 5, 2 );
$array = newPosition( $array, 1, 5 );
$array = newPosition( $array, 1, 5 );
$array = newPosition( $array, 3, 3 );

Output

cost 300 credits!
cost 700 credits!
cost 0 credits!
cost 400 credits!

See the demo

Upvotes: 1

jeroen
jeroen

Reputation: 91734

I would use a different method: As the first digit defines the row and the second digit the column, I would split the number in these two digits and use these numbers to determine how many rows and how many columns are being travelled.

So for any position:

$row = floor($tile_value / 10);
$column = $tile_value % 10;

With this it is easy to calculate distances.

Edit: A small example to measure absolute distances:

$row_org = floor($tile_org_value / 10);
$column_org = $tile_org_value % 10;

$row_new = floor($tile_new_value / 10);
$column_new = $tile_new_value % 10;

$row_diff = $row_new - $row_org;
$col_diff = $col_new - $col_org;

$distance = sqrt(pow($row_diff, 2) + pow($col_diff, 2));

Upvotes: 2

Captain Payalytic
Captain Payalytic

Reputation: 1071

As in my comment above, you cannot measure distance in units, since not all points can be reached in a straight line through points.

You need to consider these points to be points (x, y coordinates) on a graph. Then you can get the distance between any 2 points using Pythagoras.

For example, if we consider your top row as being the coordinates (1,1) (1,2) and so on, if the person travels from (1,1) to (4,3), the distance travelled is the square root of 3 (4-1) squared plus 2 (3-1) squared, i.e. sqrt(9+4) = sqrt(13)

Upvotes: 1

powtac
powtac

Reputation: 41040

Your code seems legit. You could order the conditions so that the most used ones are first.

Upvotes: -2

Related Questions