/***
 * Generated by chatGPT using the following prompt:
 * 
 * Write an arduino program that plays a numbers guessing game. There should be 10 functions and no function should be more than 8 lines long. 
 * All of the functions should be called at least once from somewhere in the program. All of the function and variable names should be quotes
 * or characters from the movie "Finding Nemo".
 *
 * https://wokwi.com/projects/357541331693080577
 *
 * https://www.reddit.com/r/Arduino_AI/comments/11aoz5r/finding_nemos_number_see_it_run_on_wokwi_right_now/?utm_source=share&utm_medium=web2x&context=3
 *
 * ++ripred (trent m. wyatt) Feb 24, 2023 
 */

int nemo_number; // global variable to hold the secret number
int touch_the_butt; // global variable to hold the number of guesses

// function to turn on an LED
void bloat_on(int pin) {
  digitalWrite(pin, HIGH); // turn the LED on
}

// function to turn off an LED
void nigel_off(int pin) {
  digitalWrite(pin, LOW); // turn the LED off
}

// function to blink an LED
void gill_blink(int pin) {
  bloat_on(pin); // turn the LED on
  delay(500); // wait for half a second
  nigel_off(pin); // turn the LED off
  delay(500); // wait for half a second
}

// function to flash an LED
void peach_flash(int pin) {
  bloat_on(pin); // turn the LED on
  delay(100); // wait for a tenth of a second
  nigel_off(pin); // turn the LED off
  delay(100); // wait for a tenth of a second
}

// function to fade an LED
void squirt_fade(int pin) {
  for (int i = 0; i <= 255; i++) { // loop from 0 to 255
    analogWrite(pin, i); // set the LED brightness
    delay(10); // wait for a tenth of a second
  }
  for (int i = 255; i >= 0; i--) { // loop from 255 to 0
    analogWrite(pin, i); // set the LED brightness
    delay(10); // wait for a tenth of a second
  }
}

// function to initialize the game
void marlin_init() {
  randomSeed(analogRead(0)); // seed the random number generator
  nemo_number = random(1, 101); // choose a random number between 1 and 100
  touch_the_butt = 0; // reset the guess counter
}

// function to prompt the user for a guess
int dory_guess() {
  touch_the_butt++; // increase the guess counter
  Serial.print("Guess number "); // prompt the user
  Serial.print(touch_the_butt, DEC); // display the guess number
  Serial.println(": What's your guess, Dory?"); // prompt the user
  int guess = 0; // variable for user input
  while (guess == 0) {
    while (Serial.available() < 2); // wait for input
    guess = Serial.parseInt(); // read the user's guess
    delay(250); // delay for last received bytes
    while (Serial.available()) Serial.read();
  }
  return guess; // return the guess to the calling function
}

// function to check the user's guess
void nemo_check(int guess) {
  if (guess < nemo_number) {
    Serial.print(guess, DEC);
    Serial.println(" is too low, Dory!"); // print a hint if the guess is too low
    gill_blink(2);
  } else if (guess > nemo_number) {
    Serial.print(guess, DEC);
    Serial.println(" is too high, Dory!"); // print a hint if the guess is too high
    peach_flash(3);
  } else {
    Serial.println("You got it, Dory!"); // print a congratulatory message if the guess is correct
    squirt_fade(5);
 }
}

// function to play the game
void bruce_play() {
  Serial.println(); // print a blank line
  Serial.println("Hi Dory. This is Nemo. I am thinking of a number between 1 and 100!"); // print the rules for a new game
  marlin_init(); // initialize the game
  int guess = 0; // initialize the user's guess
  while (guess != nemo_number) { // loop until the user guesses the number
    guess = dory_guess(); // prompt the user for a guess
    nemo_check(guess); // check the user's guess
  }
}

// function to setup the Arduino
void setup() {
  Serial.begin(115200); // initialize the serial port
  pinMode(2, OUTPUT); // initialize the LED pins
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
}

// main program loop
void loop() {
  bruce_play(); // play the game
}