/*
Welcome to this HomeWork Application (Lesson5)!");
This is a Application, Text based game where youre tasked to gess a Integer within the bounds of (1-100)

(1-101) This wil indicate the gessing Range)
*/

int RandomSelectedNumber = random(1, 101);          // Random Number choosen between defined values.
int GessAmmount = 0;                              //Ammount of Gesses input from the user.

bool GameFinnishedReset = false;                  //Boolean Used to decide if Game should reset.
bool GameIsPaused = false;                        //Boolean Used to decide if Game is paused.




/*

(This Binds a Constant Integer value to the PINs)

*/

const int ResetPin = 21;
const int SuccesGreenLed = 18;
const int PausedOrangeLed = 19;
const int FailureRedLed = 23;
const int ResetRedLed= 25;

void setup() {

  Serial.begin(115200);
  Serial.setTimeout(65000);

  pinMode(ResetPin, INPUT_PULLUP);                //Define ResetPin as a Input With a Pull up resister.

  pinMode(SuccesGreenLed, OUTPUT);                //Define SuccesGreenLed as a Output.
  pinMode(PausedOrangeLed, OUTPUT);                  //Define FailureRedLed as a Output.
  pinMode(FailureRedLed, OUTPUT);                  //Define FailureRedLed as a Output.
  pinMode(ResetRedLed, OUTPUT);                     //Define ResetRedLed as a Output.

  Serial.println("Welcome to this HomeWork Application (Lesson5)!");
  Serial.println("This is a Application, Text based game where youre tasked to gess a Integer within the bounds of (1-100)");

}

void loop() {

// Test to check for bool (GameIsPaused) status

/*

if (GameIsPaused == true)  {
  Serial.println("GameIsPaused is True (1)");
}
  else{
    Serial.println("GameIsPaused is False (0)");
  }

*/
GessAmmount++;                                 //Adds +1 To Gess total.

if (digitalRead(ResetPin) == LOW) {
  digitalWrite(ResetRedLed, HIGH);
  GameFinnishedReset =true;
  delay(250);
  digitalWrite(ResetRedLed, LOW);
}   

if (GameFinnishedReset == true) {             //This block Indicates what happens when GameFinnishedReset is true.
      Serial.println("Game is being Reset.");
      Serial.println(" ------- "); 
      digitalWrite(SuccesGreenLed, LOW);
      digitalWrite(FailureRedLed, LOW);
      RandomSelectedNumber = random(1, 101);
      GessAmmount = 0;
      GameFinnishedReset = false;
      GameIsPaused = false;

  }

/*

The Next Block checks if the Pause Boolean is true ore false and ouputs accordingly,
-Lights up the orange LEd 
-Pauze's the game with a return function.

*/

  if (GameIsPaused == true) { 
     digitalWrite(PausedOrangeLed, HIGH);                          
     return;
     }
        else {
          digitalWrite(PausedOrangeLed, LOW);
      }

  Serial.println("Please Input the Number You are gessing");
  String Gessed = Serial.readStringUntil('\n');
  Serial.println("Youre choosen Number is " + String (Gessed));
   Serial.println("Youre choosen Have 100Seconds To Gess The Number Correctly");
  delay (5);

  int Gessed_Float = Gessed.toInt();             //Changes Gessed string to Integer.


/*

The Next Block Decides what happens when the User input number is either:

== Equal to the RandomSelectedNumber,

<  Smaller then RandomSelectedNumber,

> RandomSelectedNumber, (Defined with else),

*/

  if (Gessed_Float == RandomSelectedNumber) {
   digitalWrite(SuccesGreenLed, HIGH);   
   Serial.println("Congratulations U have gessed the Random Number correctly! ");
   Serial.println("We have a Winner!!");
   Serial.println("-It took You " + String (GessAmmount) + " Tries to gess te correct Number-");
   Serial.println(" ");
   delay(2500);
   Serial.println("You Have gessed the Correct Number The game will reset When Pushing the Button");
   digitalWrite(SuccesGreenLed, LOW);
   GameIsPaused = true;
  }
   else if (Gessed_Float < RandomSelectedNumber) {
    Serial.println("Sadly Your gess of the Random Number is Lower then The Randomly Defined Number.");
    Serial.println("You have " + String (10 - GessAmmount) + " Attempts remaining.");
    Serial.println("Attempt entering a Higher value!.");

   }
      else {
      Serial.println("Sadly Your gess of the Random Number is Higher then The Randomly Defined Number.");
      Serial.println("You have " + String (10 - GessAmmount) + " Attempts remaining.");
      Serial.println("Attempt entering a Lower value.");
      delay(100);
      }


/*

The Next Block Decides what happens when the User ran out of gesses,
Indicated by int GessAmmount = 0; Higher ore equal to 10.

(GessAmmount >= 10)

*/
  if (GessAmmount >= 10) {
    Serial.println("You have Run out of Attempts, The Correct Randomly selected number was" + String (RandomSelectedNumber));
    Serial.println("You Have Not gessed the Random Number The game will reset When Pushing the Button");
    digitalWrite(FailureRedLed, HIGH);
    delay(2500);
    digitalWrite(FailureRedLed, LOW);
    GameIsPaused = true;
  }

/*

The Next Block Decides what happens when the User pushes the button.
Changing the boolean state from the boolean named (GameFinnishedReset)
Also Flashing the Control led for the reset button when pressed.
*/

if (digitalRead (ResetPin) == LOW) {
    digitalWrite(ResetRedLed, HIGH);
    GameFinnishedReset = true;
    delay(250);
    digitalWrite(ResetRedLed, LOW);
  }
}
$abcdeabcde151015202530fghijfghij