30 Jul 2012, 19:44
Generic-user-small

kevin estrella (1 post)

Ok so I have the dice game working and everything but i want to change it.
I want there to be a greater chance of winning. Right now the probability of winning is 1 out of 6. I want to be increased that you can win 1/2 of the time. I having trouble on what to change in the code. Please I know there is a easy fix but im stuck

31 Jul 2012, 15:14
Maik_schmidt_avatar2_pragsmall

Maik Schmidt (110 posts)

Hi Kevin!

So, you wanna cheat, eh? :-)

For the current structure of the sample program I do not see an easy solution to your problem. To increase your chances you have to increase your amount of guesses. Therefore you have to count the current number of guesses in a separate variable. You also need a constant storing the maximum number of guesses. Add the following declarations to the top of the program:

const unsigned int MAX_GUESSES = 2;
int guess_count = MAX_GUESSES;

Now you have to change both the handle_guess_button() and the handle_start_button() function. Keep in mind that if variable guess is not equal to zero the user has entered a guess. So you can leave handle_guess_button() immediately, if guess is greater than zero. This way you will not accept a new guess until the user has won or has guessed the maximum number of times. Add the following statement at the beginning of handle_guess_button:

if (guess > 0)
  return;

In handle_start_button() you have to do a lot more and I think it has to look as follows:

void handle_start_button() {
  if (start_button.update()) {
    if (start_button.read() == HIGH) {
      if (guess > 0) {
        while (guess_count-- > 0)
        {
          const int result = random(1, 7);
          if (result == guess) {
            output_result(result);
            Serial.print("Result: ");
            Serial.println(result);
            Serial.println("You win!");
            hooray();
            break;
          } else if (guess_count > 0) {
            continue;
          } else {
            Serial.println("You lose!");
            break;
          }
        }
      }
      delay(2000);
      guess = 0;
      guess_count = MAX_GUESSES;
    }
  }
}

I do not have an Arduino at hand right now, so I could not test the program. Still it should give you an idea of what you have to do. If you still have any problems, do not hesitate to ask.

Hm, now that I think of it, guess_count should better be named roll_count and MAX_GUESSES should be MAX_ROLLS.

Cheers,
Maik

  You must be logged in to comment