Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence generation no longer creates repeating colors more than twice #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions memorygame/memorygame.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ unsigned short userPositionInSequence;
unsigned short n;
unsigned long count = 0;


// Create a new game sequence.
// Prevent generating sequence elements repeating more than twice in a row.
void generate_sequence(unsigned short *sequence, unsigned long max_game_sequence){
randomSeed(analogRead(0));
unsigned short second_prev;
unsigned short prev;

sequence[0] = random(0, 3);
second_prev = sequence[0];
sequence[1] = random(0, 3);
prev = sequence[1];

for (int i = 2; i < max_game_sequence; i++) {
sequence[i] = random(0, 3);
if(sequence[i] == second_prev || sequence[i] == prev){
// prevent 3 matching colors in a row (2 in row is ok)
sequence[i] = (sequence[i]+1)%4;
}
second_prev = prev;
prev = sequence[i];
}
}

void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
Expand All @@ -56,12 +80,7 @@ void loop() {
if (debouncerGreen.fell()) {
digitalWrite(GREEN_LED, LOW);

// Create a new game sequence.
randomSeed(analogRead(0));

for (n = 0; n < MAX_GAME_SEQUENCE; n++) {
gameSequence[n] = random(RED_BUTTON, YELLOW_BUTTON + 1);
}
generate_sequence(gameSequence, MAX_GAME_SEQUENCE);

currentSequenceLength = 1;
currentDelay = 500;
Expand Down