Kotak Ajaib (Game Puzzle)

Dalam program puzzle geser yang di beri nama kotak ajaib adalah sebuah program yang dimana hanya bisa memainkan pemikiran dan logika anda ketika memainkannya. Maksutnya, disini program ini melakukan pergeseran dengan cara horizontal dan vertical dan tidak untuk diagonal, karena puzzle ini akan menambrak angka satu dengan angka yang lainnya. Serta pergeseran up atau down. Jadi ada empat pilihan ketika anda memainkankan, yaitu: up, down, right, left.

ini source codenya:

package gui_pratikum;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

/**

 *

 * @author AfNISA

 */

public class demo_kotakAjaib extends JFrame {

    int a = 0, x = 5, y = 45;

    int kb = 115, kk = 155;

    int menit = 0, detik = 0;

    int temp_x[] = new int[9];

    int temp_y[] = new int[9];

    JButton bt1 = new JButton(“1”);

    JButton bt2 = new JButton(“2”);

    JButton bt3 = new JButton(“3”);

    JButton bt4 = new JButton(“4”);

    JButton bt5 = new JButton(“5”);

    JButton bt6 = new JButton(“6”);

    JButton bt7 = new JButton(“7”);

    JButton bt8 = new JButton(“8″);

    JLabel blank = new JLabel(” “);

    JLabel time_out = new JLabel(“waktu: ” + menit + “:” + detik);

    JPanel panel1 = new JPanel();

    JButton mulai = new JButton(“STARTS”);

    Thread otomatis;

    public demo_kotakAjaib() {

        super(“kotJIbun”);

        setSize(187, 250);

        setLocation(400, 200);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setResizable(false);

        setVisible(true);

        tombol klik = new tombol();

        bt1.addActionListener(klik);

        bt1.setBackground(Color.red);

        bt2.addActionListener(klik);

        bt2.setBackground(Color.yellow);

        bt3.addActionListener(klik);

        bt3.setBackground(Color.red);

        bt4.addActionListener(klik);

        bt4.setBackground(Color.yellow);

        bt5.addActionListener(klik);

        bt5.setBackground(Color.red);

        bt6.addActionListener(klik);

        bt6.setBackground(Color.yellow);

        bt7.addActionListener(klik);

        bt7.setBackground(Color.red);

        bt8.addActionListener(klik);

        bt8.setBackground(Color.yellow);

        mulai.addActionListener(klik);

        panel1.setLayout(null);

        for (int i = 1; i < 9; i++) {

            a = a + 2;

            if (a == 10) {

                a = 1;

            }

            if (i == 1 || i == 4 || i == 7) {

                x = 5;

                if (i == 4 || i == 7) {

                    y += 55;

                }

            }

            temp_x[0] = 0;

            temp_y[0] = 0;

            switch (a) {

                case 1:

                    panel1.add(bt1).setBounds(x, y, 50, 50);

                    temp_x[1] = x;

                    temp_y[1] = y;

                    break;

                case 2:

                    panel1.add(bt2).setBounds(x, y, 50, 50);

                    temp_x[2] = x;

                    temp_y[2] = y;

                    break;

                case 3:

                    panel1.add(bt3).setBounds(x, y, 50, 50);

                    temp_x[3] = x;

                    temp_y[3] = y;

                    break;

                case 4:

                    panel1.add(bt4).setBounds(x, y, 50, 50);

                    temp_x[4] = x;

                    temp_y[4] = y;

                    break;

                case 5:

                    panel1.add(bt5).setBounds(x, y, 50, 50);

                    temp_x[5] = x;

                    temp_y[5] = y;

                    break;

                case 6:

                    panel1.add(bt6).setBounds(x, y, 50, 50);

                    temp_x[6] = x;

                    temp_y[6] = y;

                    break;

                case 7:

                    panel1.add(bt7).setBounds(x, y, 50, 50);

                    temp_x[7] = x;

                    temp_y[7] = y;

                    break;

                case 8:

                    panel1.add(bt8).setBounds(x, y, 50, 50);

                    temp_x[8] = x;

                    temp_y[8] = y;

                    break;

                default:

                    i–;

                    if (a <= 10) {

                        a -= 2;

                    }

                    break;

            }

            x += 55;

        }

        panel1.add(blank).setBounds(kb, kk, 50, 50);

        time_out.setForeground(Color.white);

        panel1.add(time_out).setBounds(40, 25, 100, 20);

        panel1.setBackground(Color.black);

        panel1.add(mulai).setBounds(40, 5, 90, 20);

        add(panel1);

        waktu();

    }

    public int menang() {

        if (temp_x[1] == 5 && temp_y[1] == 45 && temp_x[2] == 60 && temp_y[2] == 45 && temp_x[3] == 115 && temp_y[3] == 45 && temp_x[4] == 5 && temp_y[4] == 100 && temp_x[5] == 60 && temp_y[5] == 100 && temp_x[6] == 115 && temp_y[6] == 100 && temp_x[7] == 5 && temp_y[7] == 155 && temp_x[8] == 60 && temp_y[8] == 155) {

            JOptionPane.showMessageDialog(null, “Hore.. menang…!!!”);

            this.setVisible(false);

            return 1;

        }

        return 0;

    }

     public void waktu(){

        for(int w=0;w<=1000;w++){

            try {

                if(menang()==1)break;

                otomatis.sleep(700);

                detik++;

                if (detik == 61) {

                    menit++;

                    detik=0;

                }

                time_out.setText(“waktu “+menit+” : “+detik);

            } catch (Exception ex) {

                System.out.println(“Exeption”);

            }

            if(w==600){

            this.setVisible(false);

            JOptionPane.showMessageDialog(null, “wah Sorry, U kalah…??”);

            break;

            }

        }

        JOptionPane.showMessageDialog(null, “U can finish in “+menit+”menit,”+detik+”detik”);

    }

    class tombol implements ActionListener {

        public void actionPerformed(ActionEvent e) {

        if(e.getSource()==bt1){

            if(temp_x[1]!=kb){

            if(temp_x[1]<kb){

                if(temp_y[1]==kk){

                if(temp_x[1]+55==kb){

                    panel1.add(bt1).setBounds(temp_x[1]+55, temp_y[1] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[1]+=55;

                    kb-=55;

                }

                }

            }

            else if(temp_x[1]>kb){

                if(temp_y[1]==kk){

                    if(temp_x[1]-55==kb){

                    panel1.add(bt1).setBounds(temp_x[1]-55, temp_y[1] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[1]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if(temp_y[1]<kk){

                    if(temp_y[1]+55==kk){

                    panel1.add(bt1).setBounds(temp_x[1], temp_y[1]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[1]+=55;

                    kk-=55;

                    }

                }

                else if(temp_y[1]>kk){

                    if(temp_y[1]-55==kk){

                    panel1.add(bt1).setBounds(temp_x[1], temp_y[1]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[1]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==bt2){

            if(temp_x[2]!=kb){

            if(temp_x[2]<kb){

                if(temp_y[2]==kk){

                    if(temp_x[2]+55==kb){

                    panel1.add(bt2).setBounds(temp_x[2]+55, temp_y[2] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[2]+=55;

                    kb-=55;

                    }

                }

            }

            else if(temp_x[2]>kb){

                if(temp_y[2]==kk){

                    if(temp_x[2]-55==kb){

                    panel1.add(bt2).setBounds(temp_x[2]-55, temp_y[2] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[2]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if(temp_y[2]<kk){

                    if(temp_y[2]+55==kk){

                    panel1.add(bt2).setBounds(temp_x[2], temp_y[2]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[2]+=55;

                    kk-=55;

                    }

                }

                else if(temp_y[2]>kk){

                    if(temp_y[2]-55==kk){

                    panel1.add(bt2).setBounds(temp_x[2], temp_y[2]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[2]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==bt3){

            if(temp_x[3]!=kb){

            if(temp_x[3]<kb){

                if(temp_y[3]==kk){

                    if(temp_x[3]+55==kb){

                    panel1.add(bt3).setBounds(temp_x[3]+55, temp_y[3] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[3]+=55;

                    kb-=55;

                    }

                }

            }

            else if(temp_x[3]>kb){

                if(temp_y[3]==kk){

                    if(temp_x[3]-55==kb){

                    panel1.add(bt3).setBounds(temp_x[3]-55, temp_y[3] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[3]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if(temp_y[3]<kk){

                    if(temp_y[3]+55==kk){

                    panel1.add(bt3).setBounds(temp_x[3], temp_y[3]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[3]+=55;

                    kk-=55;

                    }

                }

                else if(temp_y[3]>kk){

                    if(temp_y[3]-55==kk){

                    panel1.add(bt3).setBounds(temp_x[3], temp_y[3]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[3]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==bt4){

            if(temp_x[4]!=kb){

            if(temp_x[4]<kb){

                if(temp_y[4]==kk){

                    if(temp_x[4]+55==kb){

                    panel1.add(bt4).setBounds(temp_x[4]+55, temp_y[4] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[4]+=55;

                    kb-=55;

                    }

                }

            }

            else if(temp_x[4]>kb){

                if(temp_y[4]==kk){

                    if(temp_x[4]-55==kb){

                    panel1.add(bt4).setBounds(temp_x[4]-55, temp_y[4] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[4]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if(temp_y[4]<kk){

                    if(temp_y[4]+55==kk){

                    panel1.add(bt4).setBounds(temp_x[4], temp_y[4]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[4]+=55;

                    kk-=55;

                    }

                }

                else if(temp_y[4]>kk){

                    if(temp_y[4]-55==kk){

                    panel1.add(bt4).setBounds(temp_x[4], temp_y[4]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[4]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==bt5){

            if(temp_x[5]!=kb){

            if(temp_x[5]<kb){

                if(temp_y[5]==kk){

                    if(temp_x[5]+55==kb){

                    panel1.add(bt5).setBounds(temp_x[5]+55, temp_y[5] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[5]+=55;

                    kb-=55;

                    }

                }

            }

            else if(temp_x[5]>kb){

                if(temp_y[5]==kk){

                    if(temp_x[5]-55==kb){

                    panel1.add(bt5).setBounds(temp_x[5]-55, temp_y[5] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[5]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if(temp_y[5]<kk){

                    if(temp_y[5]+55==kk){

                    panel1.add(bt5).setBounds(temp_x[5], temp_y[5]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[5]+=55;

                    kk-=55;

                    }

                }

                else if(temp_y[5]>kk){

                    if(temp_y[5]-55==kk){

                    panel1.add(bt5).setBounds(temp_x[5], temp_y[5]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[5]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==bt6){

            if(temp_x[6]!=kb){

            if(temp_x[6]<kb){

                if(temp_y[6]==kk){

                    if(temp_x[6]+55==kb){

                    panel1.add(bt6).setBounds(temp_x[6]+55, temp_y[6] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[6]+=55;

                    kb-=55;

                    }

                }

            }

            else if(temp_x[6]>kb){

                if(temp_y[6]==kk){

                    if(temp_x[6]-55==kb){

                    panel1.add(bt6).setBounds(temp_x[6]-55, temp_y[6] , 50, 50);

                    panel1.add(blank).setBounds(kb+55,kk, 50, 50);

                    temp_x[6]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if( temp_y[6]<kk){

                    if( temp_y[6]+55==kk){

                    panel1.add(bt6).setBounds(temp_x[6],  temp_y[6]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[6]+=55;

                    kk-=55;

                    }

                }

                else if( temp_y[6]>kk){

                    if( temp_y[6]-55==kk){

                    panel1.add(bt6).setBounds(temp_x[6],  temp_y[6]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[6]-=55;

                    kk+=55;

                    }

                }

            }

        }

         else if(e.getSource()==bt7){

            if(temp_x[7]!=kb){

            if(temp_x[7]<kb){

                if( temp_y[7]==kk){

                if(temp_x[7]+55==kb){

                    panel1.add(bt7).setBounds(temp_x[7]+55,  temp_y[7] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[7]+=55;

                    kb-=55;

                }

                }

            }

            else if(temp_x[7]>kb){

                if( temp_y[7]==kk){

                    if(temp_x[7]-55==kb){

                    panel1.add(bt7).setBounds(temp_x[7]-55,  temp_y[7] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[7]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if( temp_y[7]<kk){

                    if( temp_y[7]+55==kk){

                    panel1.add(bt7).setBounds(temp_x[7],  temp_y[7]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[7]+=55;

                    kk-=55;

                    }

                }

                else if( temp_y[7]>kk){

                    if( temp_y[7]-55==kk){

                    panel1.add(bt7).setBounds(temp_x[7],  temp_y[7]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                     temp_y[7]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==bt8){

            if(temp_x[8]!=kb){

            if(temp_x[8]<kb){

                if( temp_y[8]==kk){

                    if(temp_x[8]+55==kb){

                    panel1.add(bt8).setBounds(temp_x[8]+55,  temp_y[8] , 50, 50);

                    panel1.add(blank).setBounds(kb-55, kk, 50, 50);

                    temp_x[8]+=55;

                    kb-=55;

                    }

                }

            }

            else if(temp_x[8]>kb){

                if( temp_y[8]==kk){

                    if(temp_x[8]-55==kb){

                    panel1.add(bt8).setBounds(temp_x[8]-55,  temp_y[8] , 50, 50);

                    panel1.add(blank).setBounds(kb+55, kk, 50, 50);

                    temp_x[8]-=55;

                    kb+=55;

                    }

                }

            }

            }

            else{

                if( temp_y[8]<kk){

                    if( temp_y[8]+55==kk){

                    panel1.add(bt8).setBounds(temp_x[8],  temp_y[8]+55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk-55, 50, 50);

                    temp_y[8]+=55;

                    kk-=55;

                    }

                }

                else if( temp_y[8]>kk){

                    if( temp_y[8]-55==kk){

                    panel1.add(bt8).setBounds(temp_x[8],  temp_y[8]-55 , 50, 50);

                    panel1.add(blank).setBounds(kb, kk+55, 50, 50);

                    temp_y[8]-=55;

                    kk+=55;

                    }

                }

            }

        }

        else if(e.getSource()==mulai){

          detik=0;

          menit=0;

          bt7.doClick();

          bt5.doClick();

          bt8.doClick();

          bt2.doClick();

          bt4.doClick();

          bt6.doClick();

          bt3.doClick();

          bt1.doClick();

        }

        }

        }

    public static void main(String[] args) {

        new demo_kotakAjaib();

    }

}

keterangan : Jadi penggunaan script di atas adalah sebuah game puzzle dengan menyetting letak kotak yang akan di geser sesuai koordinat yang telah ditentukan dan penggunaan timer serta random awal pertama di klik akan bergeser. Untuk lebih jelas dan paham anda bisa menghubungi saya melawati email atau langsung melalui hp. thankz.

—————————————– ^_^ gooD luCk ^_^ ——————————-

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s