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

📄 server.java

📁 play caro online by Java
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.lang.*;
import java.lang.String.*;
import java.lang.Integer.*;

class Server extends JFrame implements ActionListener,Runnable{
	JButton butt[] = new JButton[100];
	int bv[][] = new int[10][10];
	Socket socket;
	Icon icnblack = new ImageIcon("icon1.gif");
	Icon icnwhite = new ImageIcon("icon2.gif");
	ServerSocket server;
	ObjectInputStream oin;
	ObjectOutputStream oout;
	InputStream ins;
	OutputStream outs;
	Container c;
	String s;
	Thread t ;
	int youwin=0;
	int dtwin = 0;
	int time2lose = 120;
	public Server(){
		super("Server");
		try{
			server = new ServerSocket(1234);
		}catch(IOException iou){
		}
		
	}
	void buildGUI(){
		try {
	        UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
	    } catch (InstantiationException e) {
	    } catch (ClassNotFoundException e) {
	    } catch (UnsupportedLookAndFeelException e) {
	    } catch (IllegalAccessException e) {
	    }

		c= getContentPane();
		c.setLayout(new BorderLayout());
		JPanel p = new JPanel(new GridLayout(10,10));
		p.setBorder(new EmptyBorder(5,5,5,5));
		for(int i=0;i<100;i++){
			butt[i] = new JButton("");
			p.add(butt[i]);
		}
		c.add("Center",p);
		creatButtonValue();
		setBV();
		setSize(400,400);
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
	public void run(){
		try{
		for(int i=0;i<120;i++){
			time2lose--;
			if(time2lose == 0)
			sayWin(5,"X");
			t.sleep(1000);
		}
		}catch(InterruptedException ie){
		}
	}
	public void startRet(){
		try{
		boolean finished = false;
		socket = this.server.accept();
		
		do{
			    oin = new ObjectInputStream(socket.getInputStream());
		        String gmkpos = (String)oin.readObject();
				if(gmkpos.equalsIgnoreCase("quit")) finished=true;
				decompileBV(gmkpos);

		
		}while(!finished);
		}catch(Exception e){
			
			
		}
	}
	public void creatButtonValue(){
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++)
			  bv[i][j] = 10*i+j+1;
	    }
	}
	public void clearValues(){
		for(int i=0;i<100;i++){
			butt[i].setIcon(null);
		}
		disableAll(false);
	}
	public void setBV(){
		for(int a=0;a<10;a++){
			for(int b=0;b<10;b++){
				   butt[10*a+b].setMnemonic(bv[a][b]);
			}
		}
	}
	public void setBV(int c,int d){
		butt[10*c+d].setIcon(icnblack);
	}
	public static void main(String arg[]){
		Server game = new Server();
		game.buildGUI();
		game.startRet();
	}
	public void actionPerformed(ActionEvent ae){

		        JButton but = (JButton)ae.getSource();
				Object[] butv = but.getSelectedObjects();
				String s = but.getLabel();
				Object ic = but.getIcon();
				Object butt[] = new Object[100];
				int i=0;
				time2lose = 120;
				t.stop();
		        if(ic==null)
		        {
		
						but.setIcon(icnwhite);
						i=but.getMnemonic();
				        int j = 0;
						int k = 0;
						int l = 0;
						int m = 0;
						boolean dk = false;
						for(int u = 0; u < 10; u++)
						{
							for(int v = 0; v < 10; v++)
							{
								if(bv[u][v] == i) 
								{
									l = u;
									m = v;
									String possend = new String("play"+l+"-"+m);
											try{
												oout = new ObjectOutputStream(socket.getOutputStream());
												oout.writeObject((Object)possend);
											}catch(IOException ie){}
								}
							}
				
						}
						checkRow();
						checkCol();
						checkCheo();
						disableAll(true);
						
		         }
		         if(i==0)
				  return;
         }
	
	
	public void checkRow(){
		int checkx =0;
		int checky =0;
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				if(butt[10*i+j].getIcon()==icnwhite){
					checkx =0;
					checky++;
					sayWin(checky,"O");
				}else if(butt[10*i+j].getIcon()==icnblack){
					checky=0;
					checkx++;
					sayWin(checkx,"X");
				}
			}
			checkx=0;
			checky=0;
		}
	}
	public void checkCol(){
		int checkx =0;
		int checky=0;
		for(int i=0;i<10;i++){
			for(int j=0;j<10;j++){
				if(butt[10*j+i].getIcon()==icnwhite){
					checkx =0;
					checky++;
					sayWin(checky,"O");
				}else if(butt[10*j+i].getIcon()==icnblack){
					checky=0;
					checkx++;
					sayWin(checkx,"X");
				}
			}
			checkx=0;
			checky=0;
		}
		
	}
	public void checkCheo(){
		int checkx =0;
		int checky=0;
		for(int i=0;i<10;i++){
			for(int j=i;j<100;j=j+9){
				if(butt[j].getIcon()==icnwhite){
					checkx =0;
					checky++;
					sayWin(checky,"O");
				}else if(butt[j].getIcon()==icnblack){
					checky=0;
					checkx++;
					sayWin(checkx,"X");
				}
			}
			for(int j=i;j<100;j=j+11){
				if(butt[j].getIcon()==icnwhite){
					checkx =0;
					checky++;
					sayWin(checky,"O");
				}else if(butt[j].getIcon()==icnblack){
					checky=0;
					checkx++;
					sayWin(checkx,"X");
				}
			}
			if(i>0){
				for(int k=10*i;k<95;k=k+11){
					if(butt[k].getIcon()==icnwhite){
						checkx =0;
						checky++;
						sayWin(checky,"O");
					}else if(butt[k].getIcon()==icnblack){
						checky=0;
						checkx++;
						sayWin(checkx,"X");
					}
			     }
			     for(int l=10*i+9;l<95;l=l+9){
					if(butt[l].getIcon()==icnwhite){
						checkx =0;
						checky++;
						sayWin(checky,"O");
					}else if(butt[l].getIcon()==icnblack){
						checky=0;
						checkx++;
						sayWin(checkx,"X");
					}
			     }
			}
			
			checkx=0;
			checky=0;
		}
	}
	public void sayWin(int check,String chr){
		if(check == 5){
			String message;
			if(chr == "O"){
				message = "You WIN!";
				youwin++;
			}else{
				message = "YOU LOSE!";
				dtwin++;
			}			    
			    int answer = JOptionPane.showConfirmDialog(Server.this, message , new String("OK"),JOptionPane.YES_NO_OPTION);
			    if (answer == JOptionPane.YES_OPTION) {
			        clearValues();
			    } else if (answer == JOptionPane.NO_OPTION) {
			        t.stop();
			    }
			    

		}
	}
    public void decompileBV(String pos){
		String gamecmd = pos.substring(0,4);
		
		if(gamecmd.equals("play")){
			String posret = pos.substring(4);
			try{
				int gidx = posret.indexOf("-");
				int butx = Integer.parseInt(posret.substring(0,gidx));
				int buty = Integer.parseInt(posret.substring(gidx+1));
				
				setBV(butx,buty);
				checkRow();
				checkCol();
				checkCheo();
				disableAll(false);
				t = new Thread(this);
				t.start();
			}catch(Exception he){
				
			}
		}else if(gamecmd.equals("stat")){
			String textret1 = "BAT DAU CHOI";
			disableAll(false);
			clearValues();
		}
		

	}
	public void disableAll(boolean a){
		if(a == true){
			for(int i=0;i<100;i++){
			  butt[i].removeActionListener(this);
			  butt[i].removeActionListener(this);
		    }
		    
		}else{
			for(int i=0;i<100;i++){
			  butt[i].addActionListener(this);
		    }
		}

	}
}

⌨️ 快捷键说明

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