Android Open Source - Briscola Caller Bid Select Activity






From Project

Back to project page Briscola.

License

The source code is released under:

GNU General Public License

If you think the Android project Briscola listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gmail.craptik.briscola;
/* w w  w  .jav a 2  s  .  c  o  m*/
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.ToggleButton;

public class CallerBidSelectActivity extends Activity
{
  private Player caller;
  private int callerBid;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_caller_bid_select);
  }

  @Override
  public void onResume()
  {
    super.onResume();

    GameData gameData = NewGameActivity.getGameData();

    TextView firstPlayerTextView = (TextView) findViewById(R.id.caller_bid_player_one);
    firstPlayerTextView.setText(gameData.getPlayers().get(0).getName());

    TextView secondPlayerTextView = (TextView) findViewById(R.id.caller_bid_player_two);
    secondPlayerTextView.setText(gameData.getPlayers().get(1).getName());

    TextView thirdPlayerTextView = (TextView) findViewById(R.id.caller_bid_player_three);
    thirdPlayerTextView.setText(gameData.getPlayers().get(2).getName());

    TextView fourthPlayerTextView = (TextView) findViewById(R.id.caller_bid_player_four);
    fourthPlayerTextView.setText(gameData.getPlayers().get(3).getName());

    TextView fifthPlayerTextView = (TextView) findViewById(R.id.caller_bid_player_five);
    fifthPlayerTextView.setText(gameData.getPlayers().get(4).getName());

    caller = gameData.getCaller();
    callerBid = gameData.getCallerBid();

    RadioGroup callerRadioGroup = (RadioGroup) findViewById(R.id.caller_bid_caller_radio_group);

    for(int i = 0; i < callerRadioGroup.getChildCount() - 1; i++)
    {
      ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(i)).getChildAt(0)).setChecked(false);
    }

    if(caller != null)
    {
      int ixCaller = gameData.findPlayer(caller);
      if(ixCaller == -1)
      {
        ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(5)).getChildAt(0)).setChecked(true);
      }
      else
      {
        ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(ixCaller)).getChildAt(0)).setChecked(true);
        ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(5)).getChildAt(0)).setChecked(false);
      }
    }
    else
    {
      ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(5)).getChildAt(0)).setChecked(true);
    }

    int ixNemesis = gameData.findNemesis();
    if(ixNemesis != -1)
    {
      ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(ixNemesis)).getChildAt(0)).setEnabled(false);
    }

    if(callerBid == -1)
    {
      // Set a normal default bid
      callerBid = 75;
    }

    setToggleFromBid();
    updateText();
  }

  public void onCallerRadioButtonClicked(View view)
  {
    RadioGroup callerRadioGroup = (RadioGroup) findViewById(R.id.caller_bid_caller_radio_group);
    for(int i = 0; i < callerRadioGroup.getChildCount(); i++)
    {
      ((RadioButton) ((FrameLayout) callerRadioGroup.getChildAt(i)).getChildAt(0)).setChecked(false);
    }

    ((RadioButton) view).setChecked(true);

    GameData gameData = NewGameActivity.getGameData();
    switch(view.getId())
    {
      case R.id.caller_bid_radio_one:
      {
        caller = gameData.getPlayers().get(0);
        break;
      }
      case R.id.caller_bid_radio_two:
      {
        caller = gameData.getPlayers().get(1);
        break;
      }
      case R.id.caller_bid_radio_three:
      {
        caller = gameData.getPlayers().get(2);
        break;
      }
      case R.id.caller_bid_radio_four:
      {
        caller = gameData.getPlayers().get(3);
        break;
      }
      case R.id.caller_bid_radio_five:
      {
        caller = gameData.getPlayers().get(4);
        break;
      }
      case R.id.caller_bid_radio_no_one:
      {
        caller = null;
        break;
      }
      default:
      {
        // Really should have each case...
        caller = null;
        assert false;
        break;
      }
    }
  }

  public void onNumberClicked(View view)
  {
    switch(view.getId())
    {
      case R.id.caller_bid_60:
      case R.id.caller_bid_70:
      case R.id.caller_bid_80:
      case R.id.caller_bid_90:
      case R.id.caller_bid_100:
      case R.id.caller_bid_110:
      {
        untoggleAllTens();
        setNonZeroOnesEnabled(true);

        break;
      }
      case R.id.caller_bid_120:
      {
        untoggleAllOnes();
        untoggleAllTens();
        ((ToggleButton) findViewById(R.id.caller_bid_0)).setChecked(true);
        setNonZeroOnesEnabled(false);

        break;
      }
      case R.id.caller_bid_0:
      case R.id.caller_bid_1:
      case R.id.caller_bid_2:
      case R.id.caller_bid_3:
      case R.id.caller_bid_4:
      case R.id.caller_bid_5:
      case R.id.caller_bid_6:
      case R.id.caller_bid_7:
      case R.id.caller_bid_8:
      case R.id.caller_bid_9:
      {
        untoggleAllOnes();
        break;
      }
      default:
      {
        // Forgot to cover a button
        break;
      }
    }

    ((ToggleButton) view).setChecked(true);

    setBidFromToggle();
    updateText();
  }

  public void confirm(View view)
  {
    GameData gameData = NewGameActivity.getGameData();

    gameData.setCaller(caller);
    gameData.setCallerBid(callerBid);

    finish();
  }

  private void untoggleAllTens()
  {
    ((ToggleButton) findViewById(R.id.caller_bid_60)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_70)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_80)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_90)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_100)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_110)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_120)).setChecked(false);
  }

  private void untoggleAllOnes()
  {
    ((ToggleButton) findViewById(R.id.caller_bid_0)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_1)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_2)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_3)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_4)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_5)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_6)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_7)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_8)).setChecked(false);
    ((ToggleButton) findViewById(R.id.caller_bid_9)).setChecked(false);
  }

  private void setNonZeroOnesEnabled(boolean enabled)
  {
    ((ToggleButton) findViewById(R.id.caller_bid_1)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_2)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_3)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_4)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_5)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_6)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_7)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_8)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.caller_bid_9)).setEnabled(enabled);
  }

  private void setToggleFromBid()
  {
    // Don't call this unless the callerBid is valid
    assert callerBid >= 60 && callerBid <= 120;

    untoggleAllOnes();
    untoggleAllTens();

    if(callerBid == 120)
    {
      ((ToggleButton) findViewById(R.id.caller_bid_120)).setChecked(true);
      setNonZeroOnesEnabled(false);
      return;
    }

    setNonZeroOnesEnabled(true);

    switch(callerBid / 10)
    {
      case 6:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_60)).setChecked(true);
        break;
      }
      case 7:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_70)).setChecked(true);
        break;
      }
      case 8:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_80)).setChecked(true);
        break;
      }
      case 9:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_90)).setChecked(true);
        break;
      }
      case 10:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_100)).setChecked(true);
        break;
      }
      case 11:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_110)).setChecked(true);
        break;
      }
      case 12:
      {
        // This is a special case that should already be covered
        assert false;
        break;
      }
      default:
      {
        // Shouldn't be missing any cases...
        assert false;
        break;
      }
    }

    switch(callerBid % 10)
    {
      case 0:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_0)).setChecked(true);
        break;
      }
      case 1:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_1)).setChecked(true);
        break;
      }
      case 2:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_2)).setChecked(true);
        break;
      }
      case 3:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_3)).setChecked(true);
        break;
      }
      case 4:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_4)).setChecked(true);
        break;
      }
      case 5:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_5)).setChecked(true);
        break;
      }
      case 6:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_6)).setChecked(true);
        break;
      }
      case 7:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_7)).setChecked(true);
        break;
      }
      case 8:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_8)).setChecked(true);
        break;
      }
      case 9:
      {
        ((ToggleButton) findViewById(R.id.caller_bid_9)).setChecked(true);
        break;
      }
      default:
      {
        // Shouldn't be missing any cases...
        assert false;
        break;
      }
    }
  }

  private void setBidFromToggle()
  {
    if(((ToggleButton) findViewById(R.id.caller_bid_120)).isChecked())
    {
      callerBid = 120;
      return;
    }

    if(((ToggleButton) findViewById(R.id.caller_bid_60)).isChecked())
    {
      callerBid = 60;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_70)).isChecked())
    {
      callerBid = 70;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_80)).isChecked())
    {
      callerBid = 80;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_90)).isChecked())
    {
      callerBid = 90;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_100)).isChecked())
    {
      callerBid = 100;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_110)).isChecked())
    {
      callerBid = 110;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_120)).isChecked())
    {
      // This special case should already be covered
      assert false;
      return;
    }
    else
    {
      // Shouldn't be missing any cases...
      assert false;
      return;
    }

    if(((ToggleButton) findViewById(R.id.caller_bid_0)).isChecked())
    {
      callerBid += 0;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_1)).isChecked())
    {
      callerBid += 1;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_2)).isChecked())
    {
      callerBid += 2;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_3)).isChecked())
    {
      callerBid += 3;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_4)).isChecked())
    {
      callerBid += 4;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_5)).isChecked())
    {
      callerBid += 5;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_6)).isChecked())
    {
      callerBid += 6;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_7)).isChecked())
    {
      callerBid += 7;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_8)).isChecked())
    {
      callerBid += 8;
    }
    else if(((ToggleButton) findViewById(R.id.caller_bid_9)).isChecked())
    {
      callerBid += 9;
    }
    else
    {
      // Shouldn't be missing any cases...
      assert false;
      return;
    }

    // We should get a valid score after all of this
    assert callerBid >= 60 && callerBid <= 120;
  }

  private void updateText()
  {
    TextView bidTextView = (TextView) findViewById(R.id.caller_bid_bid_text);
    bidTextView.setText(String.format(getString(R.string.caller_bid_text_format), callerBid));
  }
}




Java Source Code List

com.gmail.craptik.briscola.ActionAdapter.java
com.gmail.craptik.briscola.ActionItem.java
com.gmail.craptik.briscola.CallCardCupsSelectActivity.java
com.gmail.craptik.briscola.CallCardFeathersSelectActivity.java
com.gmail.craptik.briscola.CallCardSuitSelectActivity.java
com.gmail.craptik.briscola.CallCardSunsSelectActivity.java
com.gmail.craptik.briscola.CallCardSwordsSelectActivity.java
com.gmail.craptik.briscola.CalleeSelectActivity.java
com.gmail.craptik.briscola.CallerBidSelectActivity.java
com.gmail.craptik.briscola.Card.java
com.gmail.craptik.briscola.ECardNumber.java
com.gmail.craptik.briscola.ECardSuit.java
com.gmail.craptik.briscola.EGameProgress.java
com.gmail.craptik.briscola.FirstNemesisSelectActivity.java
com.gmail.craptik.briscola.GameData.java
com.gmail.craptik.briscola.IDatabase.java
com.gmail.craptik.briscola.MainActivity.java
com.gmail.craptik.briscola.NewGameActivity.java
com.gmail.craptik.briscola.PlayerAdapter.java
com.gmail.craptik.briscola.Player.java
com.gmail.craptik.briscola.PlayersSelectActivity.java
com.gmail.craptik.briscola.SQLGameDatabase.java
com.gmail.craptik.briscola.ScoreSelectActivity.java
com.gmail.craptik.briscola.TestDatabase.java