import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
public class ChessWZQ extends JFrame
{
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
protected Image white = null;
protected Image black = null;
protected static int pColor; // 人的颜色
protected static int cColor; //电脑的颜色
static boolean ptocFlag=false;
boolean pFirst = false;
private int bestX=0; // 电脑找到最好的棋子位置的 X坐标
private int bestY=0; // 电脑找到最好的棋子位置的Y坐标
private int RectX1=0; // 根据坐标 (x,y)确定矩形范围
private int RectY1=0; // 长度为 9
private int RectX2=0; // 大小为 9*9
private int RectY2=0;
private int weightBoard[][];
static Socket socket;
private static int PORT;
static ObjectInputStream in;
static ObjectOutputStream out;
String name=null ;
String serverAddress=null;
static char send[];
public static Message message = new Message();
public static boolean beginFlag = false;
BoardPanel bpanel = new BoardPanel();
JPanel jpanel3 = new JPanel();
JLabel label1 = new JLabel("Player1");
JLabel label2 = new JLabel(" VS ");
JLabel label3 = new JLabel("Player2");
JLabel label4 = new JLabel("Player List ");
JLabel label5 = new JLabel("Message list... ");
static JLabel label6 = new JLabel("welcome");
JLabel label7 = new JLabel("Host");
JLabel label8 = new JLabel("Player");
JRadioButton jrbBlack = new JRadioButton("Black");
JRadioButton jrbWhite = new JRadioButton("White");
DefaultListModel lItems = new DefaultListModel();
JList list = new JList(lItems);
JMenuBar mb = new JMenuBar();
JMenu create = new JMenu("Create");
JMenu setting = new JMenu("Setting");
JMenu quit = new JMenu("Quit");
JMenu about = new JMenu("About");
JMenuItem cPtoP = new JMenuItem("Play With people");
JMenuItem cPtoC = new JMenuItem("Play With Computer");
JMenuItem load = new JMenuItem("Load game...");
JMenuItem save = new JMenuItem("Save ...");
// JMenuItem sLimited = new JMenuItem("禁止禁手");
// JMenuItem sNoLimited = new JMenuItem("允许禁手");
public ChessWZQ()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
getContentPane().setLayout(null);
weightBoard = new int [15][15]; // 保存每个位置的重要性
PORT = Server.PORT;//设置套接字端口
send = new char[60];
try {
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
try {
getContentPane().setLayout(null);
jrbBlack.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bpanel.setColor(1);
cColor=2;
jrbBlack.setEnabled(false);
jrbWhite.setEnabled(false);
drawChess(1);
jrbWhite.setSelected(true);
if(ptocFlag==true)
{
return;
}
Message ms = new Message();
ms.color=1;
ms.type=13;
try{
out.writeObject(ms);
}
catch(IOException error)
{
error.printStackTrace();
}
}
});
jrbWhite.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bpanel.setColor(2);
cColor=1;
jrbWhite.setEnabled(false);
jrbBlack.setEnabled(false);
drawChess(2);
jrbBlack.setSelected(false);
if(ptocFlag==true)
{
return;
}
Message ms = new Message();
ms.color=2;
ms.type=13;
try{
out.writeObject(ms);
}
catch(IOException error)
{
error.printStackTrace();
}
}
});
about.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
JOptionPane.showMessageDialog(null,
"Author: Kahn \nCopyright (c) 2007-CUIT \nMail:zhao4824593@163.com",
"五子棋1.0(beta)",JOptionPane.INFORMATION_MESSAGE);
}
});
quit.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int flag=JOptionPane.showConfirmDialog(null,
"Quit the Program ?",
"Are you sure to quit ?",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(flag==0)
{ //确定
// 断开和服务端的连接
sendDisconnect();
System.exit(0);
}
}
});
setting.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
Object selection[] = {"Forbiden","Allow any"};
Object set = JOptionPane.showInputDialog(null,
"Setting...","would you allow any method?",
JOptionPane.QUESTION_MESSAGE,null,selection,selection[0]);
if(ptocFlag==true)
{
return;
}
Message ms = new Message();
if(set==null)
{
return;
}
if(set.toString().equals(selection[0]))
{
ms.setting=false;
}
else{
ms.setting=true;
}
//System.out.println("setting begin..."+msg.setting);
ms.type=12;
try{
out.writeObject(ms);
}
catch(IOException error)
{
error.printStackTrace();
}
}
});
cPtoP.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ptocFlag=false;
JOptionPane.showMessageDialog(null,
"You can choose a player from the listBox on the right","Welcome...",
JOptionPane.INFORMATION_MESSAGE);
}
});
cPtoC.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(ptocFlag==true)
{
int flag=JOptionPane.showConfirmDialog(null,
"You give up... ?","Message",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(flag==0)
{
newGame();
return;
}
}
label3.setText("Computer");
Object selection[] = {"Computer First","You First"};
Object set = JOptionPane.showInputDialo