import java.awt.*; import java.awt.Color; import java.awt.event.*; import java.lang.String; public class Mo extends Frame implements MouseMotionListener,Runnable{ static int pole[][] =new int [10][10]; static int d[]=new int [20]; static Label la=new Label("",Label.CENTER); static Label la2=new Label("YOU", Label.CENTER); static Label la3=new Label("made it !", Label.CENTER); static Button kno[][]=new Button[10][10]; String z; static String v; int x; int y; int resu=0; int moves=0; static int probka=1; boolean vot=false; static int per=0; public void run(){ } public Mo(){ super("The fifteen"); setSize(320, 320); setVisible(true); setLayout(new GridLayout(4,4)); setFont(new Font("Helvetica",Font.BOLD, 24)); for (int n=1;n<5;n++){ for (int nn=1;nn<5;nn++){ if (pole[n][nn]!=25){add(kno[n][nn]);} else {add(la);}} } setVisible(true); addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); } public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){ } public boolean action(Event e, Object o){ if (o instanceof String){ z=((String) o); for (int t=1;t<5;t++){ for (int tt=1;tt<5;tt++){ int b=pole[t][tt]; v=""+b; if (v.equals(z)){ x=tt; y=t;} }} vot=false; if (pole[y+1][x]==25){ pole[y+1][x]=pole[y][x]; pole[y][x]=25;vot=true;} if (pole[y-1][x]==25){ pole[y-1][x]=pole[y][x]; pole[y][x]=25;vot=true;} if (pole[y][x-1]==25){ pole[y][x-1]=pole[y][x]; pole[y][x]=25;vot=true;} if (pole[y][x+1]==25){ pole[y][x+1]=pole[y][x]; pole[y][x]=25;vot=true;} if (vot){ for (int t=1;t<5;t++){ for (int tt=1;tt<5;tt++){ remove(kno[t][tt]); kno[t][tt]=new Button(""+pole[t][tt]); }} remove(la); setLayout(new GridLayout(4,4)); for (int n=1;n<5;n++){ for (int nn=1;nn<5;nn++){ if (pole[n][nn]!=25){kno[n][nn].setBackground(Color.magenta);add(kno[n][nn]);} else {la.setBackground(Color.orange);add(la);}} } setVisible(true); moves=moves+1; } for(int n=1;n<5;n++){ for(int nn=1;nn<5;nn++){ if (pole[n][nn]==nn+4*n-4){resu=resu+1;}}} if (resu==15){ for (int t=1;t<5;t++){ for (int tt=1;tt<5;tt++){ remove(kno[t][tt]); kno[t][tt]=new Button(""+pole[t][tt]); }} remove(la); setLayout(new GridLayout(3,1)); la2.setForeground(Color.green); la3.setForeground(Color.red); la.setForeground(Color.blue); la.setBackground(Color.pink); la2.setBackground(Color.pink); la3.setBackground(Color.pink); add(la2); add(la3); la.setText("Your efficiency is "+per*100/moves+"% !"); add(la); setVisible(true); } resu=0; } return false;} static int j=0; static int h=0; public static void main(String argumendid[]){ while(probka!=0){ probka=0; per=0; for(int h=1;h<16;h++){ d[h]=0;} for(int w=1;w<16;w++){ do{ h=(int)(15*Math.random())+1; j=0; for (int ww=1;ww<16;ww++){ if (d[ww]==h){j=1;} } if (j!=1){d[w]=h;} }while(j==1); } for (int w=1;w<16;w++){ for (int ww=w+1;ww<16;ww++){ if (d[w]>d[ww]) {per=per+1;if(probka==1){probka=0;}else{probka=1;}} } } } for (int t=1;t<5;t++){ for (int tt=1;tt<5;tt++){ pole[t][tt]=d[tt+4*t-4]; kno[t][tt]=new Button(""+pole[t][tt]); kno[t][tt].setBackground(Color.magenta); }} pole[4][4]=25; la.setBackground(Color.orange); for (int t=1;t<5;t++){ for (int tt=1;tt<5;tt++){ probka=pole[t][tt]; }} new Mo(); } }