@Override public void onClick(View v) { if ((isTurn && cardSelected != null) && currentState != FIRST_ROUND_BETTING && currentState != SECOND_ROUND_BETTING && (currentState == PICK_IT_UP || currentState == PLAY_LEAD_CARD || gameRules.checkCard(cardSelected, trumpSuit, cardLead, cardHand)) && cardHand.size() != 0) { // play card or discard if it is pick_it_up mode connection.write(currentState, cardSelected); playerContext.removeFromHand(cardSelected.getIdNum()); cardSelected = null; if (currentState == PICK_IT_UP) { play.setText(R.string.Play); playingView(); } setButtonsEnabled(false); isTurn = false; cardSuggestedId = -1; playerContext.setSelected(-1, cardSuggestedId); } else if (currentState == FIRST_ROUND_BETTING || currentState == SECOND_ROUND_BETTING) { EuchreBet bet = new EuchreBet(trumpSuit, false, false); connection.write(currentState, bet.toString()); isBettingNow = false; playingView(); setButtonsEnabled(false); isTurn = false; cardSuggestedId = -1; playerContext.setSelected(-1, cardSuggestedId); } }
/** * Used to set the play and draw buttons to enable or disabled Also if it is the player's turn * then set the cards to be greyed out if they are not playable. if it is not the player's turn * then do not grey out any cards * * @param isEnabled */ private void setButtonsEnabled(boolean isEnabled) { play.setEnabled(isEnabled); bet.setEnabled(isEnabled); goAlone.setEnabled(isEnabled); if (isEnabled && currentState == MSG_PLAY_CARD) { // it is your turn grey out cards for (Card c : cardHand) { boolean isPlayable = gameRules.checkCard(c, trumpSuit, cardLead, cardHand); playerContext.setCardPlayable( c.getIdNum(), isPlayable || currentState == PICK_IT_UP || currentState == PLAY_LEAD_CARD); } } else { // it is not your turn make cards normal if (playerHandLayout != null) { for (int i = 0; i < playerHandLayout.getChildCount(); i++) { ImageView v = (ImageView) playerHandLayout.getChildAt(i); playerContext.setCardPlayable(v.getId(), true); } } } }
/* (non-Javadoc) * @see com.worthwhilegames.cardgames.shared.PlayerController#handleBroadcastReceive(android.content.Context, android.content.Intent) */ @Override public void handleBroadcastReceive(Context context, Intent intent) { String action = intent.getAction(); if (ConnectionConstants.MESSAGE_RX_INTENT.equals(action)) { String object = intent.getStringExtra(ConnectionConstants.KEY_MESSAGE_RX); int messageType = intent.getIntExtra(ConnectionConstants.KEY_MESSAGE_TYPE, -1); if (Util.isDebugBuild()) { Log.d(TAG, "message: " + object); } switch (messageType) { case MSG_SETUP: // Parse the Message if it was to start the game over cardHand.removeAll(cardHand); playerContext.removeAllCards(); try { JSONArray arr = new JSONArray(object); for (int i = 0; i < arr.length(); i++) { JSONObject obj = arr.getJSONObject(i); int suit = obj.getInt(KEY_SUIT); int value = obj.getInt(KEY_VALUE); int id = obj.getInt(KEY_CARD_ID); playerContext.addCard(new Card(suit, value, id)); } } catch (JSONException ex) { ex.printStackTrace(); } setButtonsEnabled(false); isTurn = false; cardSuggestedId = -1; break; case FIRST_ROUND_BETTING: /* purposely have both here */ case SECOND_ROUND_BETTING: mySM.sayBet(playerName); isTurn = true; this.setCardLead(object); currentState = messageType; if (currentState == SECOND_ROUND_BETTING) { trumpSuit = -1; } else { trumpSuit = cardLead.getSuit(); } // start select bet activity for round 1 // start select bet activity to let the player bet isBettingNow = true; bettingView(); isTurn = true; setButtonsEnabled(true); break; case PLAY_LEAD_CARD: playingView(); mySM.sayTurn(playerName); currentState = PLAY_LEAD_CARD; setButtonsEnabled(true); isTurn = true; break; case PICK_IT_UP: currentState = PICK_IT_UP; mySM.sayPickItUp(playerName); try { JSONObject obj = new JSONObject(object); int suit = obj.getInt(KEY_SUIT); int value = obj.getInt(KEY_VALUE); int id = obj.getInt(KEY_CARD_ID); playerContext.addCard(new Card(suit, value, id)); } catch (JSONException ex) { ex.printStackTrace(); } isTurn = true; dealerDiscardView(); break; case MSG_PLAY_CARD: playingView(); mySM.sayTurn(playerName); this.setCardLead(object); currentState = MSG_PLAY_CARD; setButtonsEnabled(true); isTurn = true; break; case Constants.MSG_SUGGESTED_CARD: if (isTurn && object != null && isPlayAssistMode) { try { JSONObject obj = new JSONObject(object); int id = obj.getInt(Constants.KEY_CARD_ID); cardSuggestedId = id; // Let the UI know which card was suggested int selectedId = -1; if (cardSelected != null) { selectedId = cardSelected.getIdNum(); } playerContext.setSelected(selectedId, cardSuggestedId); } catch (JSONException ex) { ex.printStackTrace(); } } case MSG_REFRESH: // Parse the refresh Message try { JSONArray arr = new JSONArray(object); JSONObject refreshInfo = arr.getJSONObject(0); isTurn = refreshInfo.getBoolean(Constants.KEY_TURN); currentState = refreshInfo.getInt(KEY_CURRENT_STATE); playerName = refreshInfo.getString(Constants.KEY_PLAYER_NAME); trumpSuit = refreshInfo.getInt(TRUMP); // add more refresh info here playerContext.removeAllCards(); JSONObject obj = arr.getJSONObject(1); int suit = obj.getInt(KEY_SUIT); int value = obj.getInt(KEY_VALUE); int id = obj.getInt(KEY_CARD_ID); cardLead = new Card(suit, value, id); // the 2nd through however many are the cards of the player for (int i = 2; i < arr.length(); i++) { obj = arr.getJSONObject(i); suit = obj.getInt(KEY_SUIT); value = obj.getInt(KEY_VALUE); id = obj.getInt(KEY_CARD_ID); playerContext.addCard(new Card(suit, value, id)); } } catch (JSONException ex) { ex.printStackTrace(); } if ((currentState == FIRST_ROUND_BETTING || currentState == SECOND_ROUND_BETTING)) { if (isTurn && isBettingNow) { // don't want to open 2 betting windows break; } else if (isTurn && !isBettingNow) { bettingView(); setButtonsEnabled(true); } else { playingView(); isTurn = false; cardSuggestedId = -1; setButtonsEnabled(isTurn); isBettingNow = true; } break; } else if (isTurn && currentState == PICK_IT_UP) { dealerDiscardView(); } else { playingView(); } setButtonsEnabled(isTurn); cardSelected = null; break; case ROUND_OVER: currentState = ROUND_OVER; break; case MSG_WINNER: playerContext.unregisterReceiver(); Intent winner = new Intent(playerContext, GameResultsActivity.class); winner.putExtra(GameResultsActivity.IS_WINNER, true); playerContext.startActivityForResult(winner, QUIT_GAME); break; case MSG_LOSER: playerContext.unregisterReceiver(); Intent loser = new Intent(playerContext, GameResultsActivity.class); loser.putExtra(GameResultsActivity.IS_WINNER, false); playerContext.startActivityForResult(loser, QUIT_GAME); break; } } }