Home > Software engineering >  Php array to pow numbers revers function problem
Php array to pow numbers revers function problem

Time:03-20

<style>
.red{color:red;}
.green{color:green;}
</style>
<?php
function array2aiflag($inputString){
    $arAIFlag = ["AGGR","NOMOVE","COWARD","NOATTSHINSU","NOATTCHUNJO","NOATTJINNO","ATTMOB","BERSERK","STONESKIN","GODSPEED","DEATHBLOW","REVIVE"];
    $retValue = 0;
    $arInputString = explode(",",$inputString);
    for($i=0;$i<sizeof($arAIFlag);$i  ) {
        $tempString = $arAIFlag[$i];
        for ($j=0;$j<30;$j  ){
            if(isset($arInputString[$j])){
                    $tempString2 = $arInputString[$j];
            }else{
                $tempString2 = $inputString;
            }
            if(strcmp($tempString2,$tempString) == 0){
                $retValue =pow(2,$i);
            }           
            if(strcmp($tempString2,"") == 0){
                break;
            }
        }
    }
    unset($arInputString);
    return $retValue;
}
function aiflag2array($flag,$target=[],$str=""){
    $tar=["AGGR","NOMOVE","COWARD","NOATTSHINSU","NOATTCHUNJO","NOATTJINNO","ATTMOB","BERSERK","STONESKIN","GODSPEED","DEATHBLOW","REVIVE"];
    for($x=0;$x<count($tar);$x  ){
        $target[pow(2,$x)]=$tar[$x];
    }   
    for($i=count($tar)-1;$i>-1;$i--){
        $pow = pow(2, $i);
        if($pow < $flag){
            $str = ",".$target[$pow].$str;
            $flag = $flag-$pow;
        }elseif($pow == $flag){
            return "".$target[$pow].$str;
        }
    }
    return "NONE";
}


$sample=["AGGR","AGGR,BERSERK","AGGR,BERSERK,DEATHBLOW,REVIVE","AGGR,BERSERK,STONESKIN","AGGR,BERSERK,STONESKIN,DEATHBLOW","AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE","AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE","AGGR,DEATHBLOW","AGGR,GODSPEED","AGGR,GODSPEED","AGGR,NOATTCHUNJO","AGGR,NOATTJINNO","AGGR,NOATTSHINSU","AGGR,NOMOVE","AGGR,REVIVE","AGGR,STONESKIN","BERSERK","BERSERK,STONESKIN","COWARD","NOMOVE","AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW","NOMOVE,STONESKIN","NOMOVE,STONESKIN","REVIVE"];
foreach($sample as $k=>$v){
    $no1=array2aiflag($v);
    $no2=array2aiflag(aiflag2array($no1));
    if($v != aiflag2array($no1)){
        echo '<a > OriginalData - <b>'.$v.' ['.$no1.']</b> - ReversData - <b>'.aiflag2array($no1).' ['.$no2.']</b><br>';
    }else{      
        echo '<a > OriginalData - <b>'.$v.' ['.$no1.']</b> - ReversData - <b>'.aiflag2array($no1).' ['.$no2.']</b><br>';
    }
}
?>

When I run the code above, I convert 24 sample data into a folded number and print it. Then when I want to call the numbers from the database and convert them back to strings, some of them seem wrong.

Result:

OriginalData - AGGR [30] - ReversData - NOMOVE,COWARD,NOATTSHINSU,NOATTCHUNJO [30]
OriginalData - AGGR,BERSERK [129] - ReversData - AGGR,BERSERK [129]
OriginalData - AGGR,BERSERK,DEATHBLOW,REVIVE [3201] - ReversData - AGGR,BERSERK,DEATHBLOW,REVIVE [3201]
OriginalData - AGGR,BERSERK,STONESKIN [385] - ReversData - AGGR,BERSERK,STONESKIN [385]
OriginalData - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409] - ReversData - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409]
OriginalData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - ReversData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
OriginalData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - ReversData - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
OriginalData - AGGR,DEATHBLOW [1025] - ReversData - AGGR,DEATHBLOW [1025]
OriginalData - AGGR,GODSPEED [513] - ReversData - AGGR,GODSPEED [513]
OriginalData - AGGR,GODSPEED [513] - ReversData - AGGR,GODSPEED [513]
OriginalData - AGGR,NOATTCHUNJO [17] - ReversData - AGGR,NOATTCHUNJO [17]
OriginalData - AGGR,NOATTJINNO [33] - ReversData - AGGR,NOATTJINNO [33]
OriginalData - AGGR,NOATTSHINSU [9] - ReversData - AGGR,NOATTSHINSU [9]
OriginalData - AGGR,NOMOVE [3] - ReversData - AGGR,NOMOVE [3]
OriginalData - AGGR,REVIVE [2049] - ReversData - AGGR,REVIVE [2049]
OriginalData - AGGR,STONESKIN [257] - ReversData - AGGR,STONESKIN [257]
OriginalData - BERSERK [3840] - ReversData - STONESKIN,GODSPEED,DEATHBLOW,REVIVE [3840]
OriginalData - BERSERK,STONESKIN [384] - ReversData - BERSERK,STONESKIN [384]
OriginalData - COWARD [120] - ReversData - NOATTSHINSU,NOATTCHUNJO,NOATTJINNO,ATTMOB [120]
OriginalData - NOMOVE [60] - ReversData - COWARD,NOATTSHINSU,NOATTCHUNJO,NOATTJINNO [60]
OriginalData - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667] - ReversData - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667]
OriginalData - NOMOVE,STONESKIN [258] - ReversData - NOMOVE,STONESKIN [258]
OriginalData - NOMOVE,STONESKIN [258] - ReversData - NOMOVE,STONESKIN [258]
OriginalData - REVIVE [61440] - ReversData - NONE [0]

Reverse engineering is required. There is a problem with the number 30 in the aiflag2array function.

The array2aiflag function should not be modified. aiflag2array needs to be edited.

I would appreciate it if you could help me fix it. thanks.

CodePudding user response:

OK, I thought this was a nice thing to work on. I must say that I miss the context, I have no idea what this will be used for. Context is important for understanding the code. I assume it is some kind of game?

The first thing to do is to get the array definitions out of the way:

$states = ["AGGR",
           "NOMOVE",
           "COWARD",
           "NOATTSHINSU",
            "NOATTCHUNJO",
           "NOATTJINNO",
           "ATTMOB",
           "BERSERK",
           "STONESKIN",
           "GODSPEED",
           "DEATHBLOW",
           "REVIVE"];

$samples = ["AGGR",
            "AGGR,BERSERK",
            "AGGR,BERSERK,DEATHBLOW,REVIVE",
            "AGGR,BERSERK,STONESKIN",
            "AGGR,BERSERK,STONESKIN,DEATHBLOW",
            "AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE",
            "AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE",
            "AGGR,DEATHBLOW",
            "AGGR,GODSPEED",
            "AGGR,GODSPEED",
            "AGGR,NOATTCHUNJO",
            "AGGR,NOATTJINNO",
            "AGGR,NOATTSHINSU",
            "AGGR,NOMOVE",
            "AGGR,REVIVE",
            "AGGR,STONESKIN",
            "BERSERK",
            "BERSERK,STONESKIN",
            "COWARD",
            "NOMOVE",
            "AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW",
            "NOMOVE,STONESKIN",
            "NOMOVE,STONESKIN",
            "REVIVE"];

I used $states and $samples. You can change these names, of course. What is important is that you don't make your lines to long, no longer than 80 - 100 characters.

Next is the code for the conversion from, what I call flags, to states and the other way around:

function states2flags($userStates, $allStates)
{
    $userFlags = 0;
    $bit = 1;
    foreach ($allStates as $state) {
        if (in_array($state, $userStates)) {
           $userFlags  = $bit;
        }
        $bit <<= 1;
    }
    return $userFlags;
}

function flags2states($userFlags, $allStates)
{
    $userStates = [];
    $bit = 1;
    foreach ($allStates as $state) {
        if ($userFlags & $bit) {
           $userStates[] = $state;
        }
        $bit <<= 1;
    }
    return $userStates;
}

The only special thing in here is the Bitwise Assignment Operator. No pow() is needed for working with powers of two.

Every function parameter is an array, this is intentional. It clearly is intended as an array, so why use strings? Alternatively you could choose to only work with strings, but don't mix arrays and strings, it is confusing.

I kept most of your test code, although the names have changed:

foreach($samples as $sample) {
    $sampleStates  = explode(",", $sample);
    $sampleFlags   = states2flags($sampleStates, $states);
    $reverseStates = flags2states($sampleFlags, $states);
    $reverseFlags  = states2flags($reverseStates, $states);
    $textColor = (count(array_diff($sampleStates, $reverseStates)) > 0) ? "red" : "green";
    echo '<a >' . $textColor . ' Original Data - <b>' . $sample .
         ' [' . $sampleFlags . ']</b> - Reverse Data - <b>' .
         implode(",", flags2states($sampleFlags, $states)) . ' ['.$reverseFlags. ']</b><br>';
}

The output is:

green Original Data - AGGR [1] - Reverse Data - AGGR [1]
green Original Data - AGGR,BERSERK [129] - Reverse Data - AGGR,BERSERK [129]
green Original Data - AGGR,BERSERK,DEATHBLOW,REVIVE [3201] - Reverse Data - AGGR,BERSERK,DEATHBLOW,REVIVE [3201]
green Original Data - AGGR,BERSERK,STONESKIN [385] - Reverse Data - AGGR,BERSERK,STONESKIN [385]
green Original Data - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409] - Reverse Data - AGGR,BERSERK,STONESKIN,DEATHBLOW [1409]
green Original Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - Reverse Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
green Original Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457] - Reverse Data - AGGR,BERSERK,STONESKIN,DEATHBLOW,REVIVE [3457]
green Original Data - AGGR,DEATHBLOW [1025] - Reverse Data - AGGR,DEATHBLOW [1025]
green Original Data - AGGR,GODSPEED [513] - Reverse Data - AGGR,GODSPEED [513]
green Original Data - AGGR,GODSPEED [513] - Reverse Data - AGGR,GODSPEED [513]
green Original Data - AGGR,NOATTCHUNJO [17] - Reverse Data - AGGR,NOATTCHUNJO [17]
green Original Data - AGGR,NOATTJINNO [33] - Reverse Data - AGGR,NOATTJINNO [33]
green Original Data - AGGR,NOATTSHINSU [9] - Reverse Data - AGGR,NOATTSHINSU [9]
green Original Data - AGGR,NOMOVE [3] - Reverse Data - AGGR,NOMOVE [3]
green Original Data - AGGR,REVIVE [2049] - Reverse Data - AGGR,REVIVE [2049]
green Original Data - AGGR,STONESKIN [257] - Reverse Data - AGGR,STONESKIN [257]
green Original Data - BERSERK [128] - Reverse Data - BERSERK [128]
green Original Data - BERSERK,STONESKIN [384] - Reverse Data - BERSERK,STONESKIN [384]
green Original Data - COWARD [4] - Reverse Data - COWARD [4]
green Original Data - NOMOVE [2] - Reverse Data - NOMOVE [2]
green Original Data - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667] - Reverse Data - AGGR,NOMOVE,BERSERK,GODSPEED,DEATHBLOW [1667]
green Original Data - NOMOVE,STONESKIN [258] - Reverse Data - NOMOVE,STONESKIN [258]
green Original Data - NOMOVE,STONESKIN [258] - Reverse Data - NOMOVE,STONESKIN [258]
green Original Data - REVIVE [2048] - Reverse Data - REVIVE [2048]
  • Related