Implement game

This commit is contained in:
Gabriel Augendre 2021-04-29 20:23:21 +02:00
parent e1459654ba
commit 187bfe12ac
4 changed files with 75 additions and 17 deletions

View file

@ -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

View file

@ -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

View file

@ -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);
}

View file

@ -2,16 +2,29 @@
// Created by Gabriel Augendre on 29/04/2021.
//
#include <LiquidCrystal.h>
#include <Arduino.h>
#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);
}