FileDocCategorySizeDatePackage
RingMaster.javaAPI DocExample4757Tue Dec 12 18:58:42 GMT 2000bingo.game

RingMaster

public class RingMaster extends Object implements States

Fields Summary
private SocketGate
socketGate
private GameParameters
gameParameters
private NotaryPublic
notary
private Roster
roster
private Stack
announcedBalls
private int
gameNumber
private int
state
Constructors Summary
RingMaster()


       
	socketGate = new SocketGate();
	gameParameters = new GameParameters();
	notary = new NotaryPublic();
	roster = new Roster();
	announcedBalls = new Stack();
	announcedBalls.push(new BingoBall(BingoBall.FREE_SPACE));
    
Methods Summary
voidannounceBall(BingoBall b)

	socketGate.sendBall(b);
	socketGate.sendGameStatusMessage(statusString());
	announcedBalls.push(b);
    
synchronized booleangameInProgress()

	return (state == PLAYING || state == CHECKINGFORWINNER) ? true : false;
    
intgetGameNumber()

	return gameNumber;
    
GameParametersgetGameParameters()

	return gameParameters;
    
RostergetRoster()

	return roster;
    
booleanisCheckingForWinner()

	return (state == CHECKINGFORWINNER) ? true : false;
    
booleanisCountingDown()

	return (state == COUNTINGDOWN) ? true : false;
    
synchronized booleanisRegistering()

	return (state == WAITING || state == COUNTINGDOWN) ? true : false;
    
booleanready()

	return (state == BEFOREINITIALIZATION) ? false : true;
    
voidsendPlayerStatusMessage(PlayerRecord p)

	socketGate.sendPlayerStatusMessage(p);
    
voidsendTimeRemainingMessage(int timeRemaining)

	socketGate.sendGameStatusMessage(statusString(timeRemaining));
    
voidsetCheckingForWinner()

	state = CHECKINGFORWINNER;
    
synchronized voidsetGameOver()

	state = GAMEOVER;
        announceBall(new BingoBall(BingoBall.GAME_OVER));
	announcedBalls.removeAllElements();
	announcedBalls.push(new BingoBall(BingoBall.FREE_SPACE));
	roster.removeAllElements();
	notifyAll();
    
synchronized voidsetGameResumed()

	if (state == CHECKINGFORWINNER)
	    state = PLAYING;
    
voidsignTheCards(Card[] cards)

	try {
	    for (int i = 0; i < cards.length; i++)
	        notary.signTheCard(cards[i], gameNumber);
	} catch (Exception e) {
	    // PENDING: can't sign the cards, what to do?
	}
    
synchronized voidstartCountDown()

	state = COUNTINGDOWN;
	notifyAll();
    
voidstartGame()

	state = PLAYING;
	socketGate.sendGameStatusMessage("Game Starting...");
    
java.lang.StringstatusString()

	return statusString(-1);
    
private java.lang.StringstatusString(int anArgument)

	switch (state) {
	case BEFOREINITIALIZATION:
	    return "The BINGO server isn't ready yet.";
	case WAITING:
	    return "Waiting for first player to register for game # " + gameNumber + ".";
	case COUNTINGDOWN:
	    return "Game # " + gameNumber + " starts in "
		   + anArgument + " seconds.";
	case PLAYING:
	    return "Game #" + gameNumber + " in progress. "
		   + announcedBalls.size() + " balls announced.";
	case CHECKINGFORWINNER:
	    return "Game paused while checking for winner."
		   + announcedBalls.size() + " balls announced.";
	case GAMEOVER:
	    return "Game over.";
	default:
	    return "Nuttin'.";
	}
    
booleanverify(Card c)


	int colMatches = 0;
	int rowMatches = 0;
	int diagMatches = 0;
	int otherDiagMatches = 0;

	if (!(notary.verifyTheSignature(c, gameNumber)))
	    return false;

	for (int i = 0; i < Card.SIZE; i ++) {
	    for (int j = 0; j < Card.SIZE; j ++) {
		if (announcedBalls.contains(c.boardValues[i][j]))
		    rowMatches ++;
		if (announcedBalls.contains(c.boardValues[j][i]))
		    colMatches ++;
		if ((i == j) && announcedBalls.contains(c.boardValues[i][j]))
		    diagMatches ++;
		if (((i + j) == (Card.SIZE-1)) && announcedBalls.contains(c.boardValues[j][i]))
		    otherDiagMatches ++;
	    }
	    if (colMatches == Card.SIZE) {
		return true;
	    } else if (rowMatches == Card.SIZE) {
		return true;
	    } else {
		rowMatches = 0;
		colMatches = 0;
	    }
	}
	if (diagMatches == Card.SIZE) {
	    return true;
	} else if (otherDiagMatches == Card.SIZE) {
	    return true;
	}

	return false;
    
synchronized voidwaitForFirstPlayer()

	gameNumber++;
	state = WAITING;
	socketGate.sendGameStatusMessage(statusString());

        while (state == WAITING) {
            try {
               wait();
            } catch (InterruptedException e) {
            }
        }

        socketGate.sendGameStatusMessage("Beginning count down ... ");
    
synchronized voidwaitForGameToEnd()

        while (gameInProgress()) {
            try {
               wait();
            } catch (InterruptedException e) {
            }
        }