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

📄 client.cs

📁 this is a good book for the visual c#
💻 CS
📖 第 1 页 / 共 2 页
字号:
         this.panel3.MouseUp += new System.Windows.Forms.MouseEventHandler(this.square_MouseUp);
         // 
         // displayTextBox
         // 
         this.displayTextBox.Location = new System.Drawing.Point(8, 184);
         this.displayTextBox.Multiline = true;
         this.displayTextBox.Name = "displayTextBox";
         this.displayTextBox.ReadOnly = true;
         this.displayTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
         this.displayTextBox.Size = new System.Drawing.Size(272, 80);
         this.displayTextBox.TabIndex = 1;
         this.displayTextBox.Text = "";
         // 
         // idLabel
         // 
         this.idLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
         this.idLabel.Location = new System.Drawing.Point(8, 8);
         this.idLabel.Name = "idLabel";
         this.idLabel.TabIndex = 0;
         // 
         // Client
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(292, 273);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.panel9,
                                                                      this.panel8,
                                                                      this.panel7,
                                                                      this.panel6,
                                                                      this.panel5,
                                                                      this.panel3,
                                                                      this.panel2,
                                                                      this.panel1,
                                                                      this.displayTextBox,
                                                                      this.idLabel,
                                                                      this.panel4});
         this.Name = "Client";
         this.Text = "Tic Tac Toe Client";
         this.Closing += new System.ComponentModel.CancelEventHandler(this.Client_Closing);
         this.Paint += new System.Windows.Forms.PaintEventHandler(this.Client_Paint);
         this.ResumeLayout(false);

      }
		#endregion

      [STAThread]
      static void Main() 
      {
         Application.Run( new Client() );
      }

      protected void Client_Paint (
         object sender, System.Windows.Forms.PaintEventArgs e )
      {
         PaintSquares();
      }

      protected void Client_Closing(
         object sender, CancelEventArgs e )
      {
         done = true;
      }

      // draws the mark of each square
      public void PaintSquares()
      {
         Graphics g;

         // draw the appropriate mark on each panel
         for ( int row = 0; row < 3; row++ )
            for ( int column = 0; column < 3; column++ )
            {
               // get the Graphics for each Panel
               g = board[ row, column ].SquarePanel.CreateGraphics();

               // draw the appropriate letter on the panel
               g.DrawString( board[ row, column ].Mark.ToString(), 
                  this.Font, brush, 8, 8 );
            }
      } // end method PaintSquares

      // send location of the clicked square to server
      protected void square_MouseUp( 
         object sender, System.Windows.Forms.MouseEventArgs e )
      {
         // for each square check if that square was clicked
         for ( int row = 0; row < 3; row++ )
            for ( int column = 0; column < 3; column++ )
               if ( board[ row, column ].SquarePanel == sender )
               {
                  CurrentSquare = board[ row, column ];

                  // send the move to the server
                  SendClickedSquare( board[ row, column ].Location );
               }
      } // end method square_MouseUp

      // control thread that allows continuous update of the
      // textbox display
      public void Run()
      {
         // first get players's mark (X or O)
         myMark = reader.ReadChar();
         idLabel.Text = "You are player \"" + myMark + "\"";
         myTurn = ( myMark == 'X' ? true : false );

         // process incoming messages
         try
         {
            // receive messages sent to client
            while ( true )
               ProcessMessage( reader.ReadString() );
         }
         catch ( EndOfStreamException )
         {
            MessageBox.Show( "Server is down, game over", "Error",
               MessageBoxButtons.OK, MessageBoxIcon.Error );
         }

      } // end method Run

      // process messages sent to client
      public void ProcessMessage( string message )
      {
         // if the move player sent to the server is valid
         // update the display, set that square's mark to be
         // the mark of the current player and repaint the board
         if ( message == "Valid move." )
         {
            displayTextBox.Text += 
               "Valid move, please wait.\r\n";
            currentSquare.Mark = myMark;
            PaintSquares();
         }

            // if the move is invalid, display that and it is now
            // this player's turn again
         else if ( message == "Invalid move, try again" )
         {
            displayTextBox.Text += message + "\r\n";
            myTurn = true;
         }

            // if opponent moved
         else if ( message == "Opponent moved" )
         {
            // find location of their move
            int location = reader.ReadInt32();

            // set that square to have the opponents mark and
            // repaint the board
            board[ location / 3, location % 3 ].Mark =
               ( myMark == 'X' ? 'O' : 'X' );
            PaintSquares();

            displayTextBox.Text += 
               "Opponent moved.  Your turn.\r\n";

            // it is now this player's turn
            myTurn = true;
         }

            // display the message
         else
            displayTextBox.Text += message + "\r\n";

      } // end method ProcessMessage

      // sends the server the number of the clicked square
      public void SendClickedSquare( int location )
      {
         // if it is the current player's move right now
         if ( myTurn )
         {
            // send the location of the move to the server
            writer.Write( location );

            // it is now the other player's turn
            myTurn = false;
         }
      }

      // write-only property for the current square
      public Square CurrentSquare
      {
         set
         {
            currentSquare = value;
         }
      }

   } // end class Client
}

/*
 **************************************************************************
 * (C) Copyright 2002 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 + -