#include <Arduino.h>

// In this PlatformIO C++ program, include Arduino.h explicitly.
// It is the header that gives you the normal
// Arduino environment: pinMode(), digitalWrite(), digitalRead(), millis(), HIGH,
// LOW, INPUT_PULLUP, and many other familiar functions and constants.

// These are the pins used for the row of LEDs.
// We store them in an array so we can treat them as a group.
const int ledPins[] = {2, 3, 4, 5, 6, 7};

// This calculates how many LEDs are in the array.
const int ledCount = sizeof(ledPins) / sizeof(ledPins[0]);

// The button pin.
// We will use INPUT_PULLUP, so the logic will be inverted:
// HIGH = not pressed, LOW = pressed.
const int buttonPin = 8;

// The player is trying to stop the moving light on this position.
const int jackpotIndex = 3;

// currentIndex tracks which LED is currently lit.
int currentIndex = 0;

// moving tells us whether the light is still sweeping.
bool moving = true;

// These variables help us move the light at regular time intervals.
unsigned long lastStepTime = 0;
const unsigned long stepInterval = 120;

// We remember the old button state so we can detect the moment the button is pressed.
int lastButtonState = HIGH;

// A void function does work but does not return a value.
// This one turns every LED off.
void clearLeds() {
  for (int i = 0; i < ledCount; i++) {
    digitalWrite(ledPins[i], LOW);
  }
}

// Another void function.
// It lights only the current LED.
void showCurrentLed() {
  clearLeds();
  digitalWrite(ledPins[currentIndex], HIGH);
}

// This function returns a bool, which means true or false.
// It answers the question: are we on the jackpot position?
bool isJackpot() {
  return currentIndex == jackpotIndex;
}

// setup() runs once when the board powers on or resets.
// This is where we configure pins and start Serial.
void setup() {
  for (int i = 0; i < ledCount; i++) {
    pinMode(ledPins[i], OUTPUT);
  }

  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(9600);
  showCurrentLed();

  Serial.println("Cyclone practice game started!");
  Serial.println("Press the button to stop the moving light.");
}

// loop() runs over and over forever after setup() finishes.
// This is where the main game behavior lives.
void loop() {
  int buttonState = digitalRead(buttonPin);

  if (moving) {
    // millis() returns the number of milliseconds since the board started.
    // This lets us do timing without freezing the whole program.
    unsigned long now = millis();

    if (now - lastStepTime >= stepInterval) {
      lastStepTime = now;
      currentIndex++;

      // If the light goes past the last LED, wrap back to the first one.
      if (currentIndex >= ledCount) {
        currentIndex = 0;
      }

      showCurrentLed();
    }
  }

  // Detect the instant the button changes from not pressed to pressed.
  if (lastButtonState == HIGH && buttonState == LOW) {
    moving = false;

    Serial.print("Stopped on LED index: ");
    Serial.println(currentIndex);

    if (isJackpot()) {
      Serial.println("JACKPOT!");
    } else {
      Serial.println("Missed it. Try again.");
    }
  }

  lastButtonState = buttonState;
}
