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

📄 wuzhiapplet.java

📁 1.用applet实现一个简单的网络五子棋
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			 parent.list_user.clear();///清空用户列表
		     try
		     {parent.listen.stop();
		      parent.listen=null;
		      parent.soc.close();
			  parent.soc=null;
			  
             }catch(IOException e)
             {
             	System.out.println("Error:"+e);
             	}
			  
			 
			 return;
		 }
	  }

 }     //end of run method
}      //end of Listen inner class


 


///////////////////////////////////////////////五字棋楼

class wuWindow extends Frame implements MouseListener

{		//WuzhiApplet listen=new WuzhiApplet();
		int[] x=new int[41];
		int[] y=new int[41];
		int [][] piece=new int[16][16];
		static int f=5;
		int num=0;///控制下的局数
		int side=0;///控制你是哪一方0 白方1黑方
		Point point;
		Font font=new Font("Fixedsys",Font.BOLD,20);
	Button button1=new Button("开始");
	Button button2=new Button("离开");
	TextField white_user= new TextField(20);//白方
	TextField black_user= new TextField(20);//黑方
	Panel  panel1=new Panel();
	Panel  panel2=new Panel();
	Canvas canvas1=new Canvas();
	//////Graphics g;
	Color  color=Color.pink;
	////以下用于网络的变量//
	//PrintStream dous;
	String socket;///用于存放对方的地址信息
	String line;///用于存入socket,x,y;
	StringBuffer message=new StringBuffer("WUZHI:");
	////用于存放发送给对放的数据,
	/////构成 :WUZHI:socket(USER:IP:PORT):X:Y:F
	public wuWindow()
	{
		panel2=new Panel();
		x=new int[41];
		y=new int[41];
		button1=new Button("开始");
		button2=new Button("离开");
		try{
			init();
		
		}catch(Exception e){e.printStackTrace();}
	}/////wuWindow over!!!!

	
	private void init() //初始化程序
	{
		

		this.setTitle("五子棋");
		//this.setSize(740,550);//
		resize(500,400);
		  setBackground(new Color(0,110,0));
		this.setLayout(new BorderLayout());
		this.add(panel2,BorderLayout.EAST);
		panel2.setBackground(Color.red);
		panel2.setLayout(new GridLayout(6,1));
		panel2.setFont(new Font("Fixedsys",Font.BOLD,15));
		panel2.add(new Label("--白     方--"));
		panel2.add(white_user);
		panel2.add(new Label("--黑     方--"));panel2.add(black_user);
		panel2.add(button1);panel2.add(button2);
		white_user.setEditable(false);black_user.setEditable(false);
		white_user.setText("--------");black_user.setText("--------");
		addWindowListener(new WindowAdapter(){
		public void windowClosing(WindowEvent e){
		setTitle("五子棋");
		///////////////////////
		//关闭窗口时做相应的处理
		////////////////////////////////
		setVisible(false);
		}});
	
	for (int x=0;x<16 ;x++ )
	for (int y=0;y<16;y++)
	{
		piece[x][y]=0;
	}
	
	button1.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
		if (f==4)
		{//没有实现下一轮下棋谁开始下
		  num++;
		  if(num%2==1&&side==1){f=0;}else if(num%2==1&&side==0){f=2;}
		  else if(num%2==0&&side==1){f=1;}else{f=0;} 
			///奇数局且我是黑f=0
			///奇数避且我是白f=2
			///偶数局且我是黑f=1
			///偶数局且我是白f=0
			for (int x=0;x<16 ;x++ )
			for (int y=0;y<16;y++)
			{piece[x][y]=0;}
			repaint();			
		}else
		if (f==3)
		{num++;
		  if(num%2==1&&side==1){f=0;}else if(num%2==1&&side==0){f=2;}
		  else if(num%2==0&&side==1){f=1;}else{f=0;} 
			for (int x=0;x<16 ;x++ )
			for (int y=0;y<16;y++)
			{piece[x][y]=0;}
			repaint();			
		}	
	}});
	
	//connect();
	addMouseListener(this);
	}///init()  over!!!
	
	public void paint(Graphics g)/////////////棋盘处理
	{  
		x[0]=50;
		y[0]=50;
		 // Font font=new Font("Fixedsys",Font.BOLD,40);
		try{
		for( int i=1;i<=40;i++)
			{
			x[i]=x[i-1]+30;
			y[i]=y[i-1]+30;
			}
			}catch(Exception e){} 
			
	//画棋盘
		 g.setColor(Color.pink); 
		g.fillRect(20,20,x[15]+15,y[15]+15);
		g.setColor(Color.black); 
//
		for(int i=0;i<16;i++)
		{
		g.drawLine(x[i],y[0],x[i],y[15]);
		g.drawLine(x[0],y[i],x[15],y[i]);
		}

	///////////////// deal shut 
	
	try{//白色棋子
		g.setColor(Color.white);
		for(int k=0;k<16;k=k+1)
		for(int i=0;i<16;i++)
		{  if(piece[k][i]==2)
			g.fillOval(k*30+36,i*30+36,28,28);	
		}
		if(f==4)
			{g.setFont(font);
				g.drawString("WHITE WINNER!!",200,40);
			}
		//黑色棋子
		g.setColor(Color.black);
		for(int k=0;k<16;k=k+1)
		for(int i=0;i<16;i++)
		{if(piece[k][i]==1)
		g.fillOval(k*30+36,i*30+36,28,28);
		
		}if(f==3)
			{
				g.setFont(font);
				g.drawString("BLACK WINNER!!",200,40);
			}
		}catch(Exception e){}
	
	}//////////////////paint() over

public void Winner(int c,int d,int e)////判断输赢
	{
	int i,j;i=c;j=d;int[] n=new int[4];

	while(i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[0]=n[0]+1;j--;}
	j=d+1;
	while (i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[0]=n[0]+1;j++;}
	i=c;j=d;
	//////////////////////////////
	while(i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[1]=n[1]+1;i--;}
	i=c+1;
	while (i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[1]=n[1]+1;i++;}
	i=c;j=d;
	//////////////////////////////
	while(i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[2]=n[2]+1;i--;j--;}
	i=c+1;j=d+1;
	while (i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[2]=n[2]+1;i++;j++;}
	i=c;j=d;
	//////////////////////////////
	while(i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[3]=n[3]+1;i--;j++;}
	i=c+1;j=d-1;
	while (i>=0&&i<16&&j>=0&&j<16&&piece[i][j]==e)
	{n[3]=n[3]+1;i++;j--;}
	//i=c;j=d;
	//////////////////////////////
	if(n[0]>=5||n[1]>=5||n[2]>=5||n[3]>=5)
	{
		if(e==1)
		{f=e=3;//return(f);
		}else{f=e=4;}
	}
	}////Winner()  over!!!!
public void huaYuan(int c,int d,int e)
	{///num++;
	int a=c*30+50;  
	int b=d*30+50;
	Graphics h=getGraphics();
		if (e==1)
		{   piece[c][d]=1;
			h.setColor(Color.black);
			//h.drawString(c+"",100,100);
			//h.drawString(d+"",100,125);
			h.fillOval(a-14,b-14,28,28);
			Winner(c,d,e);
			if(f==3)
			{   h.setFont(font);
				h.drawString("BLACK WINNER!!",200,200);
			}else
			{f=0;}
			
		}else
		if (e==2)
		{   piece[c][d]=2;
			h.setColor(Color.white);
			//h.drawString(c+"",100,100);
			//h.drawString(d+"",100,125);
			h.fillOval(a-14,b-14,28,28);
			Winner(c,d,e);
			if(f==4)
			{ h.setFont(font);
				h.drawString("WHITE WINNER!!",200,200);
			}else
			{f=0;}
			
		}

	}///////huanyuan()  over!!!!
	public void mouseClicked(MouseEvent mouseevent)
			//鼠标响应得出坐标值
	{
		//Graphics h=getGraphics();
		point=mouseevent.getPoint();
		int c=(int)((point.x-50)/30.0+0.5);
		int d=(int)((point.y-50)/30.0+0.5);
	 if(f==2){socket=black_user.getText();}
	 ///socket存放对方的地址信息!!!!!!
	 if(f==1){socket=white_user.getText();}
	 if(!socket.equals(null)&&(f==1||f==2))
		{
		if (point.x>45&&point.y>45&&c>=0&&c<16&&d>=0&&d<16&&piece[c][d]==0)
		{
			
		  String message=socket+":"+f+":"+c+":"+d;
			StringBuffer string=new StringBuffer("QIZI:");
		string.append(message);
	   Listen.send(string);
//		try{Thread.sleep(10);}catch(Exception e){}
	 // System.out.println("586:"+message);
			huaYuan(c,d,f);
			
			}
		}
		
	}
	public void mouseEntered(MouseEvent mouseevent){}
	public void mouseExited(MouseEvent mouseevent){}
	public void mousePressed(MouseEvent mouseevent){}
	public void mouseReleased(MouseEvent mouseevent){}

};//class over!!!!!

⌨️ 快捷键说明

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