⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chessex.java

📁 五子棋游戏的设计与源码下载
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  			byte meassage[]=new byte[2];
  			meassage[0]=(byte)i;meassage[1]=(byte)j;
  			if(player1) chess.modify(i,j,0);//服务端为白旗
  			if(player2) chess.modify(i,j,1);//客户端为黑棋
  			qipan.draw();
  			
  			this.setTitle("等待对方下子");
  			try{
  				out.write(meassage);
  				referee();
  				}
  		    catch(IOException e){
  		    	JOptionPane.showMessageDialog(null,"连接错误",
    	                     "Error",JOptionPane.ERROR_MESSAGE);
    	                Reset();//连接错误后就将程序置于初始状态
  		    }
  		    
  		}
  	}
  	if(ppplayer){
  		if(player1==true){
    	    chess.modify(i,j,0);
    	   	player1=false;
    	   	player2=true;
    	  }
    	  else if(player2==true){
    	     chess.point[i][j]=1;
    	      player2=false;
    	      player1=true;
    	  }
    	  qipan.draw();
    	  referee();
  	}
  }
  //建立线程监听接受数据
  public void setThread(){
  	socketthread=new SocketThread(this);
  	socketthread.start();//线程开始运行
  	JOptionPane.showMessageDialog(null,"成功连接",
    	                     "消息",JOptionPane.INFORMATION_MESSAGE);
  	if(player1) this.setTitle("开始下子");//服务器先下子
  	if(player2) this.setTitle("等待对方下子");
  }
  //建立服务器连接线程
  public void connectThread(){
  	 serverthread=new ServerThread(this);
  	 serverthread.start();
  }
  //网络对战中接受到对方的消息后的处理
  public void messageDeal(byte row,byte line){
  	wait=true;
  	if(player1) chess.point[row][line]=1;//服务端时收到黑棋
  	if(player2) chess.point[row][line]=0;//客户端时收到白旗
  	qipan.draw();
  	this.setTitle("该你下子了");
  	referee();
  	wait=false;//此时响应本方的鼠标事件
  
  }
  //检测棋盘是否放满棋子,是就为和局,弹出消息
  public boolean full(){
  	boolean full=false;
  	int count=0;
  	if(chess!=null){
     out:
     for(int i=0;i<15;i++)
      for(int j=0;j<15;j++){
        if(chess.point[i][j]==2){
        	 full=false;
             break out;
             }
         count++;
         }
     }
     if(count==225)
        { full=true;
          JOptionPane.showMessageDialog(null,"太厉害了,此局平局",
    	                     "消息",JOptionPane.INFORMATION_MESSAGE);
    	  reset();//重新开局
        }
     return full;
   }        
  public static void main(String args[]){
  	ChessEx t=new ChessEx();
  	
  	t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  	t.setSize(610,670);
  	t.setVisible(true);
  	
  }
  //画棋盘的类
  class Qipan extends JPanel {
  	public void paintComponent(Graphics g){
  		super.paintComponent(g);
  		g.setColor(Color.red);
  		g.drawRect(20,20,560,560);
  		for(int i=1;i<14;i++)
  		 g.drawLine(20+40*i,20,20+40*i,580);
  		for(int i=1;i<14;i++)
  		 g.drawLine(20,20+40*i,580,20+40*i);
  		 if(chess!=null)
  		 { for(int i=0;i<15;i++)
  		     for(int j=0;j<15;j++)
  		     {
  		       if(chess.point[i][j]==0)
  		       { 
  		       	  g.setColor(Color.white);
  		          g.fillOval(j*40+5,i*40+5,30,30);
  		        }
  		       if(chess.point[i][j]==1)
  		       {
  		       	  g.setColor(Color.black);
  		          g.fillOval(j*40+5,i*40+5,30,30);
  		        }
  		 	
  		     }

  		 }
  	
  	}
  	public void draw(){
  		repaint();
  	}
  }
  //服务器设定类
 public class ServerDialog extends JDialog {
 public ServerDialog(JFrame parent) {
   super(parent, "服务器设置", true);
   Container cp = getContentPane();
   cp.setLayout(new FlowLayout());
   cp.add(new JLabel("开通端口号:"));
   final JTextField pt;
   pt=new JTextField(4); 
   cp.add(pt);
        
   JButton ok = new JButton("OK");
         ok.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e){
     	try{
     		port=Integer.parseInt(pt.getText());
     		dispose();  
     	}
     	catch(NumberFormatException ee){
     		JOptionPane.showMessageDialog(null,"请重新正确可用的端口号",
    	                     "错误",JOptionPane.ERROR_MESSAGE);
     		
     	}
     	}
       
        // Closes the dialog
     }
   );
   cp.add(ok);
   setSize(300,100);
  }
 }
 //客户端设定对话框类
 class ClientDialog extends JDialog {
 public ClientDialog(JFrame parent) {
   super(parent, "客户端设置", true);
   Container cp = getContentPane();
   cp.setLayout(new FlowLayout());
   cp.add(new JLabel("服务器地址:"));
   final JTextField ip1,ip2,ip3,ip4,pt;
   ip1=new JTextField(3);
   ip2=new JTextField(3); 
   ip3=new JTextField(3); 
   ip4=new JTextField(3); 
   cp.add(ip1);
   cp.add(ip2);
   cp.add(ip3);
   cp.add(ip4);
   cp.add(new JLabel("端口号"));
   pt=new JTextField(4); 
   cp.add(pt);
        
   JButton ok = new JButton("OK");
         ok.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e){
     	try{
     		port=Integer.parseInt(pt.getText());
     		ip=ip1.getText()+"."+ip2.getText()
     		   +"."+ip3.getText()+"."+ip4.getText();
     		dispose();  
     	}
     	catch(NumberFormatException ee){
     		JOptionPane.showMessageDialog(null,"请正确输入IP和端口号",
    	                     "错误",JOptionPane.ERROR_MESSAGE);
     		
     	}
     	}
       
        // Closes the dialog
     }
   );
   cp.add(ok);
   setSize(300,100);
 }
 }
 //人机难易控制对话框
class EasyC extends JDialog {
  	 int power=1;//默认为1
 public EasyC(JFrame parent) {
   
   super(parent, "难易设置", true);
  JLabel eD= new JLabel("难易选择:");
   eD.setFont(font);
   eD.setForeground(labelColor);
   Container cp = getContentPane();
   cp.setLayout(new FlowLayout());
   cp.add(eD);
    JRadioButton beginer,intermediate,expert;//初级,中级,高级
    ButtonGroup control=new ButtonGroup();
   beginer=new JRadioButton("初级",true);
   beginer.setFont(font);
   beginer.setForeground(labelColor);
   intermediate=new JRadioButton("中级",false);
   intermediate.setFont(font);
   intermediate.setForeground(labelColor);
   expert= new JRadioButton("高级",false);
   expert.setFont(font);
   expert.setForeground(labelColor);
   control.add(beginer);
   control.add(intermediate);
   control.add(expert);
   beginer.addActionListener(new ActionListener(){
   	public void actionPerformed(ActionEvent e){
   		setpower(1);
   	}
   }
   );
   intermediate.addActionListener(new ActionListener(){
   	public void actionPerformed(ActionEvent e){
   		setpower(2);
   	}
   }
   );
   expert.addActionListener(new ActionListener(){
   	public void actionPerformed(ActionEvent e){
   		setpower(3);
   	}
   }
   );
   cp.add(beginer);
   cp.add(intermediate);
   cp.add(expert);
   JButton ok = new JButton("OK");
   ok.setFont(font);
   ok.setForeground(controlColor);
         ok.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e){
     	    computer.power=getpower();
     		dispose();  
     		}
        }
   );
   cp.add(ok);
   
   setSize(300,100);
 }
public void setpower(int i){
	power=i;
}
public int getpower(){
	return power;
 }
}
 class helpDocu extends JDialog {//帮助窗口
 public helpDocu(JFrame parent) {
   super(parent, "五子棋", true);
   Container cp = getContentPane();
   cp.setLayout(new FlowLayout());
   String INFO="                            五子棋1.0版        \n"+
       "                      棋盘大小为600*600\n"+
       "                      单机:人机对战\n"+
       "                           人人对战\n"+
       "                      网络对战:服务器:输入可用端口号\n"+
       "                                客户端:输入服务器IP和端口号\n"+
       "                      退出:退出程序\n"+
       "                      重置:当前状态下重新开始\n"+
       "                      五子连星(横竖斜均可)即胜\n"+
       "                      2006年01月6日03点完成--张佳强    ";  
         TextArea t = new TextArea(INFO);
         t.setForeground(Color.blue);
         t.setSize(200,120);
         t.setEditable(false);
         cp.add(t);
         setSize(300,180);
 }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -