• 服务器模拟三个人猜数字的小游戏 利用线程池


    1  Server

    public class Server {
    
    	public static void main(String[] args) {
    
    		ArrayList<ServerThread> sockets = new ArrayList<ServerThread>();
    		
    		ExecutorService es = Executors.newFixedThreadPool(60);
    		
    		try (ServerSocket serverSocket = new ServerSocket(8878);){
    			
    			while (true) {
    				
    				Socket socket = serverSocket.accept();
    				
    				ServerThread serverThread = new ServerThread(socket, sockets);
    				
    				sockets.add(serverThread);
    				
    				es.execute(serverThread);
    			}
    		} catch (IOException e) {
    
    			e.printStackTrace();
    		}
    		
    		es.shutdown();
    	}
    }
    

      

    2  ServerThread

    public class ServerThread implements Runnable {
    
    	public static int randomNumber;
    	public static int left;
    	public static int right;
    	
    	private Socket socket;
    
    	private ArrayList<ServerThread> sockets;
    	
    	private String name;
    
    	private PrintWriter printWriter;
    
    	public ServerThread(Socket socket, ArrayList<ServerThread> sockets) {
    		super();
    		this.socket = socket;
    		this.sockets = sockets;
    	}
    
    
    	@Override
    	public void run() {
    
    		try (InputStream inputStream = socket.getInputStream();
    				InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    
    				OutputStream outputStream = socket.getOutputStream();
    				PrintWriter printWriter = new PrintWriter(outputStream, true);) {
    
    			this.printWriter = printWriter;
    
    			/*
    			while (true) {
    				
    				String conten = bufferedReader.readLine();
    				
    				// 要求客户端发消息的时候,也加入标识,说明发送的是名字还是数字
    				// 1. 客户端输入的是名字 ---
    				// 2. 客户端输入的是数字 ---
    			}
    			*/
    			
    			// 1. 读客户端的名字
    			String clientName = bufferedReader.readLine();
    			this.name = clientName;
    
    			// 2. 发欢迎语
    			String welcomMessage = "3#欢迎 " + clientName + " 加入游戏";
    			this.sendMessageToAll(welcomMessage);
    			
    			// 3. 判断是否够 3 个人
    			if (this.sockets.size() == 3) {
    				
    				// 生成随机数,设定左右边界
    				ServerThread.randomNumber = (int)(Math.random() * 13 + 1);
    				ServerThread.left = 1;
    				ServerThread.right = 13;
    				
    				ServerThread serverThread = this.sockets.get(0);
    				
    				// 1#人名#消息内容
    				String gameStartMessage = "1#" + serverThread.getName() + "#游戏开始了,由 " + serverThread.getName() + " 开始猜数字,范围是:" + ServerThread.left + " 到 " + ServerThread.right;
    				this.sendMessageToAll(gameStartMessage);
    			}
    			
    			while (true) {
    				
    				// 4. 接收 Client 发送过来的数字,一直等待 Client 发数字
    				String numberString = bufferedReader.readLine();
    				
    				int number = Integer.parseInt(numberString);
    				
    				if (ServerThread.randomNumber == number) {
    					
    					// 猜中了
    					String endMessage = "2#" + this.name + " 猜中了,是:" + numberString;
    					this.sendMessageToAll(endMessage);
    					
    					
    				}else {
    					// 没猜中,下一个人猜
    					
    					// 获取结果和修改边界
    					String resultMessgate = ",";
    					
    					if (number > ServerThread.randomNumber) {
    						
    						resultMessgate += "猜大了";
    						ServerThread.right = number - 1;
    					}else {
    						
    						resultMessgate += "猜小了";
    						ServerThread.left = number + 1;
    					}
    					
    					
    					// 下一个人的索引 = 当前人的索引 + 1
    					int index = this.sockets.indexOf(this) + 1;
    					
    					if (index == 3) {
    						
    						// 回到第一个人
    						index = 0;
    					}
    					
    					ServerThread next = this.sockets.get(index);
    
    					// 1#人名#消息内容
    					String nextMessage = "1#" + next.getName() + "#" + this.name + "猜的是:" + numberString + resultMessgate + "。轮到 " + next.getName() + " 来猜,范围是:" + ServerThread.left + " 到 " + ServerThread.right;
    					
    					this.sendMessageToAll(nextMessage);
    				}
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * 发消息给所有人
    	 * @param message
    	 */
    	private void sendMessageToAll(String message) {
    
    		for (ServerThread serverThread : sockets) {
    
    			serverThread.getPrintWriter().println(message);
    		}
    	}
    
    
    	public PrintWriter getPrintWriter() {
    		return printWriter;
    	}
    
    
    	public String getName() {
    		return name;
    	}
    	
    }
    

      

    3  Client

    public class Client {
    
    	public static void main(String[] args) {
    
    		try (Socket socket = new Socket("127.0.0.1", 8878);
    				
    				InputStream inputStream = socket.getInputStream();
    				InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    				
    				OutputStream outputStream = socket.getOutputStream();
    				PrintWriter printWriter = new PrintWriter(outputStream, true);
    				
    				Scanner scanner = new Scanner(System.in)) {
    			
    			// 1. 发送自己的名字
    			String clientName = scanner.next();
    			printWriter.println(clientName);
    			
    			while (true) {
    				
    				// 2. 接收游戏相关的消息
    				String gameMessage = bufferedReader.readLine();
    				
    				// 拆分收到的信息
    				String[] strings = gameMessage.split("#");
    				
    				// 1 猜数字 2 出结果 3 欢迎语
    				String flag = strings[0];
    				
    				switch (flag) {
    				
    				case "1": {
    					
    					System.out.println(strings[2]);
    					
    					if (strings[1].equals(clientName)) {
    						
    						// 需要我们这个人猜数字
    						int number = scanner.nextInt();
    						
    						printWriter.println(number);
    					}
    				}
    					break;
    				case "2": {
    					
    					System.out.println(strings[1]);
    					
    					// 进行游戏结束的处理
    				}
    					break;
    				case "3": {
    					
    					System.out.println(strings[1]);
    				}
    				default:
    					break;
    				}
    			}
    			
    		} catch (UnknownHostException e) {
    			
    			e.printStackTrace();
    		} catch (IOException e) {
    			
    			e.printStackTrace();
    		}
    	}
    
    }
    

      

  • 相关阅读:
    Git输出格式——Git placeholders
    Unix的缺陷_王垠_新浪博客
    关于文件流的模拟上传——人人网首页拖拽上传详解(HTML5 Drag&Drop、FileReader API、formdata) | 彬Go
    perl正则表达式中的常用模式
    FileReader详解与实例读取并显示图像文件 | JS Mix
    Gitweb源码解析
    用ATL的W2A和A2W宏转换Unicode与ANSI字符串
    JavaScript正则表达式
    显示访客的浏览器类型
    显示屏幕分辨率
  • 原文地址:https://www.cnblogs.com/niuxiao12---/p/7285410.html
Copyright © 2020-2023  润新知