FileDocCategorySizeDatePackage
RegistrarImpl.javaAPI DocExample2646Tue Dec 12 18:58:40 GMT 2000bingo.game

RegistrarImpl

public class RegistrarImpl extends UnicastRemoteObject implements Registrar, Constants

Fields Summary
private RingMaster
ringMaster
private GameParameters
gameParameters
private Roster
roster
Constructors Summary
RegistrarImpl(RingMaster ringMaster)

	super();
	this.ringMaster = ringMaster;
	gameParameters = ringMaster.getGameParameters();
	roster = ringMaster.getRoster();
    
Methods Summary
public AnswerBINGO(int playerID, Card c)


	PlayerRecord p = roster.searchForPlayerWithID(playerID);

	if (p == null)
	    return new Answer(false, "Can't find player with ID: " + playerID + ".");

	if (p.wolfCries >= MAX_WOLF_CRIES)
	    return new Answer(false, "Sorry, wolf cryer, you're out of the game.");

	synchronized (ringMaster) {

	    ringMaster.setCheckingForWinner();

	    if (ringMaster.verify(c)) {

	        ringMaster.setGameOver();
	        return new Answer(true, "You won! Congratulations!");

	    } else {

		p.wolfCries++;
	        ringMaster.setGameResumed();
	        ringMaster.sendPlayerStatusMessage(p);
		if (p.wolfCries == MAX_WOLF_CRIES) {
	            return new Answer(false, "You've cried wolf 3 times. You're out.");
		} else {
	            return new Answer(false, "You cried wolf..." +
				      (MAX_WOLF_CRIES - p.wolfCries) +
				      " more and you're out.");
		}
	    }
	}
    
public TicketmayIPlay(java.lang.String playerName, int numCards, long seed)


	if (!ringMaster.ready())
	    return new Ticket("BINGO server not ready. You can't play.");
	else if (!ringMaster.isRegistering())
	    return new Ticket("Registration not open. You can't play.");
	    
	if (numCards > gameParameters.getMaxCards())
	    numCards = gameParameters.getMaxCards();

	synchronized (roster) {
	    if (roster.size() == gameParameters.getMaxPlayers())
	        return new Ticket("Game full. You can't play.");

	    Card[] cards = new Card[numCards];
            Random generator = new Random(seed);

	    for (int i = 0; i < numCards; i ++)
	        cards[i] = new Card(generator);

	    ringMaster.signTheCards(cards);

	    PlayerRecord p = new PlayerRecord(roster.nextPlayerID(), playerName, numCards);
	    String welcomeMessage = "Welcome to game # " + ringMaster.getGameNumber() + ".";
	    Ticket ticket = new Ticket(welcomeMessage, p.ID, cards);
	    roster.addElement(p, ringMaster);
	    return ticket;
	}
    
public java.lang.StringwhatsHappening()

	return ringMaster.statusString();