9网上五子棋对战(java)服务端源码
网上五子棋对战(java)服务端源码 /* 五子棋游戏是本人在学习java swing时写的一个程序,程序分两部分:服务器端和客户端。运行程序时先运行服务器端,然后另一方就可以运行客户端,运行后输入作为服务器端的主机ip地址和端口号(9872)双方就能对战了. 服务端有两个.java文件构成,运行时先运行ServerChess.java文件 =========================DrawChess.java==================================================== */ package chess.server; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Stroke; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.StreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import .ServerSocket; import .Socket; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JPanel; class DrawChess extends JPanel { private char[ ][ ] chess; private boolean isBlack=true; private final static int ROWS=16; private final static int COLS=16; private final static int ROW_GAP=40;//行间隙 private final static int COL_GAP=40;//列间隙 private final static int LEFT_BORDER=20; private final static int RIGHT_BORDER=20; private final static int UP_BORDER=20; private final static int DOWN_BORDER=20; private final static int RADIUS=20;//棋子半径 private int xLoc=-1;//最后一个棋子在数组中的下标 private int yLoc=-1; private ServerSocket ss; private Socket s; private BufferedReader br; private PrintWriter pw; public DrawChess() { chess=new char[ROWS][COLS];//初始化棋子 for(int i=0;i=5) { return true; } else { return false; } } /** * 垂直方向判断输赢 * @param i * @param j * @param ch * @return */ public boolean verticalJudge(int i,int j,char ch) { int count=0; do { i--; }while(i>=0 while(ch==chess[++i][j]) { count++; if(i==chess.length-1) { break; } } if(count>=5) { return true; } else { return false; } } /** * 正斜方判断输赢 * @param i * @param j * @param ch * @return */ public boolean aJudge(int i,int j,char ch) { int count=0; do { i--; j++; }while(i>=0 if(i==chess.length-1) { break; } } if(count>=5) { return true; } else { return false; } } /** * 反斜方判断输赢 * @param i * @param j * @param ch * @return */ public boolean bJudge(int i,int j,char ch) { int count=0; do { j--; i--; }while( i>=0 while(ch==chess[++i][++j]) { count++; if(j==chess.length-1 || i==chess.length-1) { break; } } if(count>=5) { return true; } else { return false; } } /** * 保存游戏 */ public boolean saveGame() { JFileChooser jfc=new JFileChooser(); jfc.showSaveDialog(this); File f=jfc.getSelectedFile(); FileOutputStream fos=null; try { fos=new FileOutputStream(f); for(int i=0;i