Java_Class 16方格拼图游戏

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        GameFrame frame = new GameFrame();
        frame.setTitle("Game");
    }

}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;
import javax.swing.border.LineBorder;

public class GameFrame extends JFrame {
    /**
     * 菜单栏
     */
    JMenuBar menuBar;
    /**
     * "菜单"选项
     */
    JMenu menu;
    /**
     * "帮助"选项
     */
    JMenu help;
    /**
     * "重新开始"选项
     */
    JMenuItem mItem1;
    /**
     * "退出"选项
     */
    JMenuItem mItem2;
    /**
     * "关于"选项
     */
    JMenuItem hItem;
    /**
     * 游戏面板
     */
    JPanel panel;
    /**
     * 游戏中的按钮
     */
    JButton buttons[] = new JButton[16];
    /**
     * 按钮监听器
     */
    GameListener listener;
    /**
     * 背景颜色
     */
    Color backgruond = Color.DARK_GRAY;
    /**
     * 前景颜色
     */
    Color foreground = Color.WHITE;
    /**
     * 游戏中的字体
     */
    Font font = new Font("Consolas", Font.PLAIN, 14);
    
    public GameFrame() {
        // TODO Auto-generated constructor stub
        setSize(500, 520);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        menuBar = new JMenuBar();
        menu = new JMenu("Menu");
        menu.setForeground(foreground);
        menu.setFont(font);
        mItem1 = new JMenuItem("Restart");
        mItem1.setFont(font);
        mItem1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                init();
            }
        });
        mItem2 = new JMenuItem("Exit");
        mItem2.setFont(font);
        mItem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menu.add(mItem1);
        menu.add(mItem2);
        help = new JMenu("Help");
        help.setForeground(foreground);
        help.setFont(font);
        hItem = new JMenuItem("About");
        hItem.setFont(font);
        hItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Maked by : 胡靖");
            }
        });
        help.add(hItem);
        menuBar.add(menu);
        menuBar.add(help);
        menuBar.setBackground(backgruond);
        setJMenuBar(menuBar);

        panel = new JPanel();
        panel.setLayout(new GridLayout(4, 4));
        add(panel);

        setTitle("Loading...");
        listener = new GameListener(buttons);
        for (int i = 0; i < 16; i++) {
            if (i == 15)
                buttons[i] = new JButton(" ");
            else
                buttons[i] = new JButton("" + (i + 1));
            buttons[i].setBackground(backgruond);
            buttons[i].setForeground(foreground);
            buttons[i].setBorder(new LineBorder(Color.GRAY));
            buttons[i].setFont(new Font("Consolas", Font.PLAIN, 40));
            buttons[i].addActionListener(listener);
            panel.add(buttons[i]);
        }
        gameOn();

        validate();

    }
    /**
     * 初始话第一次游戏,并有动态初始化效果
     */
    public void gameOn() {
        for (int i = 0; i < 16; i++)
            buttons[i].setEnabled(false);
        menu.setEnabled(false);
        help.setEnabled(false);
        Random random = new Random();
        int d[] = { -4, 1, -1, 4 };
        int p = 15;
        for (int i = 0; i < 1000; i++) { // 打乱的次数
            int tmp = random.nextInt(4);
            int next = p + d[tmp];
            if (p % 4 == 0 && next % 4 == 3 || p % 4 == 3 && next % 4 == 0)
                continue;
            if (next >= 0 && next < 16) {
                String s = buttons[p].getText();
                buttons[p].setText(buttons[next].getText());
                buttons[next].setText(s);
                p = next;
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        judge();
        for (int i = 0; i < 16; i++)
            buttons[i].setEnabled(true);
        menu.setEnabled(true);
        help.setEnabled(true);
    }
    /**
     * 重新开始游戏的初始化,与第一次游戏使用的初始化方式不同
     */
    public void init() {
        for (int i = 0; i < 16; i++)
            buttons[i].setEnabled(false);
        Random random = new Random();
        boolean flag[] = new boolean[16];
        for (int i = 0; i < 16; i++) {
            int tmp = random.nextInt(16);
            while (flag[tmp])
                tmp = random.nextInt(16);
            if (tmp == 15)
                buttons[i].setText(" ");
            else
                buttons[i].setText("" + (tmp + 1));
            flag[tmp] = true;
        }
        judge();
        for (int i = 0; i < 16; i++)
            buttons[i].setEnabled(true);
    }
    /**
     * 判断生成的序列是否可行,如果不可行交换任意相邻两个按钮的文本即可
     */
    public void judge() {
        int sum = 0;
        int a[] = new int[16];
        for (int i = 0; i < 16; i++) {
            String s = buttons[i].getText();
            if (s.equals(" "))
                a[i] = 0;
            else
                a[i] = Integer.parseInt(s);
        }
        for (int i = 0; i < 15; i++) {
            if (a[i] == 0) {
                sum += i % 4;
                sum += i / 4;
                continue;
            }
            for (int j = i + 1; j < 16; j++)
                if (a[i] > a[j])
                    sum++;
        }
        if (sum % 2 == 0) {
            String s = buttons[0].getText();
            buttons[0].setText(buttons[1].getText());
            buttons[1].setText(s);
        }
    }
}
import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class GameListener implements ActionListener {
    /**
     * 游戏中的按钮,传进来的参数
     */
    JButton[] button = new JButton[16];
    /**
     * 四个方向
     */
    int[] d = { -4, 1, -1, 4 };

    public GameListener(JButton[] button) {
        // TODO Auto-generated constructor stub
        this.button = button;
    }
    /**
     * 监听到动作后执行的方法
     */
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        JButton jButton = (JButton) e.getSource();
        if (jButton.getText().equals(" "))
            return;
        int position = 0;
        for (int i = 0; i < 16; i++) {
            if (jButton == button[i]) {
                position = i;
                break;
            }
        }
        for (int i = 0; i < 4; i++) {
            int next = position + d[i];
            if (position % 4 == 0 && next % 4 == 3 || position % 4 == 3 && next % 4 == 0)
                continue;
            if (next >= 0 && next < 16 && button[next].getText().equals(" ")) {
                String s = button[position].getText();
                button[position].setText(button[next].getText());
                button[next].setText(s);
                break;
            }
        }
        boolean win1 = true;
        boolean win2 = true;
        if (!button[15].getText().equals(" "))
            win1 = false;
        for (int i = 0; i < 15 && win1; i++)
            if (!button[i].getText().equals("" + (i + 1)))
                win1 = false;
        if (!button[0].getText().equals(" "))
            win2 = false;
        for (int i = 1; i < 16 && win2; i++)
            if (!button[i].getText().equals("" + (16 - i)))
                win2 = false;
        if (win1 || win2) {
            for (int i = 0; i < 16; i++)
                button[i].setEnabled(false);
            JOptionPane.showMessageDialog(null, "Congratulations!\nYou win.");
        }
    }

}

 

posted @ 2016-12-27 21:52  Pacify  阅读(908)  评论(0编辑  收藏  举报