📄 thinker.java
字号:
package edu.rit.cs.mlr5773.connectfour;import java.net.*;import java.util.*;import java.io.*;/** * A single thinker process. This process will uplink to a central * server and wait for commands. The command will be a flattened board * and move information. The thinker will evaluate the board, and return * a numerical score repesenting the value of the board for the given * player. The thinker process will then await the next command. * * @author Mark Roth */public class Thinker { /** The socket to communicate to the server with */ private Socket socket; /** A local copy of the game being played */ private ConnectFourGame game; /** The maximum number of moves to look ahead. */ private int maxDepth; /** * Creates a new Thinker on the given socket, and starts the processing * loop. */ public Thinker( Socket socket ) { this.socket = socket; this.game = new ConnectFourGame(); try { InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); LineNumberReader in = new LineNumberReader( new InputStreamReader( is ) ); PrintWriter out = new PrintWriter( os, true ); // Send Thinker the host name. out.println( InetAddress.getLocalHost().getHostName() ); maxDepth = new Integer( in.readLine() ).intValue(); System.out.println( "Thinker Initialized..." ); do { String request = in.readLine(); if( (request == null) || (request.equals( "SHUTDOWN" )) ) break; String boardcode = ""; // The flattened out board. StringTokenizer st = new StringTokenizer( request ); int curPlayer = new Integer( st.nextToken() ).intValue(); int move = new Integer( st.nextToken() ).intValue(); while( st.hasMoreTokens() ) { boardcode += st.nextToken() + " "; } Board board = new Board( boardcode ); String reply = "" + thinkMove( board, curPlayer, move ); out.println( reply ); } while( true ); } catch( IOException e ) { System.err.println( "IO Error while communicating. Exiting." ); System.exit( 0 ); } System.out.println( "Shutting down..." ); } /** * Starts the Thinker on this node. Command line parameters are * <server-host> The host of the server running ConnectFour.class * <server-port> The port of the Connect Four server (see ThinkerManager) * * @see ThinkerManager */ public static void main(java.lang.String[] args) { String usage = "java Thinker <server-host> <server-port>"; if( args.length != 2 ) { System.err.println( usage ); System.exit( 0 ); } String host = args[0]; int port = new Integer( args[1] ).intValue(); try { Socket socket = new Socket( host, port ); Thinker thinker = new Thinker( socket ); } catch( UnknownHostException e ) { System.err.println( "Unknown host: " + host ); System.exit( 0 ); } catch( IOException e ) { System.err.println( "Error connecting to " + host + ":" + port + ". Reason:" + e.getMessage() ); System.exit( 0 ); } } /** * Thinks about this move, and returns what move to make. Creates a local * ComputerPlayer to do the thinking for it. */ private int thinkMove( Board board, int curPlayer, int move ) { game.setCurrentBoard( board ); ComputerPlayer computer = new ComputerPlayer( game, curPlayer, maxDepth ); computer.setThinkBoard( board ); return computer.value( move, 0 ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -