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

📄 serverchess.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 S_ChessPoint
{
	int x,y; //棋点位置坐标
	boolean isChessPiece=false; //棋点上是否有棋子
	S_ChessPiece piece=null;
	S_ChessBoard board=null;
	
	//创建棋点对象
	public S_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(S_ChessPiece piece,S_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 S_ChessPiece getPiece()
	 {
	 	return piece;
	 }	
}

//棋子类
class S_ChessPiece extends JLabel
{
    Color backColor=null; //棋子的背景色
    Color foreColor=null; //棋子的颜色
    String chessColor="Black"; //棋子颜色的类别
    int width=0,height=0;  //棋子的宽度和高度
    S_ChessBoard board=null;
    
    //创建棋子对象
    public S_ChessPiece(Color bc,Color fc,int width,int height,S_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;
    }
}

//判断输赢的类
//这个类中用到二进制运算,用1来标识棋点上有棋子,0来标识棋点上没有棋子
//用二进制来减少循环次数,提高了运行的效率
class S_IsWon
{
	int []a;//存放棋盘的列,a[1]存放第一行的棋子,a[2]存放第二行的棋子...a[15]存放第15行的棋子。
	
	//构造方法,使数组a[]实例化,并且置初始值为零,即棋盘上没有棋子。
	public S_IsWon(int i)
	{
		a=new int[i+1];
		int j;
		for(j=0;j<=i;j++) //初始化所有的数据为零
		a[j]=0;
	}
	
	//行有五个棋子连在一起,即...11111...形式的二进制数
	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++)            //从1到15逐行判断是否有五连珠出现
		{
			for(j=1;j<=11;j++)
			{
				if((a[i]&row(j))==row(j))
				flag++;            //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++)          //从1到15逐列判断是否有五连珠出现
		{
			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;  //i、j、为控制循环的变量,flag为标识五连珠出现的变量
		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 S_ChessBoard extends JPanel implements MouseListener
{
	public S_ChessPoint ppoint[][];  //棋盘中的点:棋点
	public int unitWidth,unitHeight;  //棋盘单位格的宽和高
	int xx,yy;       //棋盘的行和列
	int x,y;         //记录鼠标的位置
				
	S_IsWon whiteWon=new S_IsWon(15);  //记录白方是否获胜
	S_IsWon blackWon=new S_IsWon(15);  //记录黑方是否获胜
	
	J_Server serverOne=new J_Server();//创建一个服务器serverOne
	DataOutputStream output=null;     //打开输出流  
        DataInputStream input=null;       //打开输入流  
      
        public boolean serverPlay=false;	//控制服务器端是否走棋的变量	 
	public boolean widthPlay=true,blackPlay=false;  //控制走棋顺序的变量
	
	JPanel pan=null;              //创建用于显示走棋以及聊天的界面界面
	JTextArea displayArea=null;     
	JTextField enterField=null;
		
	//构造方法,创建对弈棋盘和聊天的界面
	public S_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 S_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());    //设置pan的布局方式		
		
		enterField=new JTextField();
		enterField.setEnabled(true);
		enterField.setSize(190,200);
		pan.add( enterField,BorderLayout.SOUTH );
		
		displayArea=new JTextArea();		
		displayArea.setBackground(Color.green);
		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());
                      }   
                }

⌨️ 快捷键说明

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