欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

chess15.java

有关java的源程序,为讲授java程序设计课程使用
JAVA
字号:
import java.awt.*;
import java.util.*;
import java.applet.*;

public class Chess15 extends Applet
{
	Table table=new Table();
	Button start=new Button("Start");
	Display display=new Display();
	Showtime showtime;
	boolean already=false;
	public void init()
	{
		setLayout(null);
		resize(200,200);
		add(table);
		table.reshape(50,40,110,100);
		table.hideChess("");
		Font msgFnt=new Font("TimesRoman",Font.ITALIC,25);
		start.setFont(msgFnt);
		add(start);
		start.reshape(30,150,140,130);
		display.setBackground(Color.green);
		add(display);
		display.reshape(10,5,190,30);
		display.size=28;
		display.showMessage("Welcome to Use !");
	}
	public boolean action(Event evt,Object what)
	{
		if(evt.target instanceof Button)
		{
			String targetLabel=(String)what;
			if(targetLabel.equals("Start"))
			{
				table.startChess();
				if(showtime !=null)
					showtime.stop();
				showtime=new Showtime(display);
				showtime.start();
				already=true;
			}
			else if(already)
			{
				table.moveChess(targetLabel);
				if(table.rightChess())
				{
					display.size=23;
                                        display.showMessage("Clever ! Used:"+String.valueOf(showtime.usedsecond)+"s");
					showtime.stop();
					already=false;
				}
			}
			return true;
		}
		else return false;
	}
}
class Table extends Panel
{
	Button keyboard[]=new Button[16];
	int fx[]={-4,-1,1,4};
	int opfx[]={3,2,1,0};
        Table()
	{  char ch;
	   int i,t;
	   setLayout(new GridLayout(4,4,0,0));
	   ch='A';
   	   for(i=0; i<15;i++,ch++)
	   {
                keyboard[i]=new Button(String.valueOf(ch));
		add(keyboard[i]);
	   }		
	   keyboard[15]=new Button("");
	   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[i].getLabel();
		if(keyboard[i].getLabel().equals(st))
		    return i;
	    }	
	    return -1;
	}
        boolean rightChess()
	{
	   int i;
	   char  ch='A';
	   for(i=0;i<15;i++,ch++)
	   {
                if(!keyboard[i].getLabel().equals(String.valueOf(ch)))
		    return false;
	   }
	   return true;
	}
	boolean hideChess(String st)
	{
	   int i;
	   i=whichChess(st);
	   if(i !=-1)
	    {
		keyboard[i].hide();
		return true;
	    }	
	    else
		return false;
	}
	boolean showChess(String st)
	{
	   int i;
	   i=whichChess(st);
	   if(i !=-1)
	   {
		keyboard[i].show();
		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[i];
		if(rightMove(location,newlocation))
		{
			if(keyboard[newlocation].getLabel().equals(""))
			{
                                keyboard[newlocation].setLabel(keyboard[location].getLabel());
                                keyboard[location].setLabel("");
				keyboard[location].hide();
				keyboard[newlocation].show();
				return true;
			}
		}
	    }	
		return false;	
	}
	void test()
	{
	   int i;
	   for(i=0; i<100;i++)
	   {  delay(10000000);
	      keyboard[1].show();
	      delay(1000000);		
	     keyboard[1].hide(); 	
	   }	
	
	}
	private boolean rightMove(int location, int newlocation)
	{
		if(rightLocation(newlocation) && ((location/4==newlocation/4)||(location %4 ==newlocation %4)))
		   return true;
		else
		   return false;
	}
	void startChess()
	{
	  int i,location,newlocation,way,time,oldway;
	  char ch;
	  ch='A';
	  for(i=0; i<15;i++,ch++)
	  {
		keyboard[i].setLabel(String.valueOf(ch));
                keyboard[i].show();
	  } 
	  keyboard[15].setLabel("");
	  keyboard[15].hide();
	  location=15;
	  oldway= -1;
	  delay(20000000);
	  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].setLabel(keyboard[newlocation].getLabel());
		keyboard[location].show();
		keyboard[newlocation].setLabel(""); 	
		keyboard[newlocation].hide();
		location=newlocation;
		delay(10000000);
	  }
	}
}
class Display extends Canvas
{
   String message="Welcome !";
   public int size;
   void showMessage(String st)		
   {  message =new String(st);
      repaint();	
   }
   public void paint(Graphics g)
   {
	Font msgFnt= new Font("TimesRoman",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.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)
		{
		}
	  }
	}
}

⌨️ 快捷键说明

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