Solution to Limited Cheats Challenge

(in ActivityQuiz.java)

private int mNumberOfCheats = 0;

mCheatButton = (Button) findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNumberOfCheats ++;

            if (mNumberOfCheats >= 4) {
                Toast.makeText(QuizActivity.this, R.string.too_many_cheats_toast, Toast.LENGTH_SHORT).show();
                return;
            }
            else {
                mNumberOfCheatsTextView = (TextView) findViewById(R.id.number_of_cheats_text_view);
                mNumberOfCheatsTextView.setText("you have done " + mNumberOfCheats + " cheat(s) ..." + (3-mNumberOfCheats) + "left");
            }

            boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
            Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);

            startActivityForResult(intent, REQUEST_CODE_CHEAT);
        }
     });

(and in strings.xml)
Can’t cheat anymore!.

(and in layout.xml after the cheat button)

1 Like

I took a different approach to solving it
(ActivityQuiz.java)

...
private TextView mTokensTextView;
private int mTokens = 3;
...
        mTokensTextView = (TextView) findViewById(R.id.tokens_text_view);
...
        mCheatButton = (Button) findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTokens > 0) {
                    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
                    Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
                    startActivityForResult(intent, REQUEST_CODE_CHEAT);
                } else {
                    mCheatButton.setEnabled(false);
                }
            }
        }); 
...
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        if (requestCode == REQUEST_CODE_CHEAT) {
                if (data == null) {
                    return;
                }
                mIsCheater = CheatActivity.wasAnswerShown(data);
                mTokens--;
                mTokensTextView.setText(mTokens + " tokens left");
            }
    }

and I just have the default text for the textView as “3 tokens left”

1 Like

… I like yours. It is better to have a more obvious spot to change how many tries are considered too many, as you do -makes it more obvious (and complete) this way. It requires a mod to two areas of the code (the listener and the onActivity), but that would seem trivial compared to the advantage … thanks for uploading the additional idea !

  • joem

My solution.

mCheatLeftTimes will only be changed when the answer has been shown. If the mCheatButton is clicked but the answer is not shown, the mCheatLeftTimes will not be changed.

mCheatButton will be disabled when the answer has been shown or the mCheatLeftTimes is equal to zero.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_CHEAT && resultCode == RESULT_OK) {
            if (data != null) {
                mIsCheaters[mCurrentIndex] = CheatActivity.wasAnswerShown(data);
                mCheatLeftTimes--;
                updateQuestion();
            }
        }
    }

    private void updateQuestion() {
        //......

        mCheatLeftTimesTextView.setText(getString(R.string.cheat_left_times, mCheatLeftTimes));

        //......
        mCheatButton.setEnabled((!mIsCheaters[mCurrentIndex]) && (mCheatLeftTimes > 0));

        //......
    }

Hello,
I’m the beginner and would love to understand the changes you made in this code. Could you please expain them in words?
Many thanks in advance.

1 Like