Hello,

I urgently need help, im trying to complete a class which will be able of multiple - clients but is not doing the required puprose. Can anyone be assistance of what is needed to add for handling the clients ??

Thanks in advance!!

import java.util.ArrayList;
import java.util.concurrent.Semaphore;

class LODServer
{
     ServerBase<ClientHandler> server = null;

    /*
    ** Once the given number of players has connected,
    ** the instance of ServerBase<ClientHandler> returns
    ** an array of them.
    */
    ArrayList<ClientHandler> clients = null;

    /*
    ** This is the data that encodes the state of the game.
    ** The labyrinth is stored in a 2D array with
    ** numbers corresponding to the contents of each location.
    */
    static final int TREASURE     = 1;
    /* Larger positive numbers are several lots of treasure */
    static final int EMPTY        = 0;
    static final int HEALTH       = -1;
    static final int LANTERN      = -2;
    static final int SWORD        = -3;
    static final int ARMOUR       = -4;
    static final int EXIT         = -5;
    static final int WALL         = -6;

    int mapWidth = 5;
    int mapHeight = 5;
    int map[/*mapHeight*/][/*mapWidth*/] =
    {
	{WALL,    WALL,     ARMOUR,  WALL,     WALL    },
	{WALL,    EXIT,     EMPTY,   TREASURE, TREASURE},
	{LANTERN, TREASURE, EMPTY,   HEALTH,   LANTERN },
	{WALL,    SWORD,    EMPTY,   ARMOUR,   TREASURE},
	{WALL,    WALL,     SWORD,   WALL,     WALL    }
    };

    boolean gameStarted = false;
    boolean gameFinished = false;
    int goal = 2;           // The amount of treasure to collect

    /*
    ** The server has to be able to wait until the end of the player's turn.
    ** It uses a semaphore to do this.
    ** The player acquires the semaphore at the start of the turn
    ** and releases it at the end of the turn.
    ** This allows the server to wait until the end of the player's turn by
    ** simply waiting for the semaphore to be released.
    */
    Semaphore playerTurn = new Semaphore(1);
	

    public void setup (int port, int players, String map) {
	// Limit to one player
	if (players > 1) {
	    System.err.println("The current implementation is limited to one player.");
	    players = 1;
	}

	// Create the object to handle the incoming connections
	// ( you don't have to understand how this works )
	// If the last arguement is true then all incoming and outgoing messages
	// to the instances of ClientHandler will be logged.
	// Set to false if you don't want this.
	server = new ServerBase<ClientHandler>(ClientHandler.class, players, port, true);



	// Any set up for the game that can be done
	// before the clients connect goes here...

	// Map name is currently ignored
	System.err.println("Map loading is not implemented, \"" + map + "\" ignored.");



	// Start accepting incoming connections.
	System.out.print("Waiting for players to connect ... ");
	server.start();
    }

    public void play () throws InterruptedException {

	// Wait for all of the clients to connect
	clients = server.getConnections();
	System.out.println(" done.");

	// Any set up for the game that is specific
	// to the clients goes here.


	// Give each ClientHandler a reference to this object
	// and thus access to the game data
	for (ClientHandler c : clients) {
	    c.server = this;
	}

	// Fix the player's starting location
	clients.get(0).x = 2;
	clients.get(0).y = 2;

	// Let each client know the amount of treasure it needs
	for (ClientHandler c : clients) {
	    c.serverGoal(goal);
	}

	// Play the game!
	System.out.println("Starting the game.");
	gameStarted = true;
	while (gameFinished == false) {

	    // Check that there is at least one player connected
	    if (clients.size() <= 0) {
		System.out.println("Game abandoned");
		break;
	    }

	    // Each player takes a turn
	    //for (ClientHandler c : clients) {
	    ClientHandler c = clients.get(0);

	    // Start turn
	    System.out.println("Starting the turn of player \"" + c.name + "\"");
	    c.startTurn();     // During this, c will acquire() the playerTurn semaphore

	    // Wait for the player to finish their turn
	    // (they will release() the semaphore at the end of their turn)
	    try {
		playerTurn.acquire();
	    } catch (InterruptedException e) {
		// This shouldn't happen in normal operation
		// If it happens, it is because this code
		// has been used in a larger bit of software
		// thus the wrapper can deal with this condition
		throw(e);
	    }
	    System.out.println("End of turn");

	    // Check to see if the current player has won
	    // (gameFinished is set to true in ClientHandler.clientEndTurn())
	    if (gameFinished == true) {

		System.out.println("Game over, \"" + c.name + "\" is the winner");

		// Tell the player they have won
		c.serverMessage("Congratulations " + c.name + " you won!");
		c.serverWin();

		// Tell everyone else that they haven't
		for (ClientHandler d : clients) {

		    if (d != c) {
			d.serverMessage("Sorry " + d.name + " it looks like " + c.name + " beat you.");
			d.serverLose();
		    }

		}

	    }

	    // Release the semaphore so that the next player can start their turn
	    playerTurn.release();
	    //}


	}
	    
	return;

    }

    // A simple helper function
    public static void printUsageAndDie(String error) {
	System.err.println(error);
	System.err.println("Usage:");
	System.err.println("\tjava LODServer <port> <players> <map>");
	System.exit(1);
    }

    // The main method just handles the command line arguements
    // creates one instance of LODServer and starts it running
    public static void main(String args[])
    {

	// Check the command line arguements
	if (args.length != 3) {
	    printUsageAndDie("Incorrect number of command line arguements");
	} else {
	    // Parse the command line arguements
	    int port = Integer.parseInt(args[0]);

	    // Only ports 1025 to 65535 are suitable
	    if ((port <= 1024) || (port >= 65536)) {
		printUsageAndDie("Invalid port number");
	    }

	    int players = Integer.parseInt(args[1]);
	    if (players < 0) {
		printUsageAndDie("Invalid number of users");
	    }

	    // Create a LODServer object to run the game
	    LODServer s = new LODServer();

	    // Set it up
	    s.setup(port,players,args[2]);
	
	    // Go!
	    try {
		s.play();
	    } catch (InterruptedException e) {
		// Something has gone wrong - abort
		System.err.println("Caught InterruptedException(?)");
		System.exit(1);
	    }
	}
    }
}

What is the proplem exact you are facing, and also i noticed you are commenting the part in the code that loops over the clients to give each players his turn

    // Each player takes a turn
    //for (ClientHandler c : clients) {
    ClientHandler c = clients.get(0);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.