?? clientchess.java
字號(hào):
//////////////////////////////////////////////////////////////////////////////
//
//SeverChess.java
//
//Created by Guanyi-Zhao
//
//////////////////////////////////////////////////////////////////////////////
//Readme:
// 創(chuàng)建五子棋的界面雙方能夠?qū)模⑶夷軌蛲瑫r(shí)用漢語(yǔ)聊天。
// 這是客戶端的源代碼
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
//棋點(diǎn)類
class C_ChessPoint
{
int x,y; //棋點(diǎn)位置坐標(biāo)
boolean isChessPiece=false; //棋點(diǎn)上是否有棋子
C_ChessPiece piece=null;
C_ChessBoard board=null;
//創(chuàng)建棋點(diǎn)對(duì)象
public C_ChessPoint(int x,int y,boolean boo)
{
this.x=x;
this.y=y;
isChessPiece=boo;
}
//判斷棋點(diǎn)上是否有棋子
public boolean isPiece()
{
return isChessPiece;
}
//設(shè)置棋點(diǎn)上是否有棋子
public void setIsPiece(boolean boo)
{
isChessPiece=boo;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
//設(shè)置棋點(diǎn)的位置
public void setX_Y(int x,int y)
{
this.x=x;
this.y=y;
}
//在棋點(diǎn)上放置棋子
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();
}
//獲取棋點(diǎn)上的棋子
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;
//創(chuàng)建棋子對(duì)象
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;
}
//設(shè)置棋子的顏色類別
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++) //初始化所有的數(shù)據(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;
}
//判斷正對(duì)角線是否滿足五連珠
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;
}
//判斷反對(duì)角線是否滿足五連珠條件
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;
}
//整體綜合判斷棋盤上是否有五連珠出現(xiàn)
public boolean isWon()
{
if(rowWon()||lineWon()||diag1()||diag2())
return true;
else return false;
}
}
class C_ChessBoard extends JPanel implements MouseListener
{
public C_ChessPoint ppoint[][]; //棋盤中的點(diǎn):棋點(diǎn)
public int unitWidth,unitHeight; //棋盤單位格的寬和高
int xx,yy; //棋盤的行和列
int x,y; //記錄鼠標(biāo)的位置
C_IsWon whiteWon=new C_IsWon(15); //記錄白方是否獲勝
C_IsWon blackWon=new C_IsWon(15); //記錄黑方是否獲勝
J_Client clientOne=new J_Client(); //創(chuàng)建客戶端的實(shí)例
DataOutputStream output; //創(chuàng)建輸出流
DataInputStream input; //創(chuàng)建輸入流
JPanel pan=null; //用于創(chuàng)建顯示走棋和聊天的界面
JTextArea displayArea=null;
JTextField enterField=null;
public boolean clientPlay=true; //控制客戶端走棋的變量
public boolean widthPlay=false,blackPlay=true; //控制走棋順序的變量
//創(chuàng)建對(duì)弈棋盤
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];
//棋盤的左上角的點(diǎn)是point[1][1],
//第一行的點(diǎn)是point[1][1],point[2][1],point[3][1]...point[xx][1],
//右下角的點(diǎn)是point[xx][yy].
pan=new JPanel();
pan.setLayout(new BorderLayout()); //設(shè)置布局方式
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;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -