Android Open Source - Briscola Score 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;
//from ww  w.  j a  v a2  s. c  o  m
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ScoreSelectActivity extends Activity
{
  private int callerScore;
  private boolean callerPerspective;

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

    callerPerspective = true;
  }

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

    GameData gameData = NewGameActivity.getGameData();

    callerScore = gameData.getCallerScore();

    if(callerScore == -1)
    {
      // Set a normal default score
      callerScore = Math.min(gameData.getCallerBid() + 5, 120);
    }

    ((CompoundButton) findViewById(R.id.score_perspective_switch)).setChecked(callerPerspective);

    setToggleFromScore();
    updateText();
  }

  public void onTensClicked(View view)
  {
    if(view.getId() == R.id.score_hundred_twenty)
    {
      setNonZeroOnesEnabled(false);
      untoggleAllOnes();
      ((ToggleButton) findViewById(R.id.score_0)).setChecked(true);
    }
    else
    {
      setNonZeroOnesEnabled(true);
    }

    untoggleAllTens();
    ((ToggleButton) view).setChecked(true);
    setBidFromToggle();
    updateText();
  }

  public void onOnesClicked(View view)
  {
    untoggleAllOnes();
    ((ToggleButton) view).setChecked(true);
    setBidFromToggle();
    updateText();
  }

  public void togglePerspective(View view)
  {
    callerPerspective = ((CompoundButton) view).isChecked();
    setBidFromToggle();
    updateText();
  }

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

    gameData.setCallerScore(callerScore);

    finish();
  }

  private void untoggleAllTens()
  {
    ((ToggleButton) findViewById(R.id.score_zero_tens)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_ten)).setChecked(      false);
    ((ToggleButton) findViewById(R.id.score_twenty)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_thirty)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_forty)).setChecked(      false);
    ((ToggleButton) findViewById(R.id.score_fifty)).setChecked(      false);
    ((ToggleButton) findViewById(R.id.score_sixty)).setChecked(      false);
    ((ToggleButton) findViewById(R.id.score_seventy)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_eighty)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_ninety)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_hundred)).setChecked(    false);
    ((ToggleButton) findViewById(R.id.score_hundred_ten)).setChecked(  false);
    ((ToggleButton) findViewById(R.id.score_hundred_twenty)).setChecked(false);
  }

  private void untoggleAllOnes()
  {
    ((ToggleButton) findViewById(R.id.score_0)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_1)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_2)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_3)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_4)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_5)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_6)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_7)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_8)).setChecked(false);
    ((ToggleButton) findViewById(R.id.score_9)).setChecked(false);
  }

  private void setNonZeroOnesEnabled(boolean enabled)
  {
    ((ToggleButton) findViewById(R.id.score_1)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_2)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_3)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_4)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_5)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_6)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_7)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_8)).setEnabled(enabled);
    ((ToggleButton) findViewById(R.id.score_9)).setEnabled(enabled);
  }

  private void setToggleFromScore()
  {
    // Don't call this unless the callerScore is valid
    assert callerScore >= 0 && callerScore <= 120;

    int displayScore = callerPerspective ? callerScore : 120 - callerScore;

    untoggleAllOnes();
    untoggleAllTens();

    if(displayScore == 120)
    {
      ((ToggleButton) findViewById(R.id.score_hundred_twenty)).setChecked(true);
      ((ToggleButton) findViewById(R.id.score_0)).setChecked(true);
      setNonZeroOnesEnabled(false);
      return;
    }

    setNonZeroOnesEnabled(true);

    switch(displayScore / 10)
    {
      case 0:
      {
        ((ToggleButton) findViewById(R.id.score_zero_tens)).setChecked(true);
        break;
      }
      case 1:
      {
        ((ToggleButton) findViewById(R.id.score_ten)).setChecked(true);
        break;
      }
      case 2:
      {
        ((ToggleButton) findViewById(R.id.score_twenty)).setChecked(true);
        break;
      }
      case 3:
      {
        ((ToggleButton) findViewById(R.id.score_thirty)).setChecked(true);
        break;
      }
      case 4:
      {
        ((ToggleButton) findViewById(R.id.score_forty)).setChecked(true);
        break;
      }
      case 5:
      {
        ((ToggleButton) findViewById(R.id.score_fifty)).setChecked(true);
        break;
      }
      case 6:
      {
        ((ToggleButton) findViewById(R.id.score_sixty)).setChecked(true);
        break;
      }
      case 7:
      {
        ((ToggleButton) findViewById(R.id.score_seventy)).setChecked(true);
        break;
      }
      case 8:
      {
        ((ToggleButton) findViewById(R.id.score_eighty)).setChecked(true);
        break;
      }
      case 9:
      {
        ((ToggleButton) findViewById(R.id.score_ninety)).setChecked(true);
        break;
      }
      case 10:
      {
        ((ToggleButton) findViewById(R.id.score_hundred)).setChecked(true);
        break;
      }
      case 11:
      {
        ((ToggleButton) findViewById(R.id.score_hundred_ten)).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(displayScore % 10)
    {
      case 0:
      {
        ((ToggleButton) findViewById(R.id.score_0)).setChecked(true);
        break;
      }
      case 1:
      {
        ((ToggleButton) findViewById(R.id.score_1)).setChecked(true);
        break;
      }
      case 2:
      {
        ((ToggleButton) findViewById(R.id.score_2)).setChecked(true);
        break;
      }
      case 3:
      {
        ((ToggleButton) findViewById(R.id.score_3)).setChecked(true);
        break;
      }
      case 4:
      {
        ((ToggleButton) findViewById(R.id.score_4)).setChecked(true);
        break;
      }
      case 5:
      {
        ((ToggleButton) findViewById(R.id.score_5)).setChecked(true);
        break;
      }
      case 6:
      {
        ((ToggleButton) findViewById(R.id.score_6)).setChecked(true);
        break;
      }
      case 7:
      {
        ((ToggleButton) findViewById(R.id.score_7)).setChecked(true);
        break;
      }
      case 8:
      {
        ((ToggleButton) findViewById(R.id.score_8)).setChecked(true);
        break;
      }
      case 9:
      {
        ((ToggleButton) findViewById(R.id.score_9)).setChecked(true);
        break;
      }
      default:
      {
        // Shouldn't be missing any cases...
        assert false;
        break;
      }
    }
  }

  private void setBidFromToggle()
  {
    if(((ToggleButton) findViewById(R.id.score_hundred_twenty)).isChecked())
    {
      callerScore = callerPerspective ? 120 : 0;
      return;
    }

    if(((ToggleButton) findViewById(R.id.score_zero_tens)).isChecked())
    {
      callerScore = 0;
    }
    else if(((ToggleButton) findViewById(R.id.score_ten)).isChecked())
    {
      callerScore = 10;
    }
    else if(((ToggleButton) findViewById(R.id.score_twenty)).isChecked())
    {
      callerScore = 20;
    }
    else if(((ToggleButton) findViewById(R.id.score_thirty)).isChecked())
    {
      callerScore = 30;
    }
    else if(((ToggleButton) findViewById(R.id.score_forty)).isChecked())
    {
      callerScore = 40;
    }
    else if(((ToggleButton) findViewById(R.id.score_fifty)).isChecked())
    {
      callerScore = 50;
    }
    else if(((ToggleButton) findViewById(R.id.score_sixty)).isChecked())
    {
      callerScore = 60;
    }
    else if(((ToggleButton) findViewById(R.id.score_seventy)).isChecked())
    {
      callerScore = 70;
    }
    else if(((ToggleButton) findViewById(R.id.score_eighty)).isChecked())
    {
      callerScore = 80;
    }
    else if(((ToggleButton) findViewById(R.id.score_ninety)).isChecked())
    {
      callerScore = 90;
    }
    else if(((ToggleButton) findViewById(R.id.score_hundred)).isChecked())
    {
      callerScore = 100;
    }
    else if(((ToggleButton) findViewById(R.id.score_hundred_ten)).isChecked())
    {
      callerScore = 110;
    }
    else if(((ToggleButton) findViewById(R.id.score_hundred_twenty)).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.score_0)).isChecked())
    {
      callerScore += 0;
    }
    else if(((ToggleButton) findViewById(R.id.score_1)).isChecked())
    {
      callerScore += 1;
    }
    else if(((ToggleButton) findViewById(R.id.score_2)).isChecked())
    {
      callerScore += 2;
    }
    else if(((ToggleButton) findViewById(R.id.score_3)).isChecked())
    {
      callerScore += 3;
    }
    else if(((ToggleButton) findViewById(R.id.score_4)).isChecked())
    {
      callerScore += 4;
    }
    else if(((ToggleButton) findViewById(R.id.score_5)).isChecked())
    {
      callerScore += 5;
    }
    else if(((ToggleButton) findViewById(R.id.score_6)).isChecked())
    {
      callerScore += 6;
    }
    else if(((ToggleButton) findViewById(R.id.score_7)).isChecked())
    {
      callerScore += 7;
    }
    else if(((ToggleButton) findViewById(R.id.score_8)).isChecked())
    {
      callerScore += 8;
    }
    else if(((ToggleButton) findViewById(R.id.score_9)).isChecked())
    {
      callerScore += 9;
    }
    else
    {
      // Shouldn't be missing any cases...
      assert false;
      return;
    }

    if(!callerPerspective)
    {
      callerScore = 120 - callerScore;
    }

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

  private void updateText()
  {
    GameData gameData = NewGameActivity.getGameData();

    TextView guiltyScoreTextView = (TextView) findViewById(R.id.score_guilty_text);
    guiltyScoreTextView.setText(String.format(getString(R.string.guilty_score_text_format), callerScore, gameData.getCallerBid()));

    TextView innocentScoreTextView = (TextView) findViewById(R.id.score_innocent_text);
    innocentScoreTextView.setText(String.format(getString(R.string.innocent_score_text_format), 120 - callerScore, 120 - gameData.getCallerBid() + 1));

    Resources res = getResources();
    if(callerScore >= gameData.getCallerBid())
    {
      guiltyScoreTextView.setTextColor(res.getColor(R.color.lime_green));
      innocentScoreTextView.setTextColor(res.getColor(R.color.red));
    }
    else
    {
      guiltyScoreTextView.setTextColor(res.getColor(R.color.red));
      innocentScoreTextView.setTextColor(res.getColor(R.color.lime_green));
    }
  }
}




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