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

📄 clientchess.java

📁 基于java语言编程实现的简单ftp协议源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//////////////////////////////////////////////////////////////////////////////
//
//SeverChess.java
//
//Created by Guanyi-Zhao
//
//////////////////////////////////////////////////////////////////////////////
//Readme:
//        创建五子棋的界面双方能够对弈,并且能够同时用汉语聊天。 
//        这是客户端的源代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

//棋点类
class C_ChessPoint
{
	int x,y; //棋点位置坐标
	boolean isChessPiece=false; //棋点上是否有棋子
	C_ChessPiece piece=null;
	C_ChessBoard board=null;
	
	//创建棋点对象
	public C_ChessPoint(int x,int y,boolean boo)
	{
		this.x=x;
		this.y=y;
		isChessPiece=boo;
	}
	
	//判断棋点上是否有棋子
	public boolean isPiece()
	{
		return isChessPiece;
	}
	
	//设置棋点上是否有棋子
	public void setIsPiece(boolean boo)
	{
		isChessPiece=boo;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
	
	//设置棋点的位置	
	public void setX_Y(int x,int y)
	{
		this.x=x;
		this.y=y;
	}	
	
	//在棋点上放置棋子
	public void setPiece(C_ChessPiece piece,C_ChessBoard board)
	{
		this.board=board;
		this.piece=piece;
		board.add(piece);
		int w=(board.unitWidth);
		int h=(board.unitHeight);
		piece.setBounds(x-w/2,y-h/2,w,h);
		isChessPiece=true;
		board.validate();		
	}
	
	//获取棋点上的棋子
	 public C_ChessPiece getPiece()
	 {
	 	return piece;
	 }	
}

//棋子类
class C_ChessPiece extends JLabel
{
    Color backColor=null; //棋子的背景色
    Color foreColor=null; //棋子的颜色
    String chessColor="Black"; //棋子颜色的类别
    int width=0,height=0;  //棋子的宽度和高度
    C_ChessBoard board=null;  
    
    //创建棋子对象
    public C_ChessPiece(Color bc,Color fc,int width,int height,C_ChessBoard board)
    {
    	backColor=bc;
    	foreColor=fc; 
    	this.width=width;
    	this.height=height;   	
    	this.board=board;
    	
    	setSize(width,height);
    	setBackground(bc);
      	addMouseListener(board);    	  	
    }
    
    //绘制棋子的外观
    public void paint(Graphics g)
    {
    	g.setColor(foreColor);
    	g.fillOval(2,2,width-2,height-2);
    	g.setColor(Color.red);
    	g.drawOval(2,2,width-2,height-2);
    }
    
    //获取棋子的宽度
    public int getWidth()
    {
    	return width;
    }
    
    //获取棋子的高度
    public int getHeigth()
    {
    	return height;
    }
    
    //获取棋子的颜色
    public Color getColor()
    {
    	return foreColor;
    }
    
    //获取棋子的颜色类别
    public String chessColor()
    {
    	return chessColor;
    }
    
    //设置棋子的颜色类别
    public void setChessColor(String cc)
    {
    	chessColor=cc;
    }
}

//判断输赢的类
class C_IsWon
{
	int []a;  //存放棋盘的列
	
	public C_IsWon(int i)
	{
		a=new int[i+1];
		int j;
		for(j=0;j<=i;j++) //初始化所有的数据为零
		a[j]=0;
	}
	
	public int row(int n)
	{
		int f;
		f=(1<<n)+(1<<(n+1))+(1<<(n+2))+(1<<(n+3))+(1<<(n+4));
		return(f);
	}
	
	//判断行是否满足五连珠
	public boolean rowWon()
	{
		int i=0,j=0,flag=0;
		for(i=1;i<=15;i++)
		{
			for(j=1;j<=11;j++)
			{
				if((a[i]&row(j))==row(j))
				flag++;
			}
		}
		if(flag!=0) return true;
		else return false;
	}
	
	//判断列是否满足五连珠
	public boolean lineWon()
	{
		int i=0,j=0,flag=0;
		for(j=1;j<=15;j++)
		{
			for(i=1;i<=11;i++)
			{
				if(((a[i]&(1<<j))==(1<<j))&&((a[i+1]&(1<<j))==(1<<j))&&((a[i+2]&(1<<j))==(1<<j))
				  &&((a[i+3]&(1<<j))==(1<<j))&&((a[i+4]&(1<<j))==(1<<j)))
				flag++;
			}
		}
		if(flag!=0) return true;
		else return false;
	}
	
	//判断正对角线是否满足五连珠
	public boolean diag1()
	{
		int i=0,j=0,flag=0;
		for(i=1;i<=11;i++)
		for(j=1;j<=11;j++)
		{
			if((a[i]&(1<<j))==(1<<j)&&(a[i+1]&(1<<(j+1)))==(1<<(j+1))
			  &&(a[i+2]&(1<<(j+2)))==(1<<(j+2))&&(a[i+3]&(1<<(j+3)))==(1<<(j+3))
			  &&(a[i+4]&(1<<(j+4)))==(1<<(j+4)))
			  flag++;			 
		}
		if(flag!=0) return true;
		else return false;
	}
	
	//判断反对角线是否满足五连珠条件
	public boolean diag2()
	{
		int i=0,j=0,flag=0;
		for(i=1;i<=11;i++)
		for(j=15;j>=5;j--)
		{
			if((a[i]&(1<<j))==(1<<j)&&(a[i+1]&(1<<(j-1)))==(1<<(j-1))
			  &&(a[i+2]&(1<<(j-2)))==(1<<(j-2))&&(a[i+3]&(1<<(j-3)))==(1<<(j-3))
			  &&(a[i+4]&(1<<(j-4)))==(1<<(j-4)))
			  flag++;
		}
		if(flag!=0) return true;
		else return false;
	}
	
	//整体综合判断棋盘上是否有五连珠出现
	public boolean isWon()
	{
		if(rowWon()||lineWon()||diag1()||diag2())
		  return true;
		else return false;
	}	
}

class C_ChessBoard extends JPanel implements MouseListener
{
	public C_ChessPoint ppoint[][];  //棋盘中的点:棋点
	public int unitWidth,unitHeight;  //棋盘单位格的宽和高
	int xx,yy;       //棋盘的行和列
	int x,y;         //记录鼠标的位置
			
	C_IsWon whiteWon=new C_IsWon(15);  //记录白方是否获胜
	C_IsWon blackWon=new C_IsWon(15);  //记录黑方是否获胜
	
	J_Client clientOne=new J_Client();      //创建客户端的实例
	DataOutputStream output;                 //创建输出流
        DataInputStream input;                   //创建输入流
        
        JPanel pan=null;                     //用于创建显示走棋和聊天的界面
	JTextArea displayArea=null;
	JTextField enterField=null;        
        
        public boolean clientPlay=true;		//控制客户端走棋的变量
	public boolean widthPlay=false,blackPlay=true;  //控制走棋顺序的变量
	
	//创建对弈棋盘
	public C_ChessBoard(int w,int h,int r,int c)
	{
		setLayout(null);
		addMouseListener(this);		
		setBackground(Color.orange);
		Color bc=getBackground();
				
		unitWidth=w;
		unitHeight=h;
		xx=r;
		yy=c;			
		ppoint=new C_ChessPoint[r+1][c+1];
		//棋盘的左上角的点是point[1][1],
		//第一行的点是point[1][1],point[2][1],point[3][1]...point[xx][1],
		//右下角的点是point[xx][yy].	
		
		pan=new JPanel();
		pan.setLayout(new BorderLayout());    //设置布局方式		
		
		enterField=new JTextField();
		enterField.setEnabled( true );
		enterField.setSize(190,200);
		pan.add( enterField,BorderLayout.SOUTH );
		
		displayArea=new JTextArea();		
		displayArea.setBackground(Color.yellow);
		displayArea.setSize(190,390);
		pan.add(new JScrollPane( displayArea ),BorderLayout.CENTER);
		
		pan.setSize(150,600);
		pan.setBackground(Color.white);
		enterField.addActionListener(new ActionListener()
		{             
                     public void actionPerformed( ActionEvent ev )
                     {
                         sendData(ev.getActionCommand());
                     }   
                }  
               );
               
		int i=1;

⌨️ 快捷键说明

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