Search in sources :

Example 1 with ProtocolMessageTurnOver

use of nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver in project Samples-for-Java by blackberry.

the class GameScreen method gameMessage.

public void gameMessage(ProtocolMessage message) {
    Utilities.log("XXXX gameMessage:" + message);
    // should indicate it's now our turn
    if (message instanceof ProtocolMessageTurnOver) {
        // OK, it's our turn now!
        ProtocolMessageTurnOver pmsg_to = (ProtocolMessageTurnOver) message;
        Utilities.log("XXXX gameMessage: ProtocolMessageTurnOver");
        game_state.setBoardLocked(false);
        int other_symbol = pmsg_to.get_symbol_played();
        // derive the symbol we're playing with from the one player 1 is using (they got to choose)
        if (other_symbol == Constants.PLAYER_SYMBOL_CROSS) {
            _symbol = Constants.PLAYER_SYMBOL_NOUGHT;
        } else {
            _symbol = Constants.PLAYER_SYMBOL_CROSS;
        }
        cmd_select_tile.set_symbol(_symbol);
        int other_player_tile = pmsg_to.get_tile_changed();
        game_state.updateTileOtherPlayer(other_player_tile, other_symbol);
        setStatusMessage("It's your turn now!");
        proto.stopListeningForMessages();
        return;
    }
    if (message instanceof ProtocolMessageMasterBid) {
        // from this screen, this means "let's start a new game"
        // we may or may not have already sent our bid
        Utilities.log("XXXX gameMessage: ProtocolMessageMasterBid: game_state.isBid_sent()=" + game_state.isBid_sent());
        boolean ok = true;
        other_bid = (ProtocolMessageMasterBid) message;
        game_state.setOther_bid_received(other_bid);
        // if we've received this message then one way or another it must be game over (maybe the other device has won)
        game_state.setGame_over(true);
        if (!game_state.isBid_sent()) {
            my_bid = new ProtocolMessageMasterBid();
            try {
                proto.sendMasterBid(my_bid);
                ok = true;
            } catch (NFCException e1) {
                ok = false;
                Utilities.log("XXXX " + e1.getClass().getName() + ":" + e1.getMessage());
                setStatusMessage("Error - try again");
            }
        }
        proto.stopListeningForMessages();
        if (ok) {
            checkBids();
        }
        return;
    }
    if (message instanceof ProtocolMessageResetBidState) {
        Utilities.log("XXXX gameMessage: ProtocolMessageResetBidState");
        proto.stopListeningForMessages();
        setStatusMessage("Please touch devices again");
        resetBidStateTracking();
        return;
    }
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) ProtocolMessageTurnOver(nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver) ProtocolMessageResetBidState(nfc.sample.tictactoe.protocol.ProtocolMessageResetBidState) NFCException(net.rim.device.api.io.nfc.NFCException)

Example 2 with ProtocolMessageTurnOver

use of nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver in project Samples-for-Java by blackberry.

the class GameScreen method initialiseGame.

private void initialiseGame() {
    turn_over = new ProtocolMessageTurnOver();
    game_state.initialiseGameState();
    // focus on central tile
    focused_tile_row = 1;
    focused_tile_col = 1;
    synchronized (UiApplication.getEventLock()) {
        tiles[4].setFocus();
    }
    game_state.setCurrent_tile(Constants.INITIAL_TILE);
    if (_player_no == Constants.PLAYER_1) {
        cmd_select_tile.set_symbol(_symbol);
        game_state.setGameState(GameState.GAME_STATE_CHANGE_MY_TURN_NOW);
    } else {
        game_state.setBoardLocked(true);
        proto.listenForMessages();
    }
}
Also used : ProtocolMessageTurnOver(nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver)

Aggregations

ProtocolMessageTurnOver (nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver)2 NFCException (net.rim.device.api.io.nfc.NFCException)1 ProtocolMessageMasterBid (nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid)1 ProtocolMessageResetBidState (nfc.sample.tictactoe.protocol.ProtocolMessageResetBidState)1