《Java语言程序设计》大做业报告 九宫格游戏

 

 

 

 

 

 

 

 

Java语言程序设计》大做业报告java

 

 

中国石油大学(北京)2015 — 2016 学年第二学期算法

 

 

班级:_____计算机14-1_______dom

姓名:_____   _________________ide

学号:______2014011329___________函数

 

 

 

  1. 题意分析

程序首先须要九个能够移动的格子,大小相等,有字符串标示,其次要能够相应鼠标和键盘方向键的控制,能够自由移动,而且与此同时记录步数,最后在知足条件时弹出对话框并显示步数以及是否打破记录,关于打破记录还须要文件的操做。布局

        须要用到事件监听和键盘监听借口和方法,以及按钮的出发,还有文件的读写和对话框等技术。this

 

  1. 程序设计思路

(1) 首先将容器的大小肯定,划分红九个的网格布局。spa

this.setLayout(new GridLayout(3,3));设计

(2) 按照必定的逻辑算法,随机将0~8九个button放到九个网格中。code

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);

                }

            }

        }

}

(3) 设计button的点击事件,和键盘的响应事件(实现方法代码见源程序)

(4) 经过移动button完成游戏响应检查函数弹出成功对话框显示步数和是否打破记录(实现方法代码见源程序)

(5) 点击对话框的再来一次

 

  1. 关键功能说明

(1) 九宫格初始化模块:按照必定算法将0~8随机安放到9个格子中;添加控件添加事件监听。

(2) Button按钮触发事件模块:找出触发的button,找出他的四周有没有空的格子,若是有则交换,没有则不动,交换后判断此状态是否胜利,是否弹出对话框,读出历史最佳纪录,判断是否打破纪录写入文件。

(3) 键盘事件监听模块:找到空的格子,根据触发的按键,判断空格子的反方向有没有button,若是有则交换,没有则不动,交换后判断此状态是否胜利,是否弹出对话框,读出历史最佳纪录,判断是否打破纪录写入文件。

(4) 是否成功模块:依次判断格子的字符串是否分别是1”“2”“3”“4”“5”“6”“7”“8”,是则返回true,不然返回false

 

  1. 运行结果及分析

 

 

由于是字节流储存,因此显示是字符

 

 

  1. 设计经验总结

刚刚开始感受很难,由于对图形界面太不了解了,有的函数和类都不知道,因此超级难入手,可是当看网上的代码和同窗的多了以后也就知道该怎么编了,一旦入手,后面的就感受没有那么难了,如今编完了就感受图形界面也就那么回事,都是固定的模式,模块添加也很简单,虽然仍是有不少不懂的地方可是相比于作大做业以前已经有不少的提升了,还认识了不少不认识的组件,同窗们都用了不一样的方法,我用的是网格布局而后添加button,感受这样界面友好一点。弹出的对话框能够JOptionPane而不是再定义JDialog,文件的读写方法不少,选一种本身理解的就好。

总的来讲学到了不少知识,更加巩固了原本就很薄弱的java

:源程序

package com.Game1;

import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.*;

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 str1="最佳成绩:";
    static JButton[][] Buttons=new JButton[3][3];        //九宫格
    public Game(){            
        super("九宫格游戏");
        this.setBounds(400,100,700,700);
        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);
                }
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {    //鼠标事件监听,交换格子内容步数加1
        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) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if(replay==JOptionPane.YES_OPTION){
                    Game G=new Game();
                }
            }
        }
    }
    public void keyPressed(KeyEvent e){        //键盘事件监听,交换格子内容步数加1
        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) {
                // 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) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if(replay==JOptionPane.YES_OPTION){
                    Game G=new Game();
                }
            }
        }
    }
    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
        
    }
}
相关文章
相关标签/搜索