Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not absolutely completely sure that I understand exactly what you're saying, but as an analagous situation, say someone's inputting an endless stream of numbers on the keyboard. '117' is a magic sequence, '468' is another one, '411799' is another (which contains the first one). </p> <p>So if the user enters:</p> <blockquote> <p>55468411799</p> </blockquote> <p>you want to fire 'magic events' at the *s:</p> <blockquote> <p>55468*4117*99*</p> </blockquote> <p>or something like that, right? If that's analagous to the problem you're talking about, then what about something like (Java-like pseudocode):</p> <pre><code>MagicSequence fireworks = new MagicSequence(new FireworksAction(), 1, 1, 7); MagicSequence playMusic = new MagicSequence(new MusicAction(), 4, 6, 8); MagicSequence fixUserADrink = new MagicSequence(new ManhattanAction(), 4, 1, 1, 7, 9, 9); Collection&lt;MagicSequence&gt; sequences = ... all of the above ...; while (true) { int num = readNumberFromUser(); for (MagicSequence seq : sequences) { seq.handleNumber(num); } } </code></pre> <p>while MagicSequence has something like:</p> <pre><code>Action action = ... populated from constructor ...; int[] sequence = ... populated from constructor ...; int position = 0; public void handleNumber(int num) { if (num == sequence[position]) { // They've entered the next number in the sequence position++; if (position == sequence.length) { // They've got it all! action.fire(); position = 0; // Or disable this Sequence from accepting more numbers if it's a once-off } } else { position = 0; // missed a number, start again! } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload