 
 
 /**
+ * Run the 'Simon Says' game.
+ */
+void playGame(int *pattern) {
+
+    // Set game parameters then begin.
+    int lives = TOTAL_LIVES;
+    int currentLevel = 1;
+    while (lives > 0 && currentLevel < TOTAL_LEVELS) {
+
+        // Display the pattern.
+        for (int lvl = 0; lvl < currentLevel; lvl++) {
+
+            // Blink the LED.
+            digitalWrite(pattern[lvl], HIGH);
+            delay(GAME_SPEED);
+            digitalWrite(pattern[lvl], LOW);
+
+            // Do not wait after blinking the final pattern LED.
+            if (lvl + 1 != currentLevel) delay(GAME_SPEED);
+        }
+
+        // Check player input.
+        if (verifyPlayerInput(pattern, currentLevel)) {
+
+            // The player was correct; move to the next level.
+            currentLevel++;
+
+        // The player was incorrect; subtract a life.
+        } else {
+
+            // Turn on the next red LED.
+            if (lives == TOTAL_LIVES) digitalWrite(RED_1, HIGH);
+            else if (lives == TOTAL_LIVES - 1) digitalWrite(RED_2, HIGH);
+            else if (lives == TOTAL_LIVES - 2) digitalWrite(RED_3, HIGH);
+            delay(LIGHT_DELAY);
+
+            // Remove a life from the current total.
+            lives--;
+        }
+    }
+}
+
+
+/**
  * Cycles LEDs on and off in a decorative pattern while waiting for game start.
  */
 void playLightShow() {

 * Author: David Timmons ( http://david.timmons.io )
 * License: MIT
 */

// Turn debug mode on or off.
const boolean DEBUG = true;

        playLightShow();  // The tilt switch starts a new game.
    }

    // Create a random 'Simon Says' pattern.
    int pattern[TOTAL_LEVELS];
    for (int i = 0; i < TOTAL_LEVELS; i++) {
        int num = 1 + random(4);  // Random between [1, 4].
        if (num == 1) pattern[i] = SIMON_1;
        else if (num == 2) pattern[i] = SIMON_2;
        else if (num == 3) pattern[i] = SIMON_3;
        else pattern[i] = SIMON_4;
    }

    // Begin the game.
    playGame(pattern);
}


/**
 * Determine whether the player successfully matched the pattern.
 */
boolean verifyPlayerInput(int *pattern, int currentLevel) {

    // Get player input up through the current game level.
    int lvl = 0;
    while (lvl < currentLevel) {

        // Wait for a button press.
        int simonLed = -1;
        while (simonLed == -1) {
            simonLed = getSimonLed(analogRead(BUTTONS));
        }

        // Blink the corresponding LED.
        digitalWrite(simonLed, HIGH);
        delay(GAME_SPEED);
        digitalWrite(simonLed, LOW);
        delay(GAME_SPEED);

        // Check if the button press was correct.
        if (pattern[lvl] == simonLed) {
            lvl++;
        } else {
            // Player input was incorrect.
            return false;
        }
    }

    // The player successfully matched the pattern.
    return true;
}


