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 :
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("]");
}
}
}
}