📄 chessex.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChessEx extends JFrame{
String ip;//客户端连接服务器时的IP
int port=8008;//服务器和客户端使用的端
JMenuBar mb1;
Qipan qipan;
boolean cplayer=false;//人机对战
boolean wplayer=false;//网络对战
boolean ppplayer=false;//人人对战
boolean player1=true;
boolean player2=false;
boolean wait=false;
public Chess chess;
public IfWin judge;
public Computer computer;
public ServerSocket server;
public Socket connection;
public InputStream in;
public OutputStream out;
SocketThread socketthread;
ServerThread serverthread;
private Color controlColor=new Color(051,102,255);
private Color labelColor=new Color(204,51,0);
private Font font=new Font("宋体",Font.PLAIN,13);
public ChessEx(){
this.setTitle("五子棋游戏 江南大学计算机科学与技术0303 张佳强 开发设计");
Container container=getContentPane();
container.setLayout(new BorderLayout());
qipan=new Qipan();
qipan.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
int x,y;
int i,j;//记录行号和列号
x=e.getX();y=e.getY();//将点击的位置记录下来
j=x/40;i=y/40;
if(j<15&&i<15&&!full()){
if(chess!=null){
if(chess.point[i][j]==2){
xiazilistener(i,j);
}
else JOptionPane.showMessageDialog(null,"该点已有棋子",
"Error",JOptionPane.ERROR_MESSAGE);
}
repaint();
}
}
});
JMenuBar mb1 = new JMenuBar();
JMenu hlp= new JMenu("帮助");
hlp.setForeground(controlColor);
hlp.setFont(font);
JMenu control = new JMenu("控制");
control.setForeground(controlColor);
control.setFont(font);
JMenu Sdalone= new JMenu("单机");
Sdalone.setForeground(controlColor);
Sdalone.setFont(font);
JMenuItem help = new JMenuItem("说明");
help.setForeground(controlColor);
help.setFont(font);
help.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
helpDocu s=new helpDocu(null);
s.move(300,300);
s.show();
s=null;
}
}
);
JMenuItem res=new JMenuItem("重置");//此局从来
res.setForeground(controlColor);
res.setFont(font);
res.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
reset();
}
});
JMenuItem pps=new JMenuItem("人人对战");//单机人人
pps.setForeground(controlColor);
pps.setFont(font);
pps.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{ setTitle("单机人人对战");
Reset();//整个程序状态置于初始状态
ppplayer=true;
chess=new Chess();
judge=new IfWin(chess);
qipan.draw();
}
});
JMenuItem pc=new JMenuItem("人机对战");//人机
pc.setForeground(controlColor);
pc.setFont(font);
pc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Reset();//整个程序状态置于初始状态
setTitle("人机对战");
qipan.draw();
cplayer=true;
chess=new Chess();
judge=new IfWin(chess);
computer=new Computer(judge);
EasyC ea=new EasyC(null);//难易控制
ea.move(300,300);
ea.show();
ea=null;
}
});
JMenu ppw=new JMenu("网络对战");//网络对战
ppw.setForeground(controlColor);
ppw.setFont(font);
JMenuItem server1=new JMenuItem("服务器");//服务器
server1.setForeground(controlColor);
server1.setFont(font);
server1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Reset();//整个程序状态置于初始状态
ServerDialog s=new ServerDialog(null);
s.move(300,300);
s.show();
s=null;
chess=new Chess();
judge=new IfWin(chess);
connectThread();
}
});
JMenuItem client1=new JMenuItem("客户端");//客户端
client1.setForeground(controlColor);
client1.setFont(font);
client1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Reset();//整个程序状态置于初始状态
ClientDialog s=new ClientDialog(null);
s.move(300,300);
s.show();
s=null;
chess=new Chess();
judge=new IfWin(chess);
try{
connection=new Socket(ip,port);
in=connection.getInputStream();
out=connection.getOutputStream();
wait=true;//客户端等待
wplayer=true;
player1=false;
player2=true; //设置本机为客户端
setThread();
}
catch(IOException ex){
Reset();//连接错误后就将程序置于初始状态
JOptionPane.showMessageDialog(null,"连接错误",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
});
ppw.add(server1);
ppw.add(client1);
JMenuItem exit=new JMenuItem("退出");//退出程序
exit.setFont(font);
exit.setForeground(controlColor);
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
hlp.add(help);
Sdalone.add(pc);
Sdalone.add(pps);
control.add(Sdalone);
control.add(ppw);
control.add(res);
control.add(exit);
mb1.add(control);
mb1.add(hlp);
container.add(mb1,BorderLayout.NORTH);
container.add(qipan,BorderLayout.CENTER);
}
//裁判谁赢谁输
public void referee(){
judge.count();
if(judge.judge()==0)
{
JOptionPane.showMessageDialog(null,"恭喜!!白旗赢!!!",
"恭喜",JOptionPane.INFORMATION_MESSAGE);
qipan.draw();
reset();//重置
}
else if(judge.judge()==1)
{
JOptionPane.showMessageDialog(null,"恭喜!!黑旗赢!!!",
"恭喜",JOptionPane.INFORMATION_MESSAGE);
qipan.draw();
reset();//重置
}
}
//重置状态
public void reset(){
if(chess!=null)
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
chess.point[i][j]=2;
qipan.draw();
if(!wplayer){
player1=true;
player2=false;
}
if(wplayer){
byte meassage[]=new byte[2];
meassage[0]=-1;meassage[1]=0;
try{
out.write(meassage);
if(player1) setTitle("该你下子");
if(player2) setTitle("等待对方下子");
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"连接错误",
"Error",JOptionPane.ERROR_MESSAGE);
Reset();//连接错误后就将程序置于初始状态
}
}
}
//重置状态到运行初始阶段
public void Reset(){
if(wplayer){
byte meassage[]=new byte[2];
try{ meassage[0]=-1;meassage[1]=-1;
out.write(meassage);
socketthread.stop();
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"连接错误",
"Error",JOptionPane.ERROR_MESSAGE);
}
}
chess=null;
judge=null;
connection=null;
server=null;
in=null;
out=null;
computer=null;
cplayer=false;
wplayer=false;
ppplayer=false;
serverthread=null;
socketthread=null;
}
//下棋监听处理程序
public void xiazilistener(int i,int j){
if(cplayer){
if(player1){
chess.modify(i,j,0);
player1=false;
player2=true;
qipan.draw();
referee();
}
if(player2&&chess!=null){
computer.location();
i=computer.row;
j=computer.line;
chess.modify(i,j,1);
player2=false;
player1=true;
qipan.draw();
referee();
}
}
if(wplayer){
if(wait==false){
wait=true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -