Search in sources :

Example 1 with ProtocolMessageMasterBid

use of nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid 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 ProtocolMessageMasterBid

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

the class GameScreen method staleMate.

private void staleMate() {
    game_state.lockBoard();
    setStatusMessage(Constants.STALE_MATE_MESSAGE, true);
    for (int i = 0; i < 9; i++) {
        if (game_state.getTileState(i) == Constants.PLAYER_SYMBOL_CROSS) {
            tiles[i].setImage(stale_mate_cross_bmp);
        } else {
            tiles[i].setImage(stale_mate_nought_bmp);
        }
    }
    game_state.setGame_over(true);
    my_bid = new ProtocolMessageMasterBid();
    try {
        proto.sendMasterBid(my_bid);
        proto.listenForMessages();
    } catch (NFCException e) {
        Utilities.log("XXXX " + e.getClass().getName() + ":" + e.getMessage());
        setStatusMessage("It didn't work, please try again");
    }
    Utilities.popupMessage("Stale mate!");
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) NFCException(net.rim.device.api.io.nfc.NFCException)

Example 3 with ProtocolMessageMasterBid

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

the class StartGameScreen method initNewGameMessaging.

public void initNewGameMessaging() {
    try {
        // send bid message over SNEP
        proto = new GameProtocol(this);
        my_bid = new ProtocolMessageMasterBid();
        proto.sendMasterBid(my_bid);
        proto.listenForMessages();
    } catch (NFCException e) {
        Utilities.log("XXXX StartGameScreen:" + e.getClass().getName() + ":" + e.getMessage());
        Utilities.popupMessage("Error: separate devices and try again!");
    }
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) NFCException(net.rim.device.api.io.nfc.NFCException) GameProtocol(nfc.sample.tictactoe.protocol.GameProtocol)

Example 4 with ProtocolMessageMasterBid

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

the class GameScreen method gameWon.

private void gameWon() {
    game_state.lockBoard();
    // we may already be listening so remove the NDEF listener in anticipation of adding it
    proto.stopListeningForMessages();
    // again shortly
    setStatusMessage(Constants.WINNER_MESSAGE, true);
    int[] win_line = game_state.getWinningLine(_symbol);
    if (_symbol == Constants.PLAYER_SYMBOL_CROSS) {
        win_line_bmp = BitmapFactory.addStateIndicator(cross_unfocused, Constants.WIN_LINE_COLOUR);
    } else {
        win_line_bmp = BitmapFactory.addStateIndicator(nought_unfocused, Constants.WIN_LINE_COLOUR);
    }
    for (int i = 0; i < 3; i++) {
        tiles[win_line[i]].setImage(win_line_bmp);
    }
    game_state.setGame_over(true);
    my_bid = new ProtocolMessageMasterBid();
    try {
        proto.sendMasterBid(my_bid);
        proto.listenForMessages();
    } catch (NFCException e) {
        Utilities.log("XXXX " + e.getClass().getName() + ":" + e.getMessage());
        setStatusMessage("It didn't work, please try again");
    }
    Utilities.popupMessage("You have won!");
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) NFCException(net.rim.device.api.io.nfc.NFCException)

Example 5 with ProtocolMessageMasterBid

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

the class StartGameScreen method gameMessage.

public void gameMessage(ProtocolMessage message) {
    if (message instanceof ProtocolMessageMasterBid) {
        ProtocolMessageMasterBid other_bid = (ProtocolMessageMasterBid) message;
        int device_status = 0;
        boolean initialised = false;
        if (my_bid.getBid() > other_bid.getBid()) {
            Utilities.log("XXXX won the bid: player 1");
            device_status = Constants.PLAYER_1;
            initialised = true;
        } else {
            if (my_bid.getBid() < other_bid.getBid()) {
                Utilities.log("XXXX lost the bid: player 2");
                device_status = Constants.PLAYER_2;
                initialised = true;
            } else {
                Utilities.popupMessage("It didn't work, try again!");
            }
        }
        if (initialised) {
            try {
                proto.disableMessaging();
            } catch (NFCException e) {
                Utilities.log("XXXX StartGameScreen:" + e.getClass().getName() + ":" + e.getMessage());
                Utilities.popupMessage("We had a problem, please try again");
            }
            final int player_no = device_status;
            final MainScreen next_screen;
            if (player_no == Constants.PLAYER_2) {
                next_screen = GameScreen.getInstance(player_no);
            } else {
                next_screen = new SymbolSelectionScreen();
            }
            UiApplication.getUiApplication().invokeLater(new Runnable() {

                public void run() {
                    UiApplication.getUiApplication().pushScreen(next_screen);
                }
            });
        }
    }
}
Also used : ProtocolMessageMasterBid(nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid) NFCException(net.rim.device.api.io.nfc.NFCException) MainScreen(net.rim.device.api.ui.container.MainScreen)

Aggregations

NFCException (net.rim.device.api.io.nfc.NFCException)5 ProtocolMessageMasterBid (nfc.sample.tictactoe.protocol.ProtocolMessageMasterBid)5 MainScreen (net.rim.device.api.ui.container.MainScreen)1 GameProtocol (nfc.sample.tictactoe.protocol.GameProtocol)1 ProtocolMessageResetBidState (nfc.sample.tictactoe.protocol.ProtocolMessageResetBidState)1 ProtocolMessageTurnOver (nfc.sample.tictactoe.protocol.ProtocolMessageTurnOver)1