Last active
June 1, 2020 10:39
-
-
Save Chadtech/4e53b16583135a8f3689626eae8b1415 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool listen(time: int) { | |
// Blocking function that waits for `time` and then returns a bool representing | |
// if there was a hit within that time period | |
} | |
int main() { | |
// Succeeded in hitting the correct knock | |
int succeeded = false; | |
// A knock is a sequence of durations. Each duration is | |
// the time between physical hits on a detector | |
int correctKnock [5] = [ 1000, 500, 500, 1000, 1500, 1000, 1000 ]; | |
// Human beings arent going to hit the detector | |
// at precisely the expected duration so we | |
// need a margin of error within which a | |
// human being could hit the detector and still | |
// count | |
int marginOfError = 150; | |
// The first knock has not yet occurred, so | |
// the index is -1 to indicate that we are not | |
// yet within the sequence | |
int indexInKnockSequence = -1; | |
while (!succeeded) { | |
if (indexInKnockSequence == -1) { | |
// Just wait for the first hit | |
while (!listen(1)) { | |
// the first hit occurred, so | |
// we are at the first position | |
// in the knock sequence | |
indexInKnockSequence = 0; | |
} | |
} | |
if (listen(correctKnock[indexInKnockSequence] - marginOfError)) { | |
// They hit the detector, but too early, so | |
// reset the position so they can try again | |
indexInKnockSequence = -1; | |
} | |
if (listen(marginOfError * 2)) { | |
// They hit the detector within the time period, so | |
// incremenet to listen for the next hit | |
indexInKnockSequence++; | |
} else { | |
// They never hit the detector, so reset the position | |
// in the sequence so we can go back to listening | |
// for them to start the knock | |
indexInKnockSequence = -1; | |
} | |
if (indexInKnockSequence == 6) { | |
// They reached the end of the sequence | |
// so they succeeded and we can exit this | |
// while loop and hit the print() below | |
succeeded = true; | |
} | |
} | |
print("That is the correct knock!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment