diff --git a/super_simon/include/main.h b/super_simon/include/main.h index b8fb041..5c2d1a3 100644 --- a/super_simon/include/main.h +++ b/super_simon/include/main.h @@ -24,6 +24,7 @@ #define TONE_YELLOW 277 #define TONE_BLUE 329 #define TONE_RED 440 +#define TONE_ERROR 100 // Indices in the constants tables #define GREEN 0 @@ -33,8 +34,4 @@ #define MAX_GAME 10 -const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; -const uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED}; -const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; - #endif //SUPER_SIMON_MAIN_H diff --git a/super_simon/include/utils.h b/super_simon/include/utils.h index 2daeb8e..e36ac35 100644 --- a/super_simon/include/utils.h +++ b/super_simon/include/utils.h @@ -10,5 +10,10 @@ void buzz(byte index, unsigned long duration = 0); bool buttonIsPressed(byte index); void deactivateAll(); void playSequence(const byte sequence[], byte upTo); +bool userSequence(const byte sequence[], byte upTo); +byte waitForButton(); +void error(); +void endGame(LiquidCrystal lcd, bool win, byte score); +void configure(); #endif //SUPER_SIMON_UTILS_H diff --git a/super_simon/src/main.cpp b/super_simon/src/main.cpp index 6dd18d2..17a7625 100644 --- a/super_simon/src/main.cpp +++ b/super_simon/src/main.cpp @@ -7,6 +7,7 @@ LiquidCrystal lcd(9, 8, 4, 5, 6, 7); byte sequence[MAX_GAME] = {}; int8_t currentPosition = -1; +bool win = true; void setup() { pinMode(LED_BUILTIN, OUTPUT); @@ -15,13 +16,7 @@ void setup() { lcd.print("Super Simon"); randomSeed(analogRead(A0)); pinMode(BUZZER, OUTPUT); - for (byte i = 0; i < 4; i++) { - const byte led = LEDS[i]; - pinMode(led, OUTPUT); - pinMode(BUTTONS[i], INPUT_PULLUP); - activate(i); - delay(300); - } + configure(); deactivateAll(); digitalWrite(LED_BUILTIN, LOW); delay(2000); @@ -29,16 +24,19 @@ void setup() { void loop() { if (currentPosition + 1 >= MAX_GAME) { - lcd.clear(); - lcd.print("Bravo!"); - delay(10000); + endGame(lcd, win, currentPosition); return; } currentPosition += 1; byte newItem = random(0, 4); sequence[currentPosition] = newItem; + playSequence(sequence, currentPosition); + if (!userSequence(sequence, currentPosition)) { + win = false; + currentPosition = MAX_GAME; + } delay(2000); } diff --git a/super_simon/src/utils.cpp b/super_simon/src/utils.cpp index 3728828..abc87fc 100644 --- a/super_simon/src/utils.cpp +++ b/super_simon/src/utils.cpp @@ -2,16 +2,29 @@ // Created by Gabriel Augendre on 29/04/2021. // +#include #include #include "main.h" #include "utils.h" +const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; +const uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED}; +const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; + +void configure() { + for (byte i = 0; i < 4; i++) { + const byte led = LEDS[i]; + pinMode(led, OUTPUT); + pinMode(BUTTONS[i], INPUT_PULLUP); + activate(i); + delay(300); + } +} + void activate(byte index) { for (const byte led : LEDS) { - if (led != index) { - digitalWrite(led, LOW); - } + digitalWrite(led, LOW); } digitalWrite(LEDS[index], HIGH); buzz(index); @@ -40,3 +53,48 @@ void playSequence(const byte sequence[], byte upTo) { delay(15); } } + +bool userSequence(const byte sequence[], byte upTo) { + for (byte i = 0; i <= upTo; i++) { + byte userButton = waitForButton(); + byte expectedButton = sequence[i]; + if (userButton == expectedButton) { + activate(userButton); + delay(300); + deactivateAll(); + } + else { + error(); + return false; + } + } + return true; +} + +void error() { + tone(BUZZER, TONE_ERROR, 500); + delay(500); +} + +byte waitForButton() { + while (true) { + for (byte i = 0; i < 4; i++) { + if (buttonIsPressed(i)) { + return i; + } + } + } +} + +void endGame(LiquidCrystal lcd, bool win, byte score) { + lcd.clear(); + if (win) { + lcd.print("Bravo!"); + } + else { + lcd.print("Perdu :/"); + } + lcd.setCursor(0, 1); + lcd.print(score); + delay(10000); +} \ No newline at end of file