handleasession.java

来自「一个自己做的battleShip 的java 网络游戏。。。供大家交流」· Java 代码 · 共 92 行

JAVA
92
字号
package Server;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import Utility.*;

// Define the thread derived class for handling a new session for two players

public class HandleASession extends Thread implements BattleShipConstants 
{
   private Socket client;

   NavalForce nf = new NavalForce();
   // ServerShots keeps tracks of all locations shot by server thus avoiding repetition
   ServerShots ss = new ServerShots(100);   

   private DataInputStream fromClient;
   private DataOutputStream toClient;

   /** This class encapsulates the socket and run as a separate thread.
	 It constructs a NavalForces object conatining 2 ships from 5 different categories
       without any overlap.
   */
   
   public HandleASession(Socket client) 
   {
     this.client = client;
     nf.createRandom(5,2);
   }
	
   public void bomb()
   {
       try {
//       Thread.sleep(100); 
         int n = ss.shootAt();
         toClient.writeInt(BOMBED_AT);
         toClient.writeInt(n);
       }
       catch(Exception e) {}
   }  

   /** Implements the server side of the protocol using the Damage class 
       One form of the Damage constructor reads the damage details directly from the socket
       If the server scores a hit it gets to shoot again unless all enemy ships are destroyed
       in which case the server is the winner

	 If the client is currently shooting and has caused some damage it must be reported to the client.
       The second form of Damage constructor assess the extent of damage, taking the referece to the NavalForces 
       object and the current position hit.
       If the client has destroyed all the ships then client is declared the winner.
	 If the client has failed to hit any target the server gets to shoot.
   */


   public void run() {
      try {
         // Create data input and output streams
         fromClient = new DataInputStream(client.getInputStream());
         toClient = new DataOutputStream(client.getOutputStream());

         while (true)
         {
            int header = fromClient.readInt();
            if ( header == RESPONSE ) 
            {
               Damage enemyDamage = new Damage(fromClient);
               if (enemyDamage.isShipHit())
               {
                  if (enemyDamage.areAllShipsDestroyed() ) break;  // server has won
                   bomb();               
               }  
            }  
            else if ( header == BOMBED_AT )
            {
               int pos =  fromClient.readInt();
               Damage ourDamage = new Damage(nf,pos);
               if (ourDamage.isShipHit() )
               { 
                  ourDamage.send(toClient);
                  if ( ourDamage.areAllShipsDestroyed() )  break;  // client has won 
               }
               else bomb();
            }
         }
    }
    catch(Exception ex) {
      System.err.println(ex);
    }
  }
}

⌨️ 快捷键说明

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