Código fuente del hechizo
Importar Java .
Importar javax.swing.*;
La clase mainframe extiende jframe para implementar el detector de acciones {//Define todo el marco.
JButton privado[]JB = nuevo JButton[8];
JButton privado jbs = nuevo JButton(" apertura ");
JButton privado jbres = nuevo JButton("Reiniciar");
JPanel privado jp 1 = nuevo JPanel();
JPanel privado jp2 = nuevo JPanel();
privado int [ ]n = nuevo int[9];
int privado[]n 1 = nuevo int[9];
posición int privado = 8, p, q;
Bl booleano privado, startbl = false
JLabel JL privado = nuevo JLabel();
cuenta int privada = 0;
JLabel JL privado 1 = new JLabel("+integer . tostring(0));
Public host(){ // método de construcción del marco
int I;
for( int j = 0; j & ltn .longitud; j++){
n[j]= j;
n 1[j]= n[j];
p>}
for(I = 0;i<jb.lengthI++){ //Asigne un valor correspondiente a cada botón y grabe al oyente
JB[I]=. new JButton(integer. tostring(I+1));
Jb[i].setFont("número", font. negrita, 48));
jp2 . JB[I]);
jb[i]. addActionListener(este);
}
for( i = 0;i<n.length;i++) {
if(n[i]==posición)
jp2.add(JL);
Otros
jp2 . (JB[n[I]]);
}
jp2.setLayout(new GridLayout(3, 3)); //Registrar el oyente
jbs. addactionlistener(this);
jbres. addactionlistener(this);
jp 1. add(jbs);
jp 1. add(jbres) ;
jp 1. add(JL 1);
jp 1. set layout(new FlowLayout());/ /Establezca jp1 en diseño de flujo.
set layout(new BorderLayout()); //El diseño general es el diseño del borde.
this.add("Norte ", jp 1);
this.add("Centro ", jp2); ");
this.setBounds(100, 100, 300, 350);
this . setdefaultcloseoperation(JFrame. EXIT _ ON _ CLOSE); // Implementar el botón de cerrar < / p>
this . setresizable(false);
this . set visible(true);
}
acción nula pública realizada (evento de acción). e ){//Implementar eventos del botón.
If(e.getSource()==jbres){ //Evento del botón de reinicio.
for(int j = 0; j & ltn . length; j++)
n[j]= n 1[j];
reShow() ;
startbl = true
count = 0;
JL 1 . settext("+integer . tostring(0));
}
Else if(e.getSource()==jbs) //Iniciar evento del botón
Esto.
else if(startbl ){// Evento de movimiento del botón 1-8
for(int I = 0;i<jb.lengthi++)
if(e.getSource() == jb[i] ){
//sistema . println(I+1);
for(int a = 0; a & ltn . length; a++){
if(n[ a]==i)
p = a;
si(n[a]= =posición)
q = a ;
}
}
Si (p!= 0 & amp& ampp!= 1. & ampp!= 2)
Si ((p-3 ) == q)
Intercambio (p, q);
Si (p!= 0 & amp& ampp!= 3 & amp& ampp!= 6)
Si ((p-1) == q)
Intercambia (p, q);
Si (p! = 2 & amp& ampp! = 5 & amp& ampp! = 8)
if((p+1) == q)
Intercambiar (p, q);
Si (p!= 6 & amp& ampp!= 7 & amp& ampp!= 8)
Si ((p+3) == q)
Intercambiar (p, q);
}
}
Intercambio de vacío público (int x, int y) {//Los botones 1-8 se intercambian con imágenes en blanco
p>
z = n[x];
n[x]= n[y];
n[y] = z;
JL 1. settext("+integer . tostring(++count));
reShow();
win();
}
Public void Init(){ //Genera aleatoriamente la interfaz del juego.
int i=0,j,x;
Booleano bl;
mientras(i<9){
bl = verdadero ;
x =(int)(matemáticas. aleatorio()* 9);
for(j = 0; j<j++)
if(n[j ] == x)
bl = false;
if(bl){
n[i++]= x;
n 1[I-1]= x;
}
}
reShow();
startbl = verdadero
cuenta = 0;
JL 1 . settext("+integer . tostring(0));
}
public void show(){ //Reescribe la interfaz del juego
for(int I = 0; i<n. length; i++){
if(n[i] == Posición)
jp2 . agregar(JL);
Otros
jp2 . p>jp2 . revidate();
}
Public void win(){ //Determina si es exitoso
winbl = true
for(int I = 0;i<n.length;i++)
if(n[i]!=i)
winbl = false
if(winbl){
Panel de opciones. Mostrar cuadro de diálogo de mensaje (esto, "¡Felicitaciones, lo logró!" + "Usó" + Entero. toString(count)+ "paso", ",jooptionpane.info_message);
startbl = false
}
}
}
Clase pública Collage{//Clase de función principal
Public static void main(String[] args){
New Mainframe() ;
}
}
Mi edición anterior no fue muy buena
.