import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;
/*------------------------------棋子信息--------------------------
---------------------------------------------------------------*/
class Chessman implements java.io.Serializable
{
public int m_x;
public int m_y;
public int m_type;
public Chessman(int x,int y,int type)
{
m_x=x;
m_y=y;
m_type=type;
}
public String toString()
{
return m_x+" "+m_y+" "+m_type;
}
}
/*-------------------类介绍-------------------------
成员变量:
m_type:消息类型
m_content:消息内容
功能:m_type为不同值时m_content有不同含义。
m_type=-1:断开连接
m_type=0:开始游戏
m_type=1:下棋,m_content表示下棋的位置信息,格式为:"x:y"
m_type=2:梅棋,m_content表示梅棋的原因;
m_type=3:和棋
m_type=4:投降
m_type=5:对方赢
m_type=6;拒绝游戏
m_type=7:同意梅棋
m_type=8:拒绝梅棋
m_type=9:同意求和
m_type=10:拒绝求和
---------------------------------------------------*/
class Message implements java.io.Serializable
{
public int m_type;
public String m_content;
public Message()
{
m_type=1;
m_content=null;
}
public Message(int type,String content)
{
m_type=type;
m_content=content;
}
public String toString()
{
return m_type+" "+m_content;
}
}
class FileChess
{
public Vector m_playList;
private File m_file;
public FileChess()
{
m_playList=new Vector();
}
public FileChess(String filename)
{
m_file=new File(filename);
}
public void SetFile(String dir,String filename)
{
m_file=new File(dir,filename);
}
public boolean saveToFile(Vector m_playList)
{
if(m_file==null)
{
return false;
}
try
{
FileOutputStream file_out=new FileOutputStream(m_file,false);
ObjectOutputStream object_out=new ObjectOutputStream(file_out);
for(int i=0;i<m_playList.size();i++)
{
object_out.writeObject(m_playList.elementAt(i));
}
object_out.close();
file_out.close();
}
catch(IOException f)
{
return false;
}
return true;
}
public boolean readFromFile()
{
if(m_file==null)
{
return false;
}
m_playList=new Vector();
try
{
FileInputStream file_in=new FileInputStream(m_file);
ObjectInputStream object_in=new ObjectInputStream(file_in);
while(true)
{
try
{
Chessman obj=(Chessman)object_in.readObject();
m_playList.addElement(obj);
}
catch(Exception e)
{
break;
}
}
object_in.close();
file_in.close();
}
catch(IOException f)
{
return false;
}
return true;
}
}
class Chess
{
public static final int m_boardWidth=15;
public static final int m_boardHeight=15;
public int m_chessBoard[][]; //记录棋盘状态的数组。-1值表示此点未着棋,0为黑,1为白
public Vector m_play;
public Chess()
{
m_play=new Vector();
m_chessBoard=new int[m_boardHeight+2][m_boardWidth+2];
for(int i=0;i<m_boardHeight+2;i++)
{
for(int j=0;j<m_boardWidth+2;j++)
{
m_chessBoard[i][j]=-1;
}
}
}
public boolean check(int x,int y,int type)
{
for(int i=0;i<m_play.size();i++)
{
Chessman cs=(Chessman)m_play.elementAt(i);
if(cs.m_x==x && cs.m_y==y &&
cs.m_type==type)
{
return true;
}
}
return false;
}
public boolean check(int x,int y)
{
for(int i=0;i<m_play.size();i++)
{
Chessman cs=(Chessman)m_play.elementAt(i);
if(cs.m_x==x && cs.m_y==y)
{
return true;
}
}
return false;
}
/*--------------------------------------------------------------
参数:cs:表示棋子位置及类型
direction: 为1:| 方向 为2:/ 方向
为3: - 方向 为4:\ 方向
功能:判断并得出cs棋子在direction方向的相连棋子数目
---------------------------------------------------------------*/
public int judgeDir(Chessman cs,int direction)
{
int mx=0,my=0;
int x=cs.m_x;
int y=cs.m_y;
int count=1;
int index;
switch(direction)
{
case 1:my=-1;break;
case 2:mx=1;my=-1;break;
case 3:mx=1;break;
default:mx=1;my=1;break;
}
while(true)
{
x+=mx;
y+=my;
if(x<0 || x>=m_boardWidth || y<0 || y>=m_boardHeight)
{
break;
}
if(check(x,y,cs.m_type))
{
count++;
}
else
{
break;
}
}
x=cs.m_x;
y=cs.m_y;
while(true)
{
x-=mx;
y-=my;
if(x<0 || x>=m_boardWidth || y<0 || y>=m_boardHeight)
{
break;
}
if(check(x,y,cs.m_type))
{
count++;
}
else
{
break;
}
}
return count;
}
public boolean isWin(Chessman cs)
{
for(int i=1;i<5;i++)
{
if(judgeDir(cs,i)>=5)
{
return true;
}
}
return false;
}
}
class Board extends JPanel implements MouseListener,ActionListener
{
private JButton m_button;
private Image m_black;
private Image m_white;
private JButton m_repent;
private JButton m_start;
private JButton m_giveup;
private JButton m_peace;
private JLabel m_tip;
private JButton m_ok;
private JButton m_cancel;
private Chess m_chess;
public int m_chessType;
private int m_isPlaying; //为0:还没开始游戏;为1:该自己下;为2:对方下
private int m_status; //1:是否开始游戏;2:是否同意梅棋;3:是否同意求和
//4:已经有人赢棋;0:正常状态
public FileChess m_fileChess;
private ObjectOutputStream m_objectOut;
private ObjectInputStream m_objectIn;
public Board()
{
this.setLayout(new BorderLayout());
JPanel panel1=new JPanel(new GridLayout(2,1));
JPanel panel2=new JPanel(new GridLayout(1,4));
JPanel panel3=new JPanel(new FlowLayout());
panel1.add(panel2);
panel1.add(panel3);
this.add(panel1,BorderLayout.SOUTH);
m_repent=new JButton("梅棋");
m_start=new JButton("开始游戏");
m_giveup=new JButton("认输");
m_peace=new JButton("求和");
String temp="欢迎使用网络五子棋(范高翔制作 QQ:407994670)";
m_tip=new JLabel(temp);
m_ok=new JButton("");
m_cancel=new JButton("拒绝");
m_repent.setEnabled(false);
m_giveup.setEnabled(false);
m_peace.setEnabled(false);
m_ok.setVisible(false);
m_cancel.setVisible(false);
panel2.add(m_start);
panel2.add(m_giveup);
panel2.add(m_repent);
panel2.add(m_peace);
panel3.add(m_tip);
panel3.add(m_ok);
panel3.add(m_cancel);
m_start.addActionListener(this);
m_repent.addActionListener(this);
m_giveup.addActionListener(this);
m_peace.addActionListener(this);
m_ok.addActionListener(this);
m_cancel.addActionListener(this);
m_black=this.getToolkit().getImage("images/black.gif");
m_white=this.getToolkit().getImage("images/white.gif");
m_chess=new Chess();
m_isPlaying=0;
m_fileChess=new FileChess();
addMouseListener(this);
}
public void setObjectStream(InputStream in,OutputStream out,boolean direction)
{
try
{
if(direction)
{
m_objectIn=new ObjectInputStream(in);
m_objectOut=new ObjectOutputStream(out);
}
else
{
m_objectOut=new ObjectOutputStream(out);
m_objectIn=new ObjectInputStream(in);
}
}
catch(Exception e)
{
m_tip.setText("创建对象流失败!");
}
}
public void closeObjectStream()
{
tr