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

📄 clientchess.java

📁 基于java语言编程实现的简单ftp协议源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		for(i=1;i<=r;i++)
		{
			int j=1;
			for(j=1;j<=c;j++)
			{
				ppoint[i][j]=new C_ChessPoint(i*unitWidth,j*unitHeight,false);
			}
		}
		try
		{
			output=new DataOutputStream(clientOne.soc.getOutputStream());    //创建输入输出流的实例
			input = new DataInputStream(clientOne.soc.getInputStream());
		}
		catch(IOException ee)
		{
		}	
	}
	
     //发送数据的方法		
     public void sendData( String messageM)
     {
     	if(messageM.charAt(0)>'o'||messageM.charAt(0)<'a')
     	{
            try
            {
                 output.writeUTF( "\n客户端:" + messageM);//向服务器发送数据
                 output.flush();
                 displayArea.append( "\n客户端:" + messageM);
            }       
            catch ( IOException ioException )
           {
                displayArea.append( "\n传输错误!" );
           }
        }
      }
	
      //关闭连接的方法	
      public  void closeConnection() 
     {
     	try
     	{   
            output.close();       
            input.close();
            clientOne.soc.close();
        }
        catch(IOException ed)
        {        
        }
     }
	
	//绘制棋盘
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
						
		int j=1;              //画棋盘的横格线      
		for(j=1;j<=yy;j++)		   
		{			
			g.drawLine(ppoint[1][j].x,ppoint[1][j].y,ppoint[xx][j].x,ppoint[xx][j].y);
		}
		int i=1;		//画棋盘的竖格线
		for(i=1;i<=xx;i++)
		{
			g.drawLine(ppoint[i][1].x,ppoint[i][1].y,ppoint[i][yy].x,ppoint[i][yy].y);
		}
		
		//画棋盘的两边提示坐标		            
		for(i=1;i<=xx;i++)
		{
			g.drawString(""+i,i*unitWidth,unitHeight/2);			
		}
		j=1;
		for(char c='A';c<='O';c++)
		{			
			g.drawString("   "+c,unitWidth/4,j*unitHeight);	
			j++;		
		}				
	}
	
      //接收并处理服务器端发来的信息 	
      public void receive()
      {
      	//用于标识棋子位置的变量
      	String[]x_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
        String[]y_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
        		
	int new_M=0,new_N=0; //棋子的新位置
	C_ChessPiece piece=null;
	
	while(true)
	{
	 try
	{ 
	clientPlay=true; 		      		               
	String message=(String)input.readUTF();         //读取输入流数据 
	if(message!=null&&message.charAt(0)<='o'&&message.charAt(0)>='a') //排除标识棋子位置的字符
	{		               	 
		displayArea.append("\n服务器系统消息(对方下棋位置):");
		new_M=message.charAt(0)-96;
		new_N=message.charAt(1)-96;
		displayArea.append(""+new_M);
		displayArea.append(","+(char)(new_N+96));
		if(message.charAt(2)=='b')                 //接收到对方是黑子的处理方法
		{
			piece=new C_ChessPiece(Color.orange,Color.black,unitWidth-3,unitHeight-3,this);
			ppoint[new_M][new_N].setPiece(piece,this);
			blackWon.a[new_N]=blackWon.a[new_N]+(2<<new_M);
			widthPlay=true;
			blackPlay=false;		
			if(blackWon.isWon()==true) 
			{
				displayArea.append("\n黑方胜利!请开始..."); //如果一方胜利则提示重新开始			             	 	
				clientPlay=false;
				widthPlay=false;
			        blackPlay=false;
				break;
			}
		} 
		if(message.charAt(2)=='w')                    //接收到对方是白子的处理方法
		{
			piece=new C_ChessPiece(Color.orange,Color.white,unitWidth-3,unitHeight-3,this);
			ppoint[new_M][new_N].setPiece(piece,this);
			whiteWon.a[new_N]=whiteWon.a[new_N]+(2<<new_M);
			widthPlay=false;
			blackPlay=true;			
			if(whiteWon.isWon()==true)
			{
				displayArea.append("\n白方胜利!请开始...");       //如果一方胜利则提示重新开始
				clientPlay=false;
				widthPlay=false;
			        blackPlay=false;
				break;
			}
		}
	}
	 else if(message!=null&&(message.charAt(0)>'o'||message.charAt(0)<'a')) //显示汉语聊天内容
		 {		 	
		 	displayArea.append(message);
		 }
	
       }
       catch(IOException cc)
       {
       }
       }       
      }
      	
      	//处理鼠标点击事件	
	public void mouseClicked(MouseEvent e)	
	{	
		try
		{
		 C_ChessPiece piece=null;
		 Rectangle rect=null;
		 
		 //用于标识棋子位置的字符		
		 String[]x_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};
                 String[]y_t={"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o"};	
                 	
		 int new_M=0,new_N=0;    //记录传输过去和传输过来的棋子位置
		
		if(e.getSource()==this)		
		{			
			int p=(unitWidth-2)/2;
			int q=(unitHeight-2)/2;
			rect=new Rectangle((e.getX()-p),e.getY()-q,2*p,2*q);			
			boolean containChessPoint=false;
			int x=0,y=0;
			int m=0,n=0;
			
			//检查棋子当前覆盖的区域是否有一个棋点
		        for(int i=1;i<=xx;i++)
			{
				for(int j=1;j<=yy;j++)
				{
					x=ppoint[i][j].getX();
					y=ppoint[i][j].getY();
					if(rect.contains(x,y))
					{
						containChessPoint=true;
						m=i;
						n=j;
					}
				}				
			}
			
		        if(clientPlay==true&&widthPlay==false&&blackPlay==true)
		        {
			      try
			      {
			          piece=new C_ChessPiece(Color.orange,Color.black,unitWidth-3,unitHeight-3,this);
			          ppoint[m][n].setPiece(piece,this);         //在棋盘上放置棋子
			          blackWon.a[n]=blackWon.a[n]+(2<<(m-1));        //置有棋子的位置为1
			          widthPlay=true;
			          blackPlay=false;
			          clientPlay=false;			      
			          if(blackWon.isWon()==true) 
			          {
			          	displayArea.append("\n黑方胜利!请开始..."); //如果一方胜利则提示重新开始
			          	clientPlay=false;
			          	widthPlay=false;
			          	blackPlay=false;			          
			          }
			      	  
			          output.writeUTF(x_t[m]+y_t[n]+"b");    //向客户端发送数据
			          output.flush();			          	          		
			      }
			        catch(IOException IOException)
			        {
			        	System.out.println("Client catch black.");
			        }			     		      				
		        }		        
		        else if(clientPlay==true&&widthPlay==true&&blackPlay==false)
		        {			    
			       try
			       {
			       	  piece=new C_ChessPiece(Color.orange,Color.white,unitWidth-3,unitHeight-3,this);
			          ppoint[m][n].setPiece(piece,this);
			          whiteWon.a[n]=whiteWon.a[n]+(2<<(m-1));       //置有棋子的位置为1
			          widthPlay=false;
			          blackPlay=true;
			          clientPlay=false;			      			      	
			          if(whiteWon.isWon()==true) 
			          {
			          	displayArea.append("\n白方胜利!请开始..."); //如果一方胜利则提示重新开始	
			          	clientPlay=false;
			          	widthPlay=false;
			          	blackPlay=false;
			          }
			     	
			          output.writeUTF(x_t[m]+y_t[n]+"w");    //向客户端发送数据
			          output.flush();
			       }
			        catch(IOException IOException)
			        {
			        	System.out.println("Client catch white.");
			        }				
		        }
		}
	       }
	       catch(Exception ex)
	       {
	       	 System.out.println("Catch block.");
	       }
	}
	
	public void mouseReleased(MouseEvent e)
	{		
	}		
	public void mouseEntered(MouseEvent e)
	{
	}	
	public void mouseExited(MouseEvent e)
	{
	}	
	public void mousePressed(MouseEvent e)
	{
	}
}

public class ClientChess extends JFrame implements ActionListener
{
	C_ChessBoard board=null;
	Container con=null;	
	public ClientChess()	
	{		
		super("五子棋——客户端");
		board=new C_ChessBoard(35,35,15,15);        //创建棋盘的实例
		con=getContentPane();
		
		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,board,board.pan);
		split.setDividerSize(5);
		split.setDividerLocation(560);
		con.add(split,BorderLayout.CENTER);
						
		addWindowListener(new WindowAdapter()
		      {
		      	public void windowClosing(WindowEvent e)
		      	{
		      		System.exit(0);
		      	}
		      });
		      		
		setVisible(true);
		setBounds(60,20,850,600);
		con.validate();
		validate();
	}
	
	public void actionPerformed(ActionEvent e)
	{	
	}
	
	//主方法,客户端程序执行的入口
	public static void main(String args[])
	{
		ClientChess C_chess=new ClientChess();
		C_chess.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		C_chess.board.receive();
		C_chess.board.closeConnection();	
	}	
}

⌨️ 快捷键说明

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