九宫格——排序玩法

package com.zss.java.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;

/** * 九宫格:实现对1~8排序 * @author lemon * @date 2018/9/12 16:31 */
public class Game extends JFrame implements ActionListener,KeyListener {
    int number_path = 0; //记录步数
    int best_recond = 0; //最佳成绩
    int replay = 0;
    int[] num = {1,2,3,4,5,6,7,8,0};
    String str = "已走步数:";
    String string = "最佳成绩:";
    static JButton[][] buttons = new JButton[3][3];  //九宫格

    public Game(){
        super("九宫格游戏");
        this.setBounds(100,12,200,200);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(3,3));
        this.init();
        this.setVisible(true);
    }
    /** * 初始化使用算法使其随机出现,并填加格子以及事件监听和键盘监听 */
    public void init(){
        for(int i = 0;i < 8;i++)
        {
            this.num[i] = i+1;
        }
        this.num[8] = -1;
        this.num[5] = -1;
        this.num[8] = 6;
        for(int i = 0;i < 8;i++)
        {
            int idx =(int) (Math.random() * 8);
            int tmp = this.num[7];
            this.num[7] = this.num[idx];
            this.num[idx] = tmp;
        }
        for(int i = 0;i < 3;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                if(this.num[i * 3 + j] != -1)
                {
                    Game.buttons[i][j] =  new JButton("" + this.num[i * 3 + j]);
                    Game.buttons[i][j].addActionListener(this);
                    Game.buttons[i][j].addKeyListener(this);
                    this.getContentPane().add(Game.buttons[i][j]);
                }
                else
                {
                    Game.buttons[i][j] =  new JButton("");
                    Game.buttons[i][j].addActionListener(this);
                    Game.buttons[i][j].addKeyListener(this);
                    this.getContentPane().add(Game.buttons[i][j]);
                    Game.buttons[i][j].setEnabled(false);
                }
            }
        }
    }
    /** * 鼠标事件监听,交换格子内容步数加1 * @param e */
    @Override
    public void actionPerformed(ActionEvent e) {
        int i,j;
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                if(e.getSource()==buttons[i][j]){
                    if(i+1!=3&&buttons[i+1][j].getText()==""){
                        buttons[i+1][j].setText(buttons[i][j].getText());
                        buttons[i+1][j].setEnabled(true);
                        buttons[i][j].setText("");
                        buttons[i][j].setEnabled(false);
                        number_path++;
                    }
                    if(i-1!=-1&&buttons[i-1][j].getText()==""){
                        buttons[i-1][j].setText(buttons[i][j].getText());
                        buttons[i-1][j].setEnabled(true);
                        buttons[i][j].setText("");
                        buttons[i][j].setEnabled(false);
                        number_path++;
                    }
                    if(j+1!=3&&buttons[i][j+1].getText()==""){
                        buttons[i][j+1].setText(buttons[i][j].getText());
                        buttons[i][j+1].setEnabled(true);
                        buttons[i][j].setText("");
                        buttons[i][j].setEnabled(false);
                        number_path++;
                    }
                    if(j-1!=-1&&buttons[i][j-1].getText()==""){
                        buttons[i][j-1].setText(buttons[i][j].getText());
                        buttons[i][j-1].setEnabled(true);
                        buttons[i][j].setText("");
                        buttons[i][j].setEnabled(false);
                        number_path++;
                    }
                }
            }
        }
        if(winfail()){    //判断这次操作后是否胜利,读出最佳成绩,并判断是否打破记录
            try {
                FileInputStream  fin=new FileInputStream("bestrecond.int");
                DataInputStream din=new DataInputStream(fin);
                best_recond=din.readInt();
                din.close();
                fin.close();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(number_path>best_recond){
                JOptionPane.showConfirmDialog(this,"你太厉害了!!这么难都能赢!!!才用了"+Integer.toString(number_path)+"步!!然而并没有打破记录。历史最佳成绩是:"+Integer.toString(best_recond));
                if(replay==JOptionPane.YES_OPTION){
                    Game G=new Game();
                }
            }
            else{
                JOptionPane.showConfirmDialog(this,"你太厉害了!!这么难都能赢!!!才用了"+Integer.toString(number_path)+"步!!打破了历史记录!!历史最佳成绩是:"+Integer.toString(best_recond));
                try {
                    FileOutputStream  fin=new FileOutputStream("bestrecond.int");
                    DataOutputStream din=new DataOutputStream(fin);
                    din.writeInt(number_path);
                    din.close();
                    fin.close();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                if(replay==JOptionPane.YES_OPTION){
                    Game G=new Game();
                }
            }
        }
    }
    /** * 键盘事件监听,交换格子内容步数加1 * @param e */
    public void keyPressed(KeyEvent e){
        int i,j;
        boolean b=false;
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                if(buttons[i][j].getText()==""){
                    if(i+1!=3&&e.getKeyCode()==KeyEvent.VK_UP){
                        buttons[i][j].setEnabled(true);
                        buttons[i][j].setText(buttons[i+1][j].getText());
                        buttons[i+1][j].setText("");
                        buttons[i+1][j].setEnabled(false);
                        number_path++;
                        b=true;break;
                    }
                    if(i-1!=-1&&e.getKeyCode()==KeyEvent.VK_DOWN){
                        buttons[i][j].setEnabled(true);
                        buttons[i][j].setText(buttons[i-1][j].getText());
                        buttons[i-1][j].setText("");
                        buttons[i-1][j].setEnabled(false);
                        number_path++;
                        b=true;break;
                    }
                    if(j+1!=3&&e.getKeyCode()==KeyEvent.VK_LEFT){
                        buttons[i][j].setEnabled(true);
                        buttons[i][j].setText(buttons[i][j+1].getText());
                        buttons[i][j+1].setText("");
                        buttons[i][j+1].setEnabled(false);
                        number_path++;
                        b=true;break;
                    }
                    if(j-1!=-1&&e.getKeyCode()==KeyEvent.VK_RIGHT){
                        buttons[i][j].setEnabled(true);
                        buttons[i][j].setText(buttons[i][j-1].getText());
                        buttons[i][j-1].setText("");
                        buttons[i][j-1].setEnabled(false);
                        number_path++;
                        b=true;break;
                    }
                }
            }
            if(b) break;
        }
        if(winfail()){//判断这次操作后是否胜利,读出最佳成绩,并判断是否打破记录
            try {
                FileInputStream fin=new FileInputStream("bestrecond.int");
                DataInputStream din=new DataInputStream(fin);
                best_recond=din.readInt();
                din.close();
                fin.close();
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            if(number_path>best_recond){
                JOptionPane.showConfirmDialog(this, "你太厉害了!!这么难都能赢!!!才用了"+Integer.toString(number_path)+"步!!然而并没有打破记录。历史最佳成绩是:"+Integer.toString(best_recond));
                if(replay==JOptionPane.YES_OPTION){
                    Game G=new Game();
                }
            }
            else{
                JOptionPane.showConfirmDialog(this, "你太厉害了!!这么难都能赢!!!才用了"+Integer.toString(number_path)+"步!!打破了历史记录!!历史最佳成绩是:"+Integer.toString(best_recond));
                try {
                    FileOutputStream fin=new FileOutputStream("bestrecond.int");
                    DataOutputStream din=new DataOutputStream(fin);
                    din.writeInt(number_path);
                    din.close();
                    fin.close();
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                if(replay==JOptionPane.YES_OPTION){
                    Game G=new Game();
                }
            }
        }
    }

    /** * 判断输赢函数,每个格的字符都符合即可 * @return */
    public static boolean winfail(){
        int i,j,k=1,n=0;
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                if(buttons[i][j].getText().equals(Integer.toString(k++))){
                    n++;
                }
            }
        }
        if(n>=8) return true;
        return false;
    }
    //游戏开始入口
    public static void main(String[] args) {
        Game G=new Game();
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }
}
    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/zssapple/article/details/82685857
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞