0

I am rather new to android developing and can't figure out how to do a double check on a button press. I am working in Android Studio and am trying to create a little simple game and have the following: two buttons (button1 and button2)declared and instantiated in an onClick method. When button 1 is pressed a counter is incremented and a specific value is shown on the button from an array with the values 0, 10, 50, 100. This part works like a charm. What I can't do is create a working "tiebreaker rule" which should be something like:

if (button1 value == 100 and button2 value == 100) or this can be done with the counter if (counter1 == 3 && counter2 == 3)
tiebreaker();

this is what I need to do:

public void tiebreaker(){
if (button1 is pressed){
   button1.setText(String.valueOf("OK. One more!");
   if (button1 is pressed (again){
       resetCounters();
       goForPlayer1(); //distinct method
   } else if (button2 is pressed) {
       //set both values back to 100
       tiebreaker(); //calls itself in order to repeat until a button is pressed twice consecutivelly
} (else?)
if(button2 is presed){
//same code as above, just with the numbers 1 and 2 swapped
}
}

I have tried implementing this tiebreaker method in different ways, but with no final success. I tried with if(button1.isPressed()) as condintion, having a boolean turn true when the button is pressed and using it as a condition, using the values from the buttons with button.equals("100") or whatever should be written but nothing.

Also, I tried making the tiebreaker as a second onClick method and doing checks based on switch (View.getId()) but the problem here is that the method being view dependant (tiebreaker(View v)) I am not sure how to call it from a different method.

IMO the main issue is that if within another if or switch/case within another switch/case can't be used when it comes to button presses (I could be wrong, though), but I don't know of any other way to do this.

I hope I did not miss anything and managed to make myself clear. I appreciate any input in helping me solve this tiebreaker rule. Thank you

2 Answers 2

2

it's probably something like this ...

whenButtonAIsPressed() {

    do some stuff
    possiblyRunTiebreakerMaterial()
}

whenButtonBIsPressed() {

    do some stuff
    possiblyRunTiebreakerMaterial()
}

Note that you always run possiblyRunTiebreakerMaterial() and you always run it at the end of doing all your other processing.

Here's the nature of your possiblyRunTiebreakerMaterial() function...

possiblyRunTiebreakerMaterial() {

    // there are some conditions, which mean you DO want
    // to run the tie breaker:
    do some processing, such as
    value of A = blah blah
    value of B = blah blah

    // now have a boolean, and figure it out
    boolean  shouldWeRunTheTieBreaker

    processing
    processing
    shouldWeRunTheTieBreaker = blah

    and now finally:
    is shouldWeRunTheTieBreaker == true { tiebreaker() }
}

and then have your tiebreaker() function.

What you're really looking for is a state machine, but don't worry about that for now.

The general "programming trick" you're looking for here is that you must do these things:

  1. always do your processing
  2. then, only afterwards,
  3. in all cases, check IF you need to run the tiebreaker
  4. run it if needed

That's the pattern.

1
  • Thank you for the answer. The issue I have is not checking if the tiebreaker should be called or not, but the interior of the method itself. At the moment I have no other idea about what to write inside the tiebreaker in order for it to do what I described above.
    – Andy Gix
    Commented Mar 2, 2018 at 12:56
-1

Unfortunately, I don't have the rating to upvote. I managed to resolve the issue. Instead of a function, I created a boolean for tiebreaker and it looks something like:

if (p1 = 100 || p2 == 100)
   tiebreak = true;
if (p1 == 100 && tiebreaker){
   p1 == OK;
   tiebreaker = false;
} else //same thing for p2
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.