I'm trying to make a puzzle solver with php code like this

<?php  
    $GoalState = array(0,0,0,0,0,0,0,0,0);
    $CurrentState = array(0,0,0,0,0,0,0,0,0);
    $ChildState = array(0,0,0,0,0,0,0,0,0);
    function GetGoalState($initialstate, $goalstate){
        $total = 0;
        $GoalStateGanjil = array(1,2,3,8,0,4,7,6,5);
        $GoalStateGenap = array(0,1,2,3,4,5,6,7,8);
        for($i=0; $i<8; $i++){
            for($j=$i+1; $j<9; $j++){
                if($initialstate[$j] < $initialstate[$i] && $initialstate[$j] != 0)
                    $total = $total + 1;    
            }
        }
        if($total % 2 == 1){
            for($i=0; $i<9; $i++){
                $goalstate[$i] = $GoalStateGanjil[$i];
            }
        }else{
            for($i=0; $i<9; $i++){
                $goalstate[$i] = $GoalStateGenap[$i];
            }
        }
    }
    function PrintState($state){
        for($i=0; $i<9; $i++){
            if($i % 3 == 0)
                echo "<br>";
            echo " ".$state[$i];
        }
        echo "<br>";
    }
    function CopyState($from, $to){
        for($i=0; $i<9; $i++){
            $to[$i] = $from[$i];
        }
    }
    function SameState($currentstate, $goalstate){
        for($i=0; $i<9; $i++){
            if($currentstate[$i] != $goalstate[$i]){
                return false;
                break;
            }
        }
        return true;
    }
    function GetKotakKosong($state){
        $index = 0;
        for($i=0; $i<9; $i++){
            if($state[$i] == 0)
                $index = $i;
        }
        return $index;
    }
    function MoveUp($state){
        $index = GetKotakKosong(array(2,8,3,1,6,4,7,5,0));
        if($index > 2){     
            $state[$index] = $state[$index-3];
            $state[$index-3] = $state[$index-3] - $state[$index];
        }
    }
    function MoveDown($state){
        $index = GetKotakKosong(array(2,8,3,1,6,4,7,5,0));
        if($index < 6){
            $state[$index] = $state[$index+3];
            $state[$index+3] = $state[$index+3] - $state[$index];
        }
    }
    function MoveLeft($state){
        $index = GetKotakKosong(array(2,8,3,1,6,4,7,5,0));
        if($index % 3 > 0){
            $state[$index] = $state[$index-1];
            $state[$index-1] = $state[$index-1] - $state[$index];
        }
    }
    function MoveRight($state){
        $index = GetKotakKosong(array(2,8,3,1,6,4,7,5,0));
        if($index % 3 < 2){
            $state[$index] = $state[$index+1];
            $state[$index+1] = $state[$index+1] - $state[$index];
        }
    }
    function MatchTile($currentstate, $goalstate){
        $match = 0;
        for($i=0; $i<9; $i++){
            if($currentstate[$i] != 0 && $currentstate[$i] == $goalstate[$i])
                $match++;
        }
        return $match;
    }
    function GetTheBestMove($heuristic){
        $index = 0;
        $max = $heuristic[0];
        for($i=0; $i<4; $i++){
            if($heuristic[i] > $max){
                $max = $heuristic[$i];
                $index = $i;
            }
        }
        return $index;
    }
    $InitialState = array(2,8,3,1,6,4,7,5,0);
    GetGoalState($InitialState,$GoalState);
    echo "<br>Init State<br>----------";       
    PrintState($InitialState);
    echo "<br>Goal State<br>----------";
    PrintState($GoalState);
    echo "<br>Searching<br>---------";            
    CopyState($InitialState,$CurrentState);
    $level = 0;
    while(!SameState($CurrentState,$GoalState)){
        PrintState($CurrentState);
        $ChildState = array(2,8,3,1,6,4,7,0,5);
        $Heuristic = array(0,0,0,0);
        $level++;
        MoveUp(array(2,8,3,1,6,4,7,5,0));
        if(!SameState($ChildState,$CurrentState)){
            $Heuristic[0] = MatchTile($ChildState,$GoalState) + $level;
            MoveDown($ChildState);
        }
        MoveDown(array(2,8,3,1,6,4,7,5,0));
        if(!SameState($ChildState,$CurrentState)){
            $Heuristic[1] = MatchTile($ChildState,$GoalState) + $level;
            MoveUp($ChildState);
        }
        MoveLeft(array(2,8,3,1,6,4,7,5,0));
        if(!SameState($ChildState,$CurrentState)){
            $Heuristic[2] = MatchTile($ChildState,$GoalState) + $level;
            MoveRight($ChildState);
        }
        MoveRight(array(2,8,3,1,6,4,7,5,0));
        if(!SameState($ChildState,$CurrentState)){
            $Heuristic[3] = MatchTile($ChildState,$GoalState) + $level;
            MoveLeft($ChildState);
        }
        switch(GetTheBestMove($Heuristic)){
            case 0 : MoveUp($CurrentState); break;
            case 1 : MoveDown($CurrentState); break;
            case 2 : MoveLeft($CurrentState); break;
            case 3 : MoveRight($CurrentState); break;
        }       
    }
    echo "\n Solved with %i steps, %f seconds...".$level;
?>

The code I created from c ++ code like this:

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int GoalState[9], CurrentState[9], ChildState[9] = {0,0,0,0,0,0,0,0,0};
void GetGoalState(int initialstate[], int goalstate[]){
    int i, j, total = 0;
    int GoalStateGanjil[] = {1,2,3,8,0,4,7,6,5};
    int GoalStateGenap[] = {0,1,2,3,4,5,6,7,8};
    for(i=0; i<8; i++){
        for(j=i+1; j<9; j++){
            if(initialstate[j] < initialstate[i] && initialstate[j] != 0)
                total = total + 1;  
        }
    }
    if(total % 2 == 1){
        for(i=0; i<9; i++){
            goalstate[i] = GoalStateGanjil[i];
        }
    }else{
        for(i=0; i<9; i++){
            goalstate[i] = GoalStateGenap[i];
        }
    }
}
void PrintState(int state[]){
    for(int i=0; i<9; i++){
        if(i % 3 == 0)
            cout << endl;
        cout << " " << state[i];
    }
    cout << endl;
}
void CopyState(int from[], int to[]){
    for(int i=0; i<9; i++){
        to[i] = from[i];
    }
}
bool SameState(int currentstate[], int goalstate[]){
    for(int i=0; i<9; i++){
        if(currentstate[i] != goalstate[i]){
            return false;
            break;
        }
    }
    return true;
}
int GetKotakKosong(int state[]){
    int index = 0;
    for(int i=0; i<9; i++){
        if(state[i] == 0)
            index = i;
    }
    return index;
}
void MoveUp(int state[]){
    int index = GetKotakKosong(state);
    if(index > 2){      
        state[index] = state[index-3];
        state[index-3] = state[index-3] - state[index];
    }
}
void MoveDown(int state[]){
    int index = GetKotakKosong(state);
    if(index < 6){
        state[index] = state[index+3];
        state[index+3] = state[index+3] - state[index];
    }
}
void MoveLeft(int state[]){
    int index = GetKotakKosong(state);
    if(index % 3 > 0){
        state[index] = state[index-1];
        state[index-1] = state[index-1] - state[index];
    }
}
void MoveRight(int state[]){
    int index = GetKotakKosong(state);
    if(index % 3 < 2){
        state[index] = state[index+1];
        state[index+1] = state[index+1] - state[index];
    }
}
int MatchTile(int currentstate[], int goalstate[]){
    int match = 0;
    for(int i=0; i<9; i++){
        if(currentstate[i] != 0 && currentstate[i] == goalstate[i])
            match++;
    }
    return match;
}
int GetTheBestMove(int heuristic[]){
    int index = 0;
    int max = heuristic[0];
    for(int i=0; i<4; i++){
        if(heuristic[i] > max){
            max = heuristic[i];
            index = i;
        }
    }
    return index;
}
int main(int argc, char** argv) {
    int InitialState[] = {2,8,3,1,6,4,7,0,5};
    GetGoalState(InitialState,GoalState);
    cout << "\nInit State\n" << "----------";        
    PrintState(InitialState);
    cout << "\nGoal State\n" << "----------";
    PrintState(GoalState);
    cout << "\nSearching\n" << "---------";         
    CopyState(InitialState,CurrentState);
    int level = 0;
    clock_t t;
    t = clock();
    while(!SameState(CurrentState,GoalState)){
        PrintState(CurrentState);
        CopyState(CurrentState,ChildState);
        int Heuristic[] = {0,0,0,0};
        level++;
        MoveUp(ChildState);
        if(!SameState(ChildState,CurrentState)){
            Heuristic[0] = MatchTile(ChildState,GoalState) + level;
            MoveDown(ChildState);
        }
        MoveDown(ChildState);
        if(!SameState(ChildState,CurrentState)){
            Heuristic[1] = MatchTile(ChildState,GoalState) + level;
            MoveUp(ChildState);
        }
        MoveLeft(ChildState);
        if(!SameState(ChildState,CurrentState)){
            Heuristic[2] = MatchTile(ChildState,GoalState) + level;
            MoveRight(ChildState);
        }
        MoveRight(ChildState);
        if(!SameState(ChildState,CurrentState)){
            Heuristic[3] = MatchTile(ChildState,GoalState) + level;
            MoveLeft(ChildState);
        }
        switch(GetTheBestMove(Heuristic)){
            case 0 : MoveUp(CurrentState); break;
            case 1 : MoveDown(CurrentState); break;
            case 2 : MoveLeft(CurrentState); break;
            case 3 : MoveRight(CurrentState); break;
        }       
    }
    PrintState(CurrentState);
    t = clock() - t;
    double time_taken = ((double)t)/CLOCKS_PER_SEC;
    printf("\n Solved with %i steps, %f seconds...",level,time_taken);
    getchar();
    return 0;
}

but, php program that I created was an error. I hope you can help me, thanks, before :)

List your errors please. It would be helpful to know where you're hitting the roadblock.

Apart from the errors, which puzzle is it solving, or am I missing something obvious?

916345348a75311309dc8658ff4a3ee1

tts output shows an infinite loop with output 0

Member Avatar for diafol

Sorry to say kaz, but this is a terrible question. You do not give any info about what the code is supposed to do. You had to be prompted to show the error. Please read pritaeas' excellent sticky

As I already pointed in the different topic, Lines 58-59, 65-66, 72-73, 79-80 are not logically correct.

//# i.e.
$state[$index] = $state[$index-3];
$state[$index-3] = $state[$index-3] - $state[$index];

//This means the same as
$state[$index] = $state[$index-3];
$state[$index-3] = 0;

You need to review your algorithm...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.