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

📄 thinkermanager.java

📁 ParallelConnectFour是一个java小游戏
💻 JAVA
字号:
package edu.rit.cs.mlr5773.connectfour;import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;import java.util.*;/** * Manages a pool of Thinker processes.  Waits for connections, and * maintains a list of registered Thinkers.  Also manages the  * WaitThinkersDialog. *  * @author Mark Roth */public class ThinkerManager implements ActionListener {  /** The socket to listen on for Thinker Connections */  private ServerSocket socket;  /** The game being played */  private ConnectFourGame game;  /** The port to accept connections from */  public final int THINKER_MANAGER_PORT = 2005;  /** The Connect Four window frame, so that dialogs can be Modal */  private Frame gameFrame;  /** The dialog to display while waiting for Thinkers to connect */  private WaitThinkersDialog dialog;  /** True if we are no longer accepting connections */  private boolean donewaiting;  /** The currently registered thinkers */  private Vector thinkers;  /** The maximum number of moves to look ahead */  private int maxDepth;	  /**   * Creates a new Thinker Manager.   */  public ThinkerManager( ConnectFourGame game, Frame frame, int maxDepth ) {    this.game = game;    this.gameFrame = frame;    this.donewaiting = false;    thinkers = new Vector();    this.maxDepth = maxDepth;  }  /**   * Called whenever a new Thinker object registers   */  private void acceptThinker( int id, Socket s ) {    try {      InputStream is = s.getInputStream();      OutputStream os = s.getOutputStream();      LineNumberReader in = new LineNumberReader(         new InputStreamReader( is ) );      PrintWriter out = new PrintWriter( os, true );      ThinkerShell thinker = new ThinkerShell( id, maxDepth, in, out );      dialog.addThinker( thinker.getHostName() );      thinkers.addElement( thinker );      thinker.start();    }    catch( IOException e ) {      System.err.println( "Error communicating with thinker.  Reason: " + 	e.getMessage() );      System.exit( 0 );    }  }  /**   * Called when the user clicks OK in the Wait Thinkers dialog   */  public void actionPerformed( ActionEvent e ) {    donewaiting = true;  }  /**   * Returns the vector of registered Thinkers    */  public Vector getThinkers() {    return thinkers;  }  /**   * Shuts down this server, and sends a message to all Thinkers to shut down.   */  public void shutdown() {    int i;	    for( i = 0; i < thinkers.size(); i++ ) {      ThinkerShell thinker = (ThinkerShell)thinkers.elementAt( i );      thinker.shutdown();    }  }  /**   * Wait for thinkers, and start the game when they all come in.   */  public void waitForThinkers() {    int id = 0;    dialog = new WaitThinkersDialog( new Frame() );    dialog.show();    dialog.addActionListener( this );    try {      socket = new ServerSocket( THINKER_MANAGER_PORT );      socket.setSoTimeout( 1000 );      do {        try {          Socket newSocket = socket.accept();          acceptThinker( id++, newSocket );	}	catch( InterruptedIOException e ) {          if( donewaiting ) break;	}      } while( true );    }    catch( IOException e ) {      System.err.println( "IO Error while waiting for thinkers.  Reason: " + 	e.getMessage() );      System.exit( 0 );    }    // Now that all thinkers are registered. the game can start     game.start();  }}

⌨️ 快捷键说明

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