📄 connectfour.java
字号:
package edu.rit.cs.mlr5773.connectfour;import java.awt.*;import java.awt.event.*;import java.util.*;/** * The GUI Connect Four Client * * @author Mark Roth */public class ConnectFour extends WindowAdapter implements Observer { /** The game we are playing */ private ConnectFourGame game; /** The blue status label at the bottom of the window */ private Label messageLabel; /** Manages the Thinker processes if there are any ThinkerPlayers */ private ThinkerManager thinkerManager; /** * Creates a new game of ConnectFour * * @param player1 - The type of player that player 1 is * (1 = human, 2 = computer, 3 = thinker) * @param player2 - The type of player that player 2 is * (1 = human, 2 = computer, 3 = thinker) */ public ConnectFour( int player1, int player2, int maxDepth ) { game = new ConnectFourGame(); Frame f = new Frame(); messageLabel = new Label(); messageLabel.setText( "Ready" ); messageLabel.setBackground( Color.blue ); messageLabel.setForeground( Color.white ); f.add( "South", messageLabel ); BoardPanel boardPanel = new BoardPanel( game.getCurrentBoard() ); f.add( "Center", boardPanel ); f.pack(); f.show(); f.setResizable( false ); f.addWindowListener( this ); f.setTitle( "Connect Four" ); game.addObserver( this ); Player rPlayer1 = null, rPlayer2 = null; // If either player is a thinker, we must set up a thinker manager: boolean thinkersPresent = (player1 == 3) || (player2 == 3); thinkerManager = null; if( thinkersPresent ) { thinkerManager = new ThinkerManager( game, f, maxDepth ); } switch( player1 ) { case 1: rPlayer1 = new HumanPlayer( game, 1, boardPanel ); break; case 2: rPlayer1 = new ComputerPlayer( game, 1, maxDepth ); break; case 3: rPlayer1 = new ThinkerPlayer( game, 1, thinkerManager ); break; } switch( player2 ) { case 1: rPlayer2 = new HumanPlayer( game, 2, boardPanel ); break; case 2: rPlayer2 = new ComputerPlayer( game, 2, maxDepth ); break; case 3: rPlayer2 = new ThinkerPlayer( game, 2, thinkerManager ); break; } game.setPlayer1( rPlayer1 ); game.setPlayer2( rPlayer2 ); // If there are thinkers present, bring up the "Waiting for thinkers" // dialog. Otherwise, the game is ready to start! if( thinkersPresent ) { thinkerManager.waitForThinkers(); } else { game.start(); } } /** * Main - Execution starts here. * Commandline arguments are <player1> <player2> <maxdepth> * Where: * <player1> Is the type of player for player 1 (h = human, c = computer, * t = thinker) * <player2> Is the type of player for player 2 (h = human, c = computer, * t = thinker) * <maxdepth> Is the maximum number of moves ahead to search through. */ public static void main( String[] args ){ String usage = "Usage: ConnectFour {h|c|t} {h|c|t} <max-think-depth>"; int maxDepth; if( args.length != 3 ) { System.out.println( usage ); System.exit( 0 ); } int player1 = 0; int player2 = 0; if( args[0].equals( "h" ) ) player1 = 1; if( args[0].equals( "c" ) ) player1 = 2; if( args[0].equals( "t" ) ) player1 = 3; if( args[1].equals( "h" ) ) player2 = 1; if( args[1].equals( "c" ) ) player2 = 2; if( args[1].equals( "t" ) ) player2 = 3; maxDepth = new Integer( args[2] ).intValue(); if( (player1 == 0) || (player2 == 0) ) { System.out.println( usage ); System.exit( 0 ); } ConnectFour connectFour = new ConnectFour( player1, player2, maxDepth ); } /** * Called when the message label changes so that we can display * the new message in the window. */ public void update( Observable source, Object o ) { messageLabel.setText( (String)o ); } /** * Called when the user closes the window */ public void windowClosing( WindowEvent e ) { if( thinkerManager != null ) thinkerManager.shutdown(); System.exit( 0 ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -