黑马程序员技术交流社区

标题: java 九宫格 [打印本页]

作者: 姚远    时间: 2012-5-11 05:21
标题: java 九宫格
求解
给定一九格图,1-8八个数字随机被放入其中8个格内,编程序在方格内移动数字,使8个数字在九格图周围8个格内顺序(逆时针,顺时针均可)排列。任一数字,只有当其周围四连通的格之一为空格时,与空格交换位置。
作者: 黄坚声    时间: 2012-5-11 07:24
定义了一个package名叫aloha

把下面的代码粘贴了,编译运行就可以了


/*
* NineGrid.java
* @author libai8723@qq.com
* Created on 2011-12-20, 13:21:36
*/

package aloha;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
*
* @author libai
*/
public class NineGrid extends javax.swing.JFrame implements ActionListener{

    /** Creates new form NineGrid */
    public NineGrid() {
        initComponents();
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        this.setSize(400, 400);
        this.setTitle("Nine Grid");
        this.setLocation((int)(d.getWidth() - 400)/2, (int)(d.getHeight() - 400)/2);

        for(int i = 0;i < 15;i++)
        {
            this.arr = i+1;
        }
        this.arr[15] = -1;
        
        this.arr[11] = -1;
        this.arr[15] = 12;

        
        for(int i = 0;i < 15;i++)
        {
            int idx =(int) (Math.random() * 15);
            int tmp = this.arr[7];
            this.arr[7] = this.arr[idx];
            this.arr[idx] = tmp;
        }
        

        for(int i = 0;i < 4;i++)
        {
            for(int j = 0;j < 4;j++)
            {
                if(this.arr[i * 4 + j] != -1)
                {
                    this.Buttons[j] =  new JButton("" + this.arr[i * 4 + j]);
                    this.Buttons[j].addActionListener(this);
                    this.getContentPane().add(this.Buttons[j]);
                }
                else
                {
                    this.Buttons[j] =  new JButton("");
                    this.Buttons[j].addActionListener(this);

                    this.getContentPane().add(this.Buttons[j]);
                    this.Buttons[j].setEnabled(false);
                }
            }
        }

        
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(4, 4));

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NineGrid().setVisible(true);
            }
        });
    }

    private JButton[][] Buttons = new JButton[4][4];
    private int[] arr = new int[16];

    private boolean isSucceed()
    {
        boolean flag = true;
        for(int i  = 0;i < 4;i++)
        {
            for(int j  = 0;j < 4;j++)
            {
                if(!this.Buttons[j].getText().equals(""))
                    if(!this.Buttons[j].getText().equals(""+(i * 4 + j + 1)))
                    {
                        return false;
                    }
            }
        }
        return true;
    }

    public void actionPerformed(ActionEvent e)
    {
        int i = 0,j = 0;
        boolean in = false;
        for(i = 0;i < 4;i++)
        {
            for(j = 0;j < 4;j++)
            {
                if(e.getSource() == this.Buttons[j])
                {
                    in = true;
                    break;
                }

            }
            if(in)
                break;
        }


        if((i >= 0 && (j - 1) >= 0)&&(!this.Buttons[j - 1].isEnabled()))
        {
            String tmp = this.Buttons[j].getText();
            this.Buttons[j].setText(this.Buttons[j - 1].getText());
            this.Buttons[j - 1].setText(tmp);
            this.Buttons[j - 1].setEnabled(true);
            this.Buttons[j].setEnabled(false);
            if(this.isSucceed())
                JOptionPane.showConfirmDialog(this, "You Win!!!!");
            return;
        }
        if((i >= 0 && (j + 1) < 4)&&(!this.Buttons[j + 1].isEnabled()))
        {
            String tmp = this.Buttons[j].getText();
            this.Buttons[j].setText(this.Buttons[j + 1].getText());
            this.Buttons[j + 1].setText(tmp);
            this.Buttons[j + 1].setEnabled(true);
            this.Buttons[j].setEnabled(false);
            if(this.isSucceed())
                JOptionPane.showConfirmDialog(this, "You Win!!!!");
            return;
        }
        if((i - 1 >= 0 && j >= 0)&&(!this.Buttons[i - 1][j].isEnabled()))
        {
            String tmp = this.Buttons[j].getText();
            this.Buttons[j].setText(this.Buttons[i - 1][j].getText());
            this.Buttons[i - 1][j].setText(tmp);
            this.Buttons[i - 1][j].setEnabled(true);
            this.Buttons[j].setEnabled(false);
            if(this.isSucceed())
                JOptionPane.showConfirmDialog(this, "You Win!!!!");
            return;
        }
        if((i + 1 < 4 && j >= 0)&&(!this.Buttons[i + 1][j].isEnabled()))
        {
            String tmp = this.Buttons[j].getText();
            this.Buttons[j].setText(this.Buttons[i + 1][j].getText());
            this.Buttons[i + 1][j].setText(tmp);
            this.Buttons[i + 1][j].setEnabled(true);
            this.Buttons[j].setEnabled(false);
            if(this.isSucceed())
                JOptionPane.showConfirmDialog(this, "You Win!!!!");
            return;
        }



    }

    // Variables declaration - do not modify
    // End of variables declaration

}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2