/**
* Runs the client using {@link #agent} for decision making.
*/
public void run() throws ServerException {
try {
Message msg = null;
setStreams(System.in, System.out, null);
msg = new MessageReady(agent.getName(), agent.getClientType());
sendMessage(msg);
tournament_loop:
while(true) {
msg = receiveMessage(null);
if(msg instanceof MessageStart) {
this.graph = ((MessageStart)msg).getGraph();
this.graphReadOnly = new GraphReadOnly(this.graph);
agent.start(graphReadOnly);
sentPlacement = false;
if(agent.getClientType() == ClientType.DETECTIVE)
sendMoves();
while(true) {
msg = receiveMessage(graph);
if(!(msg instanceof MessageUpdate))
throw new ProtocolException(String.format("Message 'update' was expected, got '%s'", msg.getMessageType()), null);
MessageUpdate msgUpdate = (MessageUpdate)msg;
// Update the agent, if nobody did win the game in the last round, or if there
// is information about movements of the other agent (e.g. about the movement
// of the detectives, that captured the phantom, or the movement of the phantom
// that commited suicide).
if(msgUpdate.getWinner() == null || msgUpdate.getMoves().length > 0)
receiveUpdate(msgUpdate);
// If there is a winner, end the round
if(msgUpdate.getWinner() != null) {
agent.end(msgUpdate.getWinner());
continue tournament_loop;
}
sendMoves();
}
}
else if(msg instanceof MessageQuit) {
agent.quit();
return;
}
else {
throw new ProtocolException("Unexpected message type: " + msg.getMessageType(), null);
}
}
}
catch(Throwable e) {
e.printStackTrace();