questions regarding the use of A* with the 15-square puzzle

前端 未结 8 1924
无人共我
无人共我 2021-02-07 12:41

I\'m trying to build an A* solver for a 15-square puzzle.

The goal is to re-arrange the tiles so that they appear in their natural positions. You can only sli

8条回答
  •  余生分开走
    2021-02-07 13:03

    check this
    import javax.swing.*; 
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Object;
    
    class Puzzle extends JPanel implements ActionListener
    {
        JButton[] b = new JButton[16];
        Puzzle()
        {
            b[0] = new JButton("4");
            b[1] = new JButton("11");
            b[2] = new JButton("5");
            b[3] = new JButton("9");
            b[4] = new JButton("1");
            b[5] = new JButton("10");
            b[6] = new JButton("12");
            b[7] = new JButton("13");
            b[8] = new JButton("15");
            b[9] = new JButton("14");
            b[10] = new JButton("3");
            b[11] = new JButton("2"); 
            b[12] = new JButton("7");
            b[13] = new JButton("8");
            b[14] = new JButton("6");
            b[15] = new JButton("");
            GridLayout grid = new GridLayout(4,4);
            setLayout(grid);
            for(int i=0;i<16;i++)
                add(b[i]);
            for(int i=0;i<16;i++)
                b[i].addActionListener(this);
        }
        public void actionPerformed(ActionEvent e)
        {
            /*if(e.getSource()==b[11])
            {
                if(b[15].getText()=="")
                {
                    b[15].setText("");
                }
            }
            else if(e.getSource()==b[3])
            {
                if(b[2].getText()=="")
                {
                    b[2].setText("");
                }
            }*/
            for(int i=0;i<16;i++)
            {
                System.out.println(e.getSource());
                if(e.getSource()==b[i])
                {
                    if(i==5 || i==6 || i==9 || i==10)
                    {   
                        if(b[i-1].getText()=="")
                        {
                            b[i-1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+1].getText()=="")
                        {
                            b[i+1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-4].getText()=="")
                        {
                            b[i-4].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    else if(i==4 || i==8)
                    {   
                        if(b[i+1].getText()=="")
                        {
                            b[i+1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-4].getText()=="")
                        {
                            b[i-4].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    else if(i==7 || i==11)
                    {   
                        if(b[i-1].getText()=="")
                        {
                            b[i-1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-4].getText()=="")
                        {
                            b[i-4].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    if(i==0)
                    {   
                        if(b[i+1].getText()=="")
                        {
                            b[i+1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    if(i==3)
                    {   
                        if(b[i-1].getText()=="")
                        {
                            b[i-1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    if(i==15)
                    {   
                        if(b[i-1].getText()=="")
                        {
                            b[i-1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-4].getText()=="")
                        {
                            b[i-4].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    if(i==12)
                    {   
                        if(b[i+1].getText()=="")
                        {
                            b[i+1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-4].getText()=="")
                        {
                            b[i-4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    if(i==1 || i==2)
                    {   
                        if(b[i+1].getText()=="")
                        {
                            b[i+1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-1].getText()=="")
                        {
                            b[i-1].setText(b[i].getText());
                            b[i].setText("");
                        }                   
                        else if(b[i+4].getText()=="")
                        {
                            b[i+4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                    if(i==13 || i==14)
                    {   
                        if(b[i+1].getText()=="")
                        {
                            b[i+1].setText(b[i].getText());
                            b[i].setText("");
                        }
                        else if(b[i-1].getText()=="")
                        {
                            b[i-1].setText(b[i].getText());
                            b[i].setText("");
                        }                   
                        else if(b[i-4].getText()=="")
                        {
                            b[i-4].setText(b[i].getText());
                            b[i].setText("");
                        }
                    }
                }
            }
            //System.out.println(e.getActionCommand());
            }
    
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("15-Puzzle");             
    
            //frame.setContentPane(panel);
    
    JComponent newContentPane = new Puzzle();
            //newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
    
    
    
    
            //panel.add(button);  
            frame.setSize(400,400);
    
    
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    

提交回复
热议问题