mercredi 14 août 2019

Why is my if condition skipping characters?

I have made a game flames, where example given a name is Patrick and the other name is Abcdefg, the loop will crash out one character from one name thatis the same with a character in the other name. When my condition is if srtlen($name1)==strlen($name2), why is my loop not crashing out letter a in the name Patrick and abcdefg? It only crashes out c. My problem is in if($r==$e) part. The desired outcome from the name Patrick and abcdefg is status=10; because A and C are crashed out.

      <html>
    <head>
    <title>Flames</title>
    </head>
    <body>
    <center><form method="post" style="margin-top:60px;">
    <h2>Flames</h2>
    <input type="text" name="name1" placeholder="First name"/><br>
    <input type="text" name="name2" placeholder="Second name"/><br><br>
    <input type="submit" name="submit" value="submit"/>
    </form>
    <?php
    if(isset($_POST['submit'])){
        $name1=$_POST['name1'];
        $name2=$_POST['name2'];

//this is if the names have space on it.
        if(strstr($name1,' ')&&strstr($name2,' ')||strstr($name1,' ')||strstr($name2,' ')){
        $exploded1=explode(' ',$name1);
        $exploded2=explode(' ',$name2);
        $joined1=implode("",$exploded1);
        $joined2=implode("",$exploded2);
        $e=strlen($joined2);
        $r=strlen($joined1);
        }
        else{
        $r=strlen($name1);  
        $e=strlen($name2);
        }
        $counter=0;   
        $same=0;
        if($r>$e){
               for($m=0; $m<=$e-1; $m++){
                for($i=0; $i<=$r-1; $i++){
                    if($counter<$e){
                    if($joined1[$i]==$joined2[$m]){
                        $same++;
                        //$counter++;
                        $joined1[$i]=' ';
                        break;
                    }
                    }
                } 
                }
        }
        elseif($e>$r){
            for($m=0; $m<=$r-1; $m++){
                for($i=0; $i<=$e-1; $i++){
                    if($counter<$r){  
                    if($joined2[$i]==$joined1[$m]){
                        $same++;
                        //$counter++;
                        $joined2[$i]=' ';
                        break; 
                    }
                    }
                }
            }
        }   

    //this is where it did not check the a character which is most likely to be crashed out because it has a pair 
    if($r==$e){
            for($m=0; $m<$r; $m++){
            for($q=0; $q<$r; $q++){         
              if($name1[$q]==$name2[$m]){
                  echo $name1[$q].'<br>';
                  $same++;
                  //$counter++;
                  $name1[$q]=' ';
                  break;
                 }
            }
            }

        }


        $sum=$e+$r;
        $mult=$same*2;
        $status=$sum-$mult;
        echo $joined1.'<br>';
        echo $joined2.'<br>';
        echo 'r'.$r; echo '<br>';
        echo 'e'.$e; echo '<br>';
        echo 'status'.$status; echo '<br>';
        echo 'sum'.$sum;echo '<br>';
        echo 'mult'.$mult; echo '<br>';
        echo "$joined1 <br>";
        echo "$joined2 <br>";

//this is to determine the flames status.
    if($status==1||$status%6==1){
    echo 'Friends'; 
    }

    if($status==2||$status%6==2){
    echo 'Lovers';  
    }

    if($status==3||$status%6==3){
    echo 'Anger';   
    }

    if($status==4||$status%6==4){
    echo 'Marriage';    
    }

    if($status==5||$status%6==5){
    echo 'Enemy';   
    }

    if($status==6||$status%6==0){
    echo 'Soulmates';   
    } 
    }
    ?>
    </center>
    </body>
    </html>

Aucun commentaire:

Enregistrer un commentaire