⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randtest.java

📁 16方格排序游戏设计,用java编写的.绝对源码.
💻 JAVA
字号:
import java.lang.Math;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;

public class randtest 
{
	public static void main(String args[])
	{
		MainFrame frame=new MainFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}


//定义一个框架类
 class MainFrame extends JFrame
 {
	private JButton button[];                      //Button数组
	private int record[];

	public MainFrame()
	{
		super("排序游戏");
		Container container=getContentPane();
		record=new int[16];                            //用来跟踪记录15个数字在Button上的位置
		button=new JButton[16];
    	container.setLayout(new GridLayout(4,4));      //布局为网格型,4*4,共16个格
		ActionListener command=new ClickAction();
		int i;
		int temp;
	    for(i=0;i<16;i++)
		      record[i]=-1;                             //record[]记录每个键上的数值
		for(i=0;i<16;i++)
		 {
		 	temp=(int)(Math.random()*16);               //获得一个随机数,用于确定i在数组中的位置
		 	while(record[temp]!=-1)                     //temp为数组的下标.
		 	  { 
		 	  	temp++;
		 	  	if(temp==16) temp=0;
		 	  }
		 	record[temp]=i;
		 }

		for(i=0;i<16;i++)
		 {
		 	if(record[i]==0)                         //值为0的button对应的上面为空
		 	     {
		 	     	button[i]=new JButton("");
		 	       	button[i].addActionListener(command);
		 	     }
		 	else
		 	{
		       button[i]=new JButton(Integer.toString(record[i]));
		       button[i].addActionListener(command);
		    }
          }
		 
		for(i=0;i<16;i++)
		   container.add(button[i]);                       //添加button到布局中
		 setSize(250,250);
   }//end_construction function.

	
   private class ClickAction implements ActionListener 
	{
    
		public void actionPerformed(ActionEvent evt)
		{
			int value,i,loc,pos;//temp
			loc=0;
			pos=0;
	       if(evt.getActionCommand()=="")
	       {
	           if(Issucceed())
	             {
	             	 JOptionPane.showMessageDialog(null,"哈哈,你成功了");  //弹出消息框
	             	 reset();
	              }
	           return;
	      	}  
			try
			{
              value=Integer.valueOf(evt.getActionCommand()).intValue(); //得到事件源字符串
		   	   //查找当前值与空键在方格中的位置
		   	   for(i=0;i<16;i++)
			    {
				  if(record[i]==value) loc=i;           //查询该值的键在方格中相应的位置
				  if(record[i]==0) pos=i;               //查询空格键在方格中的位置
		    	}
			
			    if(pos==loc+1||pos==loc-1||pos==loc+4||pos==loc-4)   //判断是否在相邻的位置
			   {
				  record[pos]=record[loc];
				  record[loc]=0;                                //值互换
				  button[pos].setText(evt.getActionCommand());  //重设button上的值
				  button[loc].setText("");
		
			   }
		      if(Issucceed())                       //已经全部有序了
		      {
			  	   JOptionPane.showMessageDialog(null,"哈哈,你成功了");  //弹出消息框
		    	   reset();                       //重新设置界面
		       }
			   else return;
	
			}//end_try{}
			 catch(Exception e)
			 {
			 	JOptionPane.showMessageDialog(null,e.toString()); 
			 }
			
		}//end_actionperformed()
        
        //判断是否已经排序成功的函数
        public boolean Issucceed()
        {
        	int i;
        	boolean tag1,tag2;
        	tag1=true;
        	tag2=true;
        	for(i=0;i<15;i++)                  //判断顺序
			  if(record[i]!=i+1)
			   	tag1=false;
			  
		     
		     //下面判断是否是逆序
		  for(i=0;i<15;i++)
		    if((record[i]+i)!=15)
		       tag2=false;
					 
			return tag1|tag2;
			        
        }//end_Issucceed()
        
       //给界面重新设定的函数
        public void reset()
        {
            int i,temp;
        	for(i=0;i<16;i++)
		       record[i]=-1;             //先给数组定初始值
			for(i=0;i<16;i++)          //重新设定屏幕的顺序
		    {
		 	    temp=(int)(Math.random()*16);               //获得一个随机数,用于确定i在数组中的位置
		 	     while(record[temp]!=-1) 
		 	     { 
		 	  	      temp++;
		 	  	      if(temp==16) temp=0;
		 	     }
		        record[temp]=i;     //给temp下标的数组赋值
		    }
		    for(i=0;i<16;i++)                           //重新设定button上的数字
		    {
		         if(record[i]==0) button[i].setText("");
		         else button[i].setText(Integer.toString(record[i]));   
		   }
        }//end_reset()	
	}//end_ClickAction
}//end_mainFrame

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -