亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? tictactoeserver.java

?? tic tac toe client server
?? JAVA
字號:
// Fig. 18.8: TicTacToeServer.java
// This class maintains a game of Tic-Tac-Toe for two client applets.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public class TicTacToeServer extends JFrame {
   private char[] board;           
   private JTextArea outputArea;
   private Player[] players;
   private ServerSocket server;
   private int currentPlayer;
   private final int PLAYER_X = 0, PLAYER_O = 1;
   private final char X_MARK = 'X', O_MARK = 'O';

   // set up tic-tac-toe server and GUI that displays messages
   public TicTacToeServer()
   {
      super( "Tic-Tac-Toe Server" );

      board = new char[ 9 ]; 
      players = new Player[ 2 ];
      currentPlayer = PLAYER_X;
 
      // set up ServerSocket
      try {
         server = new ServerSocket( 12345, 2 );
      }

      // process problems creating ServerSocket
      catch( IOException ioException ) {
         ioException.printStackTrace();
         System.exit( 1 );
      }

      // set up JTextArea to display messages during execution
      outputArea = new JTextArea();
      getContentPane().add( outputArea, BorderLayout.CENTER );
      outputArea.setText( "Server awaiting connections\n" );

      setSize( 300, 300 );
      setVisible( true );

   } // end TicTacToeServer constructor

   // wait for two connections so game can be played
   public void execute()
   {
      // wait for each client to connect
      for ( int i = 0; i < players.length; i++ ) {

         // wait for connection, create Player, start thread
         try {
            players[ i ] = new Player( server.accept(), i );
            players[ i ].start();
         }

         // process problems receiving connection from client
         catch( IOException ioException ) {
            ioException.printStackTrace();
            System.exit( 1 );
         }
      }

      // Player X is suspended until Player O connects. 
      // Resume player X now.          
      synchronized ( players[ PLAYER_X ] ) {
         players[ PLAYER_X ].setSuspended( false );   
         players[ PLAYER_X ].notify();
      }
  
   }  // end method execute
   
   // utility method called from other threads to manipulate 
   // outputArea in the event-dispatch thread
   private void displayMessage( final String messageToDisplay )
   {
      // display message from event-dispatch thread of execution
      SwingUtilities.invokeLater(
         new Runnable() {  // inner class to ensure GUI updates properly

            public void run() // updates outputArea
            {
               outputArea.append( messageToDisplay );
               outputArea.setCaretPosition( 
                  outputArea.getText().length() );
            }

         }  // end inner class

      ); // end call to SwingUtilities.invokeLater
   }

   // Determine if a move is valid. This method is synchronized because 
   // only one move can be made at a time.
   public synchronized boolean validateAndMove( int location, int player )
   {
      boolean moveDone = false;

      // while not current player, must wait for turn
      while ( player != currentPlayer ) {
         
         // wait for turn
         try {
            wait();
         }

         // catch wait interruptions
         catch( InterruptedException interruptedException ) {
            interruptedException.printStackTrace();
         }
      }

      // if location not occupied, make move
      if ( !isOccupied( location ) ) {
  
         // set move in board array
         board[ location ] = currentPlayer == PLAYER_X ? X_MARK : O_MARK;

         // change current player
         currentPlayer = ( currentPlayer + 1 ) % 2;

         // let new current player know that move occurred
         players[ currentPlayer ].otherPlayerMoved( location );

         notify(); // tell waiting player to continue

         // tell player that made move that the move was valid
         return true; 
      }

      // tell player that made move that the move was not valid
      else 
         return false;

   } // end method validateAndMove

   // determine whether location is occupied
   public boolean isOccupied( int location )
   {
      if ( board[ location ] == X_MARK || board [ location ] == O_MARK )
          return true;
      else
          return false;
   }

   // place code in this method to determine whether game over 
   public boolean isGameOver()
   {
      return false;  // this is left as an exercise
   }

   public static void main( String args[] )
   {
      TicTacToeServer application = new TicTacToeServer();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      application.execute();
   }

   // private inner class Player manages each Player as a thread
   private class Player extends Thread {
      private Socket connection;
      private DataInputStream input;
      private DataOutputStream output;
      private int playerNumber;
      private char mark;
      protected boolean suspended = true;

      // set up Player thread
      public Player( Socket socket, int number )
      {
         playerNumber = number;

         // specify player's mark
         mark = ( playerNumber == PLAYER_X ? X_MARK : O_MARK );

         connection = socket;
         
         // obtain streams from Socket
         try {
            input = new DataInputStream( connection.getInputStream() );
            output = new DataOutputStream( connection.getOutputStream() );
         }

         // process problems getting streams
         catch( IOException ioException ) {
            ioException.printStackTrace();
            System.exit( 1 );
         }

      } // end Player constructor

      // send message that other player moved
      public void otherPlayerMoved( int location )
      {
         // send message indicating move
         try {
            output.writeUTF( "Opponent moved" );
            output.writeInt( location );
         }

         // process problems sending message
         catch ( IOException ioException ) { 
            ioException.printStackTrace();
         }
      }

      // control thread's execution
      public void run()
      {
         // send client message indicating its mark (X or O),
         // process messages from client
         try {
            displayMessage( "Player " + ( playerNumber == 
               PLAYER_X ? X_MARK : O_MARK ) + " connected\n" );
 
            output.writeChar( mark ); // send player's mark

            // send message indicating connection
            output.writeUTF( "Player " + ( playerNumber == PLAYER_X ? 
               "X connected\n" : "O connected, please wait\n" ) );

            // if player X, wait for another player to arrive
            if ( mark == X_MARK ) {
               output.writeUTF( "Waiting for another player" );
   
               // wait for player O
               try {
                  synchronized( this ) {   
                     while ( suspended )
                        wait();  
                  }
               } 

               // process interruptions while waiting
               catch ( InterruptedException exception ) {
                  exception.printStackTrace();
               }

               // send message that other player connected and
               // player X can make a move
               output.writeUTF( "Other player connected. Your move." );
            }

            // while game not over
            while ( ! isGameOver() ) {

               // get move location from client
               int location = input.readInt();

               // check for valid move
               if ( validateAndMove( location, playerNumber ) ) {
                  displayMessage( "\nlocation: " + location );
                  output.writeUTF( "Valid move." );
               }
               else 
                  output.writeUTF( "Invalid move, try again" );
            }         

            connection.close(); // close connection to client

         } // end try

         // process problems communicating with client
         catch( IOException ioException ) {
            ioException.printStackTrace();
            System.exit( 1 );
         }

      } // end method run

      // set whether or not thread is suspended
      public void setSuspended( boolean status )
      {
         suspended = status;
      }
   
   } // end class Player

} // end class TicTacToeServer

/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and               *
 * Prentice Hall. All Rights Reserved.                                    *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲乱码日产精品bd| 国产精品69毛片高清亚洲| 麻豆久久久久久| 成人黄色软件下载| 精品日韩欧美一区二区| 亚洲摸摸操操av| 国产999精品久久| 日韩视频免费直播| 午夜伦理一区二区| 91在线视频观看| 日本一区二区三区国色天香| 免费不卡在线观看| 欧美日韩大陆在线| 亚洲制服丝袜av| 色噜噜久久综合| 中日韩av电影| 国产一区二区三区美女| 日韩一区二区精品在线观看| 亚洲成a人片在线不卡一二三区 | 国产成人免费视频网站| 欧美一区二区三区免费在线看| 亚洲精品视频一区二区| 99亚偷拍自图区亚洲| 国产亚洲成年网址在线观看| 久久99精品久久久久久动态图| 91精品国产综合久久精品| 婷婷综合另类小说色区| 欧美群妇大交群的观看方式| 午夜日韩在线电影| 91精品国产综合久久福利软件 | 精品一区二区三区香蕉蜜桃| 精品视频在线免费看| 一区二区三区四区在线| 欧美性生活久久| 午夜视频一区二区三区| 在线成人午夜影院| 久久成人免费电影| 久久日韩粉嫩一区二区三区| 国产精品白丝jk白祙喷水网站| 久久久久国色av免费看影院| 国产精品中文字幕一区二区三区| 国产日韩视频一区二区三区| 成人高清视频在线观看| 成人免费在线播放视频| 欧美影院精品一区| 另类调教123区| 久久精品这里都是精品| 不卡的av网站| 亚洲电影一级黄| 精品国产乱码久久久久久影片| 国产超碰在线一区| 一区二区欧美国产| 日韩欧美不卡一区| 成人久久18免费网站麻豆| 亚洲人成影院在线观看| 在线播放欧美女士性生活| 久99久精品视频免费观看| 日本一二三不卡| 欧美色精品在线视频| 国产中文一区二区三区| 亚洲色图20p| 日韩亚洲欧美高清| 成人黄色大片在线观看| 日日噜噜夜夜狠狠视频欧美人| 日韩欧美的一区| 色综合久久久久综合体| 日本三级亚洲精品| 1024精品合集| 精品日韩在线一区| 在线看日本不卡| 国产成人精品亚洲午夜麻豆| 一个色妞综合视频在线观看| 久久久久亚洲综合| 欧美日韩精品免费| 国产风韵犹存在线视精品| 亚洲国产综合色| 日本一区二区三区国色天香| 91精品国产色综合久久不卡蜜臀| 成人性生交大合| 久久精品99国产精品日本| 一区二区三国产精华液| 欧美激情一区二区三区| 精品日本一线二线三线不卡| 欧美色综合网站| 97精品国产露脸对白| 国产精品一区二区三区四区| 五月综合激情网| 亚洲免费观看高清完整版在线观看| 日韩欧美亚洲国产精品字幕久久久| 欧美亚洲高清一区| 99久久伊人网影院| 国产成a人无v码亚洲福利| 韩国成人福利片在线播放| 亚洲综合激情另类小说区| 中文字幕精品—区二区四季| 精品国产乱码久久久久久久| 欧美另类videos死尸| 欧美在线视频日韩| 日本国产一区二区| 91美女福利视频| 99国产精品国产精品毛片| 国产美女娇喘av呻吟久久| 免费观看在线色综合| 日韩综合在线视频| 日韩精品久久久久久| 亚洲高清免费观看| 天堂成人国产精品一区| 亚洲va欧美va人人爽午夜| 亚洲一区成人在线| 丝袜诱惑亚洲看片| 五月婷婷综合激情| 爽爽淫人综合网网站| 日韩电影在线免费观看| 日韩精品视频网站| 全国精品久久少妇| 蜜桃传媒麻豆第一区在线观看| 奇米精品一区二区三区在线观看一 | 99综合影院在线| av亚洲产国偷v产偷v自拍| 99精品在线观看视频| 色综合久久精品| 日本韩国欧美一区二区三区| 欧美在线免费视屏| 91精品国产综合久久香蕉的特点 | 亚洲视频狠狠干| 亚洲美女电影在线| 亚洲在线观看免费| 天天综合天天做天天综合| 蜜臀a∨国产成人精品| 韩日欧美一区二区三区| 国产高清无密码一区二区三区| 国产成人午夜精品5599| 97久久久精品综合88久久| 欧美日韩一区二区欧美激情| 7777精品伊人久久久大香线蕉完整版 | 在线不卡a资源高清| 欧美岛国在线观看| 国产精品丝袜久久久久久app| √…a在线天堂一区| 日韩精品一二三区| 国产成人鲁色资源国产91色综| 一本久久a久久精品亚洲| 制服.丝袜.亚洲.中文.综合| 久久影院视频免费| 亚洲精品国久久99热| 日韩av电影一区| a4yy欧美一区二区三区| 欧美一区二区视频免费观看| 国产亚洲女人久久久久毛片| 亚洲免费在线观看| 久久99精品久久只有精品| 成人免费毛片a| 欧美精品日韩精品| 国产精品乱码人人做人人爱 | 91精品国产色综合久久久蜜香臀| 久久免费美女视频| 午夜精品福利久久久| 国产成人免费xxxxxxxx| 制服丝袜在线91| 国产精品国产三级国产普通话三级| 亚洲第一电影网| 99久久综合精品| 久久午夜电影网| 天堂av在线一区| 日本精品裸体写真集在线观看| 精品成人在线观看| 五月婷婷综合网| 日本道精品一区二区三区| 久久久久国产一区二区三区四区| 亚洲国产色一区| 成人av高清在线| 久久蜜桃av一区精品变态类天堂| 亚洲国产视频在线| 色综合久久88色综合天天免费| 久久无码av三级| 老汉av免费一区二区三区| 在线亚洲高清视频| 亚洲欧美一区二区不卡| 国产不卡免费视频| 久久久久久久久久久久久女国产乱| 亚洲电影第三页| 在线免费观看视频一区| 亚洲欧美日韩中文字幕一区二区三区| 国产米奇在线777精品观看| 91精品国产福利| 天堂在线一区二区| 欧美精品久久99| 亚洲成av人片一区二区三区| 91黄色小视频| 最新国产成人在线观看| 97精品久久久久中文字幕 | 色婷婷综合久久久| 综合色天天鬼久久鬼色| 国产精品一区二区果冻传媒| www国产精品av| 极品尤物av久久免费看| 久久美女艺术照精彩视频福利播放| 蜜桃视频在线一区| 精品少妇一区二区| 国产精品99精品久久免费|