Simon: The Full Game
Part of the Projects section of Coddy's Arduino journey. Lesson 16 of 24.
Real Simon starts small and grows. Round 1 plays one colour. Each time you repeat a round right, the board keeps the same colours and adds one at the end, until you make a mistake.
The array needs room for a long game, and a variable length for how much of it is used. Adding a colour is two lines:
sequence[length] = random(4);
length++;Seed once, when the game starts. playSequence() now plays length colours instead of four, and a round is over when step reaches length.
A mistake ends the game with a sound that is clearly not a note, 110 Hz for one second, and prints the score: the rounds completed, length - 1, since the lost round does not count.
The array has 20 places and the sketch must never write past its end, so a player who repeats 20 colours wins.
Challenge
HardTurn it into the full game:
const int MAX_STEPS = 20;,int sequence[MAX_STEPS];andint length = 0;- a game starts with one colour; each round repeated right adds one and plays them all
- a mistake: 110 Hz for 1000 ms, then print
Game over! Score:and the score, and end the game - 20 colours repeated right: print
You win!and end the game
Press Run, see how far you get, then Check.
Try it yourself
// One entry per colour: red, green, blue, yellow
const int ledPins[4] = {9, 10, 11, 12};
const int buttonPins[4] = {2, 3, 4, 5};
const int notes[4] = {262, 330, 392, 523}; // C, E, G and high C
const int buzzerPin = 8;
int lastState[4] = {HIGH, HIGH, HIGH, HIGH}; // each button's last reading
int sequence[4]; // the colours the board plays
bool playing = false; // false until a press starts the game
int step = 0; // how many colours the player has repeated
// Light colour i and play its note.
void lightOn(int i) {
digitalWrite(ledPins[i], HIGH);
tone(buzzerPin, notes[i]);
}
// Turn colour i off and stop the sound.
void lightOff(int i) {
digitalWrite(ledPins[i], LOW);
noTone(buzzerPin);
}
// Play the sequence: each colour 400 ms, then 200 ms of quiet.
void playSequence() {
delay(500);
for (int i = 0; i < 4; i++) {
lightOn(sequence[i]);
delay(400);
lightOff(sequence[i]);
delay(200);
}
}
// Pick a new random sequence and play it.
void startGame() {
randomSeed(micros());
for (int i = 0; i < 4; i++) {
sequence[i] = random(4);
}
playing = true;
step = 0;
playSequence();
}
// The player let go of colour i: is it the next one in the sequence?
void checkPress(int i) {
if (i != sequence[step]) {
Serial.println("Wrong!");
playing = false;
return;
}
step++;
if (step == 4) {
Serial.println("Well done!");
playing = false;
}
}
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 4; i++) {
int state = digitalRead(buttonPins[i]);
if (state == LOW && lastState[i] == HIGH) { // pressed
lightOn(i);
}
if (state == HIGH && lastState[i] == LOW) { // let go
lightOff(i);
if (playing == false) {
startGame();
} else {
checkPress(i);
}
}
lastState[i] = state;
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
Practice on your own: Arduino playground