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 (112 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

08 Feb 2014, 15:40
Generic-user-small

Luca Rambaldi (1 post)

Hello Maik! I’M Luca from Italy, I’m very enjoying reading your book end working with Arduino. But right now i ‘m blocked from more than a week with the Dice Game 2 button. Arduino ide give me this kind of error: “error: no matching function for call to ‘Bounce::Bounce(const unsigned int&, const unsigned int&)’ “ End also these: “e:\Documenti\Arduino\libraries\Bounce2/Bounce2.h:40: note: candidates are: Bounce::Bounce() “ …??? I can’t understand! it look like the sketch can’t recognise the libraries of Bounce. Name of libraries I using is “#include ” Is it similar to the libraries you suggest for the Dice game? The book talk about only “Bounce.h”. It’s these the mistake? It is the only I found!. I already check many tutorials end try a lot of solutions but I can’t across this step The version of IDE I using is Arduino 1.0.5-r2 My pc it’s a laptop with Windows Vista Board Arduino uno Please Maik I’m a beginner Help me! By Luca

14 Mar 2014, 19:41
Maik_schmidt_avatar2_pragsmall

Maik Schmidt (112 posts)

Hi Luca!

Sorry for my late reply, but somehow your post didn’t appear in my RSS reader or I’ve simply overlooked it.

Since the book has been published a couple of things have changed in the Bounce library. First, the library’s header file is no longer named Bounce.h but Bounce2.h. So instead of

#include "Bounce.h"

you have to use

#include "Bounce2.h"

Also the Bounce class’ interface has changed and you can no longer pass the button pin and the debounce delay to the constructor. Instead of

Bounce start_button(START_BUTTON_PIN, DEBOUNCE_DELAY);
Bounce guess_button(GUESS_BUTTON_PIN, DEBOUNCE_DELAY);

you now have to do it this way

Bounce start_button = Bounce();
Bounce guess_button = Bounce();

void setup() {
  pinMode(LED_BIT0, OUTPUT);
  pinMode(LED_BIT1, OUTPUT);
  pinMode(LED_BIT2, OUTPUT);  
  pinMode(START_BUTTON_PIN, INPUT);  
  pinMode(GUESS_BUTTON_PIN, INPUT);  
  start_button.attach(START_BUTTON_PIN);
  start_button.interval(DEBOUNCE_DELAY);
  guess_button.attach(GUESS_BUTTON_PIN);
  guess_button.interval(DEBOUNCE_DELAY);
  randomSeed(analogRead(A0));
  Serial.begin(BAUD_RATE);
}

Hope this helps!

Maik

  You must be logged in to comment