Class GameHandler

java.lang.Object
com.alessiocascini.battleship.server.GameHandler
All Implemented Interfaces:
Runnable

public class GameHandler extends Object implements Runnable
Manages the gameplay logic for an individual turn. This class handles the communication between the two players, tracks whose turn it is, and monitors the number of sunken ships to determine the game outcome. Since multiple instances run in different threads, it uses static fields to maintain a shared game state.
Author:
Alessio Cascini
  • Constructor Details

    • GameHandler

      public GameHandler(Socket clientSocket)
      Constructs a GameHandler for the connected client.
      Parameters:
      clientSocket - The socket connection to the player
  • Method Details

    • run

      public void run()
      Main game loop logic for a single turn request: 1. Verifies if it is the player's turn. 2. Processes the move through the Server logic. 3. Updates the win/loss state if a ship is sunk. 4. Waits and notifies the player of the opponent's subsequent move.
      Specified by:
      run in interface Runnable