📄 serverchess.java
字号:
);
int i=1;
for(i=1;i<=r;i++)
{
int j=1;
for(j=1;j<=c;j++)
{
ppoint[i][j]=new S_ChessPoint(i*unitWidth,j*unitHeight,false);
}
}
serverOne.waitForConnection();//等待客户端的连接
//构造输入输出流的实例
try
{
output=new DataOutputStream(serverOne.connect.getOutputStream());
input=new DataInputStream(serverOne.connect.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();
serverOne.connect.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; //棋子的新位置
S_ChessPiece piece=null;
while(true)
{
try
{
serverPlay=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 S_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]+(1<<new_M);
widthPlay=true;
blackPlay=false;
if(blackWon.isWon()==true)
{
displayArea.append("\n黑方胜利!请开始关闭后重新开始..."); //如果一方胜利则重新开始
serverPlay=false;
widthPlay=false;
blackPlay=false;
break;
}
}
if(message.charAt(2)=='w') //接收到对方是白子的处理方法
{
piece=new S_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]+(1<<new_M);
widthPlay=false;
blackPlay=true;
if(whiteWon.isWon()==true)
{
displayArea.append("\n白方胜利!请开始..."); //如果一方胜利则重新开始
serverPlay=false;
widthPlay=false;
blackPlay=false;
break;
}
}
}
else if(message!=null&&(message.charAt(0)>'o'||message.charAt(0)<'a')) //显示汉语聊天内容
{
displayArea.append(message);
}
}
catch(IOException xx)
{
}
}
}
//处理鼠标事件
public void mouseClicked(MouseEvent e)
{
try
{
S_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(serverPlay==true&&widthPlay==false&&blackPlay==true)
{
try
{
piece=new S_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;
serverPlay=false;
if(blackWon.isWon()==true) //如果一方胜利则提示重新开始
{
displayArea.append("\n黑方胜利!请开始...");
serverPlay=false;
widthPlay=false;
blackPlay=false;
}
output.writeUTF(x_t[m]+y_t[n]+"b"); //向客户端发送数据
output.flush();
}
catch(IOException IOException)
{
System.out.println("Server catch white.");
}
}
else if(serverPlay==true&&widthPlay==true&&blackPlay==false)
{
try
{
piece=new S_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;
serverPlay=false;
if(whiteWon.isWon()==true)
{
displayArea.append("\n白方胜利!请开始...");//如果一方胜利则提示重新开始
serverPlay=false;
widthPlay=false;
blackPlay=false;
}
output.writeUTF(x_t[m]+y_t[n]+"w"); //向客户端发送数据
output.flush();
}
catch(IOException IOException)
{
System.out.println("Server 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 ServerChess extends JFrame implements ActionListener
{
S_ChessBoard board=null;
Container con=null;
public ServerChess()
{
super("五子棋——服务器端");
board=new S_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[])
{
ServerChess S_chess=new ServerChess();
S_chess.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
S_chess.board.receive();
S_chess.board.closeConnection();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -