#include <Arduino.h>
#define LED_SPEED 30 //Smaller number is faster

#define LION_HEAD_REWARD_RATIO 2
#define CROWN_REWARD_RATIO 4
#define OTHER_REWARD_RATIO 4

//positions of the rewards in the ledArray
const int lionHeadPositions[2] = {0, 5};
const int crownPositions[2] = {3,8};
const int otherPositions[5] = {1,2,6,7,9};

int lion_rewards_available = 6;
int crown_rewards_available = 16;
int other_rewards_available = 5;


const int inputButtonPin = 3;
const int ledArray[10] = {31, 33, 35, 37, 39, 41, 43, 45, 47, 49};

int generatePrizeNumber(){
  int randomNumber = random(0, 10);
  if (randomNumber >=0 && randomNumber <LION_HEAD_REWARD_RATIO){
    if (lion_rewards_available > 0){
      lion_rewards_available--;
      return lionHeadPositions[random(0,2)];
    }
    else{
      return generatePrizeNumber();
    }
    
  }

  else if (randomNumber >=LION_HEAD_REWARD_RATIO && randomNumber <LION_HEAD_REWARD_RATIO+CROWN_REWARD_RATIO){
    
    if(crown_rewards_available > 0){
      crown_rewards_available--;
      return crownPositions[random(0,2)];
    }
    else{
      return generatePrizeNumber();
    }
  }

  else{
    if (other_rewards_available > 0){
      other_rewards_available--;
      return otherPositions[random(0,5)];
    }
    else{
      return generatePrizeNumber();
    }
  }

}

void spinAndStopLeds(int prizeNumber){
  // turn off all leds initially
  for(int i = 0; i < 10; i++){
    digitalWrite(ledArray[i], LOW);
  }

  //The leds start spinning in a circular motion 3 times and stop at the prize number
  for(int i = 0; i < 6; i++){
    for(int j = 0; j < 10; j++){
      digitalWrite(ledArray[j], HIGH);
      delay(LED_SPEED);
      digitalWrite(ledArray[j], LOW);
    }
  }
  for(int i = 0; i <= prizeNumber; i++){
    digitalWrite(ledArray[i], HIGH);
    delay(LED_SPEED);
    digitalWrite(ledArray[i], LOW);
  }

  //flash the prizeNumber led thrice slow and stay ON
  for(int i = 0; i < 3; i++){
    digitalWrite(ledArray[prizeNumber], HIGH);
    delay(500);
    digitalWrite(ledArray[prizeNumber], LOW);
    delay(500);
  }
  digitalWrite(ledArray[prizeNumber], HIGH);
}
void setup() {
  // put your setup code here, to run once:

  // initialize all leds as output
  for(int i = 0; i < 10; i++){
    pinMode(ledArray[i], OUTPUT);
  }
  // initialize the inputbutton pin as an input:
  pinMode(inputButtonPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(inputButtonPin) == HIGH){
    
    int prizeNumber = generatePrizeNumber();
    spinAndStopLeds(prizeNumber);
    delay(500);
  }
  else{
    //do nothing
  }
  
}