0

I'm working on a math game which presents the player with a simple math problem of the sort 4 + 3 = ? or 6 / 2 = ?. The player is offered 4 possible answers and clicks one. The screen has two buttons, one with an arrow, and one with a square.

The player selects the answer they think is correct. If the answer is correct they get a message. If the answer is incorrect, that choice is grayed out, and they can keep choosing until they get it right.

I want two modes of play: practice and timed.

In practice mode, the player clicks the arrow button and gets a new question. This works fine.

I also want a timed play mode. When the player presses the arrow button, a timer starts and the first problem comes up. If they get it right, they are immediately asked a new question. Again, if they are incorrect, they keep asking until they get it right. Meanwhile, there should be a timer counting down. Play stops when the timer ends or the player presses the square button.

I don't know how to do the timed play where there are two simultaneous activities: playing the game, and the timer.

I'm including pseudo-code instead of code -- the code is long. But if necessary, I can post the entire code.

Update:


This was my naive attempt. The problem is that I can't figure out how to wait in the while loop for until the player to ask the question before askQuestion() is called again. Perhaps it needs to be in a separate thread and have the while loop wait for that thread to return. Would the timer keep going? I want the timer to run simultaneously to a separate task in which a question is posed --> Player answers the question --> a new question is posed .....

  public void playTimed()
  {

//    Toast.makeText(getApplicationContext(), "in playPractice", Toast.LENGTH_SHORT).show();
    go_button.setOnClickListener(new OnClickListener()
    {       
      @Override
      public void onClick(View v)
      {  
        go_button.setEnabled(false);

        new CountDownTimer(10000, 1000) {

          public void onTick(long millisUntilFinished)
          {
            timer.setText("seconds remaining: " + millisUntilFinished / 1000);
          }

          public void onFinish()
          {
            timer.setText("done!");
            go_button.setEnabled(true);
            timer_done = true;
          }

       }.start();

       while(!timer_done)
       {
           askQuestion();
       }

      }
    });

  }

onCreate()
{
   -For all four answer buttons:
   answer_button.setOnClickListener(new OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
         correct = checkAnswer(answer_button_id);
      }
   });

   -get intent extras to know which game
   -swith-case to play selected game

}

checkAnswer()
{
   if (correct)
   {
      // feedback
      handleRightAnswer();
   }
   else
   {
      // feedback
      handleWrongAnswer();
   }
}

askQuestion()
{
   -choose type of problem (+,-,*,/)
   -generate arguments and answer
   -create random answer list
      where one choice is correct
   -display problem
   -display answer choices
}

playTimed()
{
   -When player presses go_button,
   timer starts and first question
   appears. When correct answer is
   selected a new question immediately
   appears. Continues until timer runs
   out or player presses stop_button.
   Seconds left is displayed during play.
}

playPractice()
{
   //When player presses go_button
   // a new question appears.
   go_button.setOnClickListener(new OnClickListener()
   {
      @Override
      public void onClick(View v)
      {
        askQuestion();
      }
    });

}

enter image description here

abalter
  • 9,663
  • 17
  • 90
  • 145
  • You don't want to do threads. You want to use a timer. When the timer goes off, you decrement the counter by 1 and display the new value. When the counter hits 0, you end the game (or move on to the next question- whatever it is you wanted to do). – Gabe Sechan Jun 16 '14 at 17:00
  • So, after all that, did you have a question? All I see is a bunch of "I want", no actual code, and what looks to be a "do my job for me and write all of this code" demand. – Marc B Jun 16 '14 at 17:00
  • You could use a CountDownTimer: http://developer.android.com/reference/android/os/CountDownTimer.html – Phantômaxx Jun 16 '14 at 17:00
  • I guess I didn't say what I had already tried. I also forgot to clarify one aspect of the problem. The play button should be disabled during the game. I've updated the question to reflect what I tried. – abalter Jun 16 '14 at 18:12
  • I want the timer to run simultaneously to a separate task in which a question is posed --> Player answers the question --> a new question is posed ..... – abalter Jun 16 '14 at 18:31

1 Answers1

0

As Gabe Sechan stated, you can do this using a timer to update a counter, then when the counter hits 0, do something. You can also use the timer to update a TextView (to display the time remaining).

Here is a class that I have used in the past, originally found here:

public class UIUpdater{
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private Runnable mStatusChecker;
    private int UPDATE_INTERVAL = 1000; //updates every second

    public UIUpdater(final Runnable uiUpdater) {
        mStatusChecker = new Runnable() {
            @Override
            public void run() {
                // Run the passed runnable
                uiUpdater.run();
                // Re-run it after the update interval
                mHandler.postDelayed(this, UPDATE_INTERVAL);
            }
        };
    }

    public UIUpdater(Runnable uiUpdater, int interval){
        this(uiUpdater);
        UPDATE_INTERVAL = interval;
    }

    public synchronized void startUpdates(){
        mStatusChecker.run();
    }

    public synchronized void stopUpdates(){
        mHandler.removeCallbacks(mStatusChecker);
    }
}

Then, in your method where you want to update your counter, you would put:

UIUpdater mUIUpdater = new UIUpdater(new Runnable() {
     @Override 
     public void run() {
        //method to update counter
 }
});

//to start the method:
mUIUpdater.startUpdates();

//to stop the method, ie, when counter == 0
mUIUpdater.stopUpdates();
Community
  • 1
  • 1
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
  • On that page, inazaruk says the class "will run your code with specified delay on the main UI thread". But I'm not trying to delay the main thread. I want the timer to run simultaneously to a separate task in which a question is posed --> Player answers the question --> a new question is posed ..... – abalter Jun 16 '14 at 18:30