Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 463|回复: 0

[Java游戏学习]拼图游戏

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-14 19:59:58 | 显示全部楼层 |阅读模式
    程序运行图:

      

      //拼图游戏。
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class ChenWin15 extends JFrame implements ActionListener{
    Table table=new Table();
             JButton start=new JButton("开始");
             JButton end=new JButton("结束");
             Display display=new Display();
             Showtime showtime;
            boolean already=false;

             public  ChenWin15(){
                     Container con=getContentPane();
                     con.setLayout(null);
                     setSize(200,200);
                     con.add(table);
                     addWindowListener(new WindowAdapter(){
                          public void windowClosing(WindowEvent e){
                               System.exit(0);
                          }
                     });
                  //   table.reshape(80,100,110,110);
                     table.setBounds(80,100,200,200);
                     table.hideChess(" ");
                     Font msgFnt=new Font("Dialog",Font.ITALIC,25);
                     start.setFont(msgFnt);
                     end.setFont(msgFnt);
                     start.addActionListener(this);
                     end.addActionListener(this);
                     con.add(start);
                     con.add(end);
                   //  start.reshape(15,350,100,30);
                   //  end.reshape(115,250,100,30);
                    start.setBounds(65,310,100,30);
                    end.setBounds(175,310,100,30);
                     display.setBackground(Color.green);
                     con.add(display);
                  //   display.reshape(20,40,250,30);
                     display.setBounds(80,40,250,30);
                    display.size=28;
                    display.showMessage("移字母游戏!");
              }

             public void actionPerformed(ActionEvent evt){
                     String temp=evt.getActionCommand();  
                      
                      if(temp.equals("结束")){
                                     System.exit(0);
                        if(showtime!=null) showtime=null;
                       }else if(temp.equals("开始")){
       table.startChess();
                                     if(showtime!=null) showtime=null;
    showtime=new Showtime(display);
    showtime.start();
                                     already=true;
                                     
                       }
                      
             }   

             public static void main(String args[]){
                   ChenWin15 aa=new ChenWin15();
                   aa.setTitle("This is ChenWin15");
                   aa.setSize(400,400);
                   aa.setVisible(true);
              }
          

             class Table extends JPanel implements  ActionListener{
                     JButton keyboard[]=new JButton[16];
    int fx[]={-4,-1,1,4};
    int opfx[]={3,2,1,0};
    Table(){
    char ch;
    int i,t;
                         //    setPreferredSize(new Dimension(200,200));
    setLayout(new GridLayout(4,4,0,0));
                             ch="A"
    for(i=0;i<15;i++,ch++){
                                     keyboard=new JButton(String.valueOf(ch));
                                     keyboard.setBackground(Color.red);
                                     keyboard.addActionListener(this);
                                     
    add(keyboard);
    }
                             keyboard[15]=new JButton(" ");
                             keyboard[15].setBackground(Color.red);
                             keyboard[15].addActionListener(this);
    add(keyboard[15]);
    }
    private void delay(int times){
    int i;
    for(i=0;i<times;i++);
    }
    int whichChess(String st){
    int i;
    String ss;
    for(i=0;i<16;i++){
                                     ss=keyboard.getText();
                                     if(keyboard.getText().equals(st)) return i;
    }
    return -1;
    }

    boolean rightChess(){
    int i;
    char ch="A"
    for(i=0;i<15;i++,ch++){
                                     if(!keyboard.getText().equals(String.valueOf(ch))) return false;
    }
    return true;
    }
    boolean hideChess(String st){
    int i;
    i=whichChess(st);
                             if(i!=-1) {keyboard.setVisible(false);return true;}
    else return false;
    }

    boolean showChess(String st){
    int i;
    i=whichChess(st);
                             if(i!=-1) {keyboard.setVisible(true);return true;}
    else return false;
    }

    private boolean rightLocation(int Location){
    if((Location>=0)&&(Location<16)) return true;
    else return false;
    }

    boolean moveChess(String st){
    int location=whichChess(st);
    if(location==-1) return false;
    int i,newlocation;
    for(i=0;i<4;i++){
    newlocation=location+fx;
    if(rightMove(location,newlocation)){
                                             if(keyboard[newlocation].getText().equals(" ")){
                                                     keyboard[newlocation].setText(
                                                             keyboard[location].getText());
                                                     keyboard[location].setText(" ");
                                                     keyboard[location].setVisible(false);
                                                     keyboard[newlocation].setVisible(true);
    return true;
    }
    }
    }
    return false;
    }

    void test(){
    int i;
    for(i=0;i<100;i++){
    delay(10000000);
                                     keyboard[1].setVisible(true);
    delay(10000000);
                                     keyboard[1].setVisible(false);
    }
    }

    private boolean rightMove(int location,int newlocation){
    if(rightLocation(newlocation)&&
    ((location/4==newlocation/4)||(location%4==newlocation%4)))
    return true;
    else return false;
    }


                     public void actionPerformed(ActionEvent e){
                          JButton temp=(JButton)e.getSource();
                          String str=temp.getText();
                        if(already){
                          moveChess(str);
                          if(rightChess()){
                             System.out.println("You are win");
                                       display.size=23;
                                       display.showMessage("成功! Used:"+
                                       String.valueOf(showtime.usedsecond)+"s");
                                          showtime.stop();
                                          already=false;
                         }
                       }

                      }

    void startChess(){
    int i,location,newlocation,way,time,oldway;
    char ch;
    ch="A"
    for(i=0;i<15;i++,ch++){
                                     keyboard.setText(String.valueOf(ch));
                                     keyboard.setVisible(true);
    }
                             keyboard[15].setText(" ");
                             keyboard[15].setVisible(false);
    location=15;
    oldway=-1;
                             delay(200000);
    time=40+(int)(Math.random()*70);
    for(i=0;i<time;i++){
    do{
    way=(int)(4*Math.random());
    newlocation=location+fx[way];
    }while(!rightMove(location,newlocation)||way==oldway);
    oldway=opfx[way];
                                     keyboard[location].setText(
                                             keyboard[newlocation].getText());
                                     keyboard[location].setVisible(true);
                                     keyboard[newlocation].setText(" ");
                                     keyboard[newlocation].setVisible(false);
    location=newlocation;
                                     delay(10000);
    }
    }
          }
    }





    class Display extends JPanel{
    String message="移字母游戏";
    public int size;
    void showMessage(String st){
    message=new String(st);
    repaint();
    }

             public void paintComponent(Graphics g){
                     super.paintComponent(g);
                     Font msgFnt=new Font("Dialog",Font.ITALIC,size);
    g.setFont(msgFnt);
    g.setColor(Color.red);
                     g.drawString(message,5,(30+size-6)/2);
    }
    }

    class Showtime extends Thread{
    long usedsecond=0;
    int oldhour,oldminute,oldsecond;
    Display display;
    Showtime(Display screen){
    super();
    Date timenow=new Date();
    oldhour=timenow.getHours();
    oldminute=timenow.getMinutes();
    oldsecond=timenow.getSeconds();
    display=screen;
    display.size=35;
    }

    public void run(){
    Date timenow;
    while(true){
    timenow=new Date();
    usedsecond=(timenow.getHours()-oldhour)*3600l+
    (timenow.getMinutes()-oldminute)*60l+
    timenow.getSeconds()-oldsecond;
    display.showMessage("Time:"+String.valueOf(usedsecond/60)+
    ":"+String.valueOf(usedsecond%60));
    try{
    Thread.sleep(1000);}
    catch(InterruptedException e) {}
    }
    }
    }



    源码下载:http://file.javaxxz.com/2014/10/14/195958199.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-6-19 17:37 , Processed in 0.315409 second(s), 38 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表