mardi 11 juillet 2017

How should i generate Random numbers from 1 to 8 in the following program without repeating them for the shuffle puzzle game?

/I created the following shuffle puzzle using java awt. At the start of the program, as you can see , all numbers are in order. I want to generate random numbers without repeating. How to do this? Should I use Random class?/

    import java.awt.*;
    import java.awt.event.*;
    class Puzzle extends Frame implements ActionListener
    {
        Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
        Puzzle()
        {
            setTitle("Shuffle");
            setSize(500,500);
            setLayout(new GridLayout(3,3));
            setVisible(true);
            Font f=new Font("Arial",Font.BOLD,100);

            b1=new Button("1");
                                b1.setFont(f);
                                b1.addActionListener(this);
            b2=new Button("2");
                                b2.setFont(f);
                                b2.addActionListener(this);
            b3=new Button("3");
                                b3.setFont(f);
                                b3.addActionListener(this);
            b4=new Button("4");
                                b4.setFont(f);
                                b4.addActionListener(this);
            b5=new Button("5");
                                b5.setFont(f);
                                b5.addActionListener(this);
            b6=new Button("6");
                                b6.setFont(f);
                                b6.addActionListener(this);
            b7=new Button("7");
                                b7.setFont(f);
                                b7.addActionListener(this);
            b8=new Button("8");
                                b8.setFont(f);
                                b8.addActionListener(this);
            b9=new Button(" ");
                                b9.setFont(f);
                                b9.addActionListener(this);

            add(b1); add(b2); add(b3);
            add(b4); add(b5); add(b6);
            add(b7); add(b8); add(b9);




        } 
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource() == b1)
            {
                if(b2.getLabel() == " ")
                {
                    b2.setLabel(b1.getLabel());
                    b1.setLabel(" ");
                }
            else if(b4.getLabel() == " ")
                {

                  b4.setLabel(b1.getLabel());
                    b1.setLabel(" ");
                }

            }
            if(ae.getSource() == b2)
            {
                if(b1.getLabel() == " ")
                {
                    b1.setLabel(b2.getLabel());
                    b2.setLabel(" ");
                }
            else if(b3.getLabel() == " ")
                {

                  b3.setLabel(b2.getLabel());
                    b2.setLabel(" ");
                }
                else if(b5.getLabel() == " ")
                {

                  b5.setLabel(b2.getLabel());
                    b2.setLabel(" ");
                }

            }
            if(ae.getSource() == b3)
            {
                if(b2.getLabel() == " ")
                {
                    b2.setLabel(b3.getLabel());
                    b3.setLabel(" ");
                }
            else if(b6.getLabel() == " ")
                {

                  b6.setLabel(b3.getLabel());
                    b3.setLabel(" ");
                }

            }
            if(ae.getSource() == b4)
            {
                if(b1.getLabel() == " ")
                {
                    b1.setLabel(b4.getLabel());
                    b4.setLabel(" ");
                }
            else if(b5.getLabel() == " ")
                {

                  b5.setLabel(b4.getLabel());
                    b4.setLabel(" ");
                }
                else if(b7.getLabel() == " ")
                {

                  b7.setLabel(b4.getLabel());
                    b4.setLabel(" ");
                }

            }
            if(ae.getSource() == b5)
            {
                if(b2.getLabel() == " ")
                {
                    b2.setLabel(b5.getLabel());
                    b5.setLabel(" ");
                }
            else if(b4.getLabel() == " ")
                {

                  b4.setLabel(b5.getLabel());
                    b5.setLabel(" ");
                }
                else if(b6.getLabel() == " ")
                {

                  b6.setLabel(b5.getLabel());
                    b5.setLabel(" ");
                }
                else if(b8.getLabel() == " ")
                {

                  b8.setLabel(b5.getLabel());
                    b5.setLabel(" ");
                }

            }
            if(ae.getSource() == b6)
            {
                if(b3.getLabel() == " ")
                {
                    b3.setLabel(b6.getLabel());
                    b6.setLabel(" ");
                }
            else if(b5.getLabel() == " ")
                {

                  b5.setLabel(b6.getLabel());
                    b6.setLabel(" ");
                }
            else if(b9.getLabel() == " ")
                {

                  b9.setLabel(b6.getLabel());
                    b6.setLabel(" ");
                }

            }
            if(ae.getSource() == b7)
            {
                if(b4.getLabel() == " ")
                {
                    b4.setLabel(b7.getLabel());
                    b7.setLabel(" ");
                }
            else if(b8.getLabel() == " ")
                {

                  b8.setLabel(b7.getLabel());
                    b7.setLabel(" ");
                }

            }
            if(ae.getSource() == b8)
            {
                if(b5.getLabel() == " ")
                {
                    b5.setLabel(b8.getLabel());
                    b8.setLabel(" ");
                }
            else if(b7.getLabel() == " ")
                {

                  b7.setLabel(b8.getLabel());
                    b8.setLabel(" ");
                }
                else if(b9.getLabel() == " ")
                {

                  b9.setLabel(b8.getLabel());
                    b8.setLabel(" ");
                }

            }
            if(ae.getSource() == b9)
            {
                if(b6.getLabel() == " ")
                {
                    b6.setLabel(b9.getLabel());
                    b9.setLabel(" ");
                }
            else if(b8.getLabel() == " ")
                {

                  b8.setLabel(b9.getLabel());
                    b9.setLabel(" ");
                }

            }

        }
        public static void main(String args[])
        {

          new Puzzle();
        }
    }




Aucun commentaire:

Enregistrer un commentaire