0

everyone , i'm managing to create a 8-puzzle game in java,

i'm trying to find possible moves (of the blank board ,that contains "0") from the original state

How ever i can't undertand why java isn't taking the original state everytime

for example :

original state = [1,2,3,4,0,5,6,7,8] first move should be to get 0 "UP" so new_etat1 = [1,0,3,4,2,5,6,7,8]

second move should be to get 0 "DOWN" so new_eta2 = [1,2,3,4,7,5,6,0,8] but it is showing me this instead : LIKE IT TAKES new_etat1 as the original state

Here is my code:

Main.java:

public class Main{


public static void main(String[] args) {
    String [] etat_init = [1,2,3,4,0,5,6,7,8,];

    Successeur S = new Successeur(etat_init);

    S.successor(etat_init);
}

Successuer.java:

import java.util.ArrayList;
import java.util.List;

public class Successeur {
private String[] etat = new String [9];
private String[] new_etat1 = new String [9];
private String[] new_etat2 = new String [9];
private String[] new_etat3 = new String [9];
private String[] new_etat4 = new String [9];
private int case_vide_index;

public Successeur(String [] etat) {
            this.new_etat1 = etat;
            this.new_etat2 = etat;
            this.new_etat3 = etat;
            this.new_etat4 = etat;
            this.case_vide_index = 4;       
}
    
private void  Deplacer_case_vide(String[] X,int a, int b){
    String temp;
    temp = X[a];
    X[a] = X[b];
    X[b] = temp;
    //return X;
}   

public void successor(String [] etat) {

            int i ;
    //ON PEUT GLISSER LA CASE VIDE EN (HAUT)
    if(this.case_vide_index - 3 >= 0)
            {
                this.Deplacer_case_vide(this.new_etat1,(this.case_vide_index - 3), this.case_vide_index);
                for(i=0;i<9;i++){
                    System.out.println("*****************L'ETAT ***********************");
                    System.out.print("[");
                    for (i=0;i<9;i++)
                    {
                        System.out.print(" ["+this.new_etat1[i]+"] ");
                    }
                    System.out.println("]");            
                }
    }
    
    //ON PEUT GLISSER LA CASE VIDE EN (BAS)
    if(this.case_vide_index + 3 < 9)
            {
                this.Deplacer_case_vide(this.new_etat2 ,(this.case_vide_index + 3), this.case_vide_index);           
                for(i=0;i<9;i++){
                    System.out.println("*****************L'ETAT ***********************");
                    System.out.print("[");
                    for (i=0;i<9;i++)
                    {
                        System.out.print(" ["+this.new_etat2[i]+"] ");
                    }
                    System.out.println("]");            
                }                    
    }   
            
    //ON PEUT GLISSER LA CASE VIDE A (DROITE)
    if(((this.case_vide_index + 1) % 3 == 1) || ((this.case_vide_index + 1) % 3 == 2))
            {
                this.Deplacer_case_vide(this.new_etat3,(this.case_vide_index + 1), this.case_vide_index); 
                for(i=0;i<9;i++){
                    System.out.println("*****************L'ETAT ***********************");
                    System.out.print("[");
                    for (i=0;i<9;i++)
                    {
                        System.out.print(" ["+this.new_etat3[i]+"] ");
                    }
                    System.out.println("]");            
                }                  
    }   
            
            
            
    //ON PEUT GLISSER LA CASE VIDE A (GAUCHE)
    if(((this.case_vide_index - 1) % 3 == 0) || ((this.case_vide_index - 1) % 3 == 1))
            {
 
                this.Deplacer_case_vide(this.new_etat4,(this.case_vide_index - 1), this.case_vide_index);   
                for(i=0;i<9;i++){
                    System.out.println("*****************L'ETAT ***********************");
                    System.out.print("[");
                    for (i=0;i<9;i++)
                    {
                        System.out.print(" ["+this.new_etat4[i]+"] ");
                    }
                    System.out.println("]");            
                }                    
    }   
     
}    

}

Maher
  • 11
  • 1
  • 5
  • It is hard to understand the logic, but case_vide_index is always 4. – pringi Mar 28 '22 at 11:29
  • 1
    I didn't read all that code (that's what you should do with a debugger) but this seems to be at least part of the problem: `this.new_etat1 = etat; this.new_etat2 = etat; ...`. You're basically refering to the _same_ array from 4 different variables and when you change an element in `new_etat1` this will be reflected in `new_etat2` because they refer to (think "point to") the same array which is also accessible from the outside. You probably want to create a copy and asign that instead. Try `new_etat1 = Arrays.copy(etat, etat.length);` etc. – Thomas Mar 28 '22 at 11:29
  • @pringi i put case_vide_index to refeer the index of the blank case (in the example [1,2,3,4,0,5,6,7,8] it is index 4) – Maher Mar 28 '22 at 11:50
  • @Thomas , thanks , but "etat" shouldn't be changed , it is passed from Main.java (Successeur S = new Successeur(etat_init);) so normaly all (new_etat1 , new_etat2 ,new_etat3 ,new_etat4) should have the same value (etat_init) at the begining of the execution of the method (successor) ? – Maher Mar 28 '22 at 11:54
  • 1
    That's exactly what I'm saying. `new_etat1 = etat` effectively makes both variables refer to the _same_ array and thus any change to either is reflected in that array. That's why you need to copy the array before assigning it. – Thomas Mar 28 '22 at 12:00
  • as @Thomas said, just to reformulate you pass the same reference to all arrays, so when one changed is reflected to all. Means `new_etat_1_4` will be always equals, no matter which one is changed – Traian GEICU Mar 28 '22 at 12:00
  • thanks @Thomas ,it worked by copying Arrays ,though i think this happens only in JAVA as i guess ,some other programming languages have the ability to pass argument by value ,not refrence – Maher Mar 28 '22 at 12:14
  • 1
    "other programming languages have the ability to pass argument by value ,not refrence". Have to be sure what is the value passed. If value is a reference to another object, then you can alter the object content (not the ref. value passed). You may check with php where your approach could work. Note: Java pass by value, but again what is the value ! – Traian GEICU Mar 28 '22 at 12:21
  • https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Traian GEICU Mar 28 '22 at 12:23

0 Answers0