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!!
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!!
Code:
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
class LODServer
{
ServerBase<ClientHandler> server = null;
ArrayList<ClientHandler> clients = null;
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;
Semaphore playerTurn = new Semaphore(1);
public void setup (int port, int players, String map) {
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");
c.serverMessage("Congratulations " + c.name + " you won!");
c.serverWin();
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);
}
public static void main(String args[])
{
if (args.length != 3) {
printUsageAndDie("Incorrect number of command line arguements");
} else {
int port = Integer.parseInt(args[0]);
if ((port <= 1024) || (port >= 65536)) {
printUsageAndDie("Invalid port number");
}
int players = Integer.parseInt(args[1]);
if (players < 0) {
printUsageAndDie("Invalid number of users");
}
LODServer s = new LODServer();
s.setup(port,players,args[2]);
try {
s.play();
} catch (InterruptedException e) {
System.err.println("Caught InterruptedException(?)");
System.exit(1);
}
}
}
}