Magic 8 Ball Demo

What is Magic 8 Ball Demo?

Rondomly selecting a string from a list of arbitrary length is a perfect use case for simpleTable()

Example

Result:

How the Template Works

The code for this demo creates an object showcasing a feature of the RollPlayer.js library and returns it to the template as demo.rollable. Both when the page loads and when the button above is pressed the template code calls demo.rollable.roll() and the returned result is displayed above. This process is identicle for all the demos allowing the explanations below to focus on only the object being demoed.

Every result is also logged to the console if you want to see a history after a bunch of button clicks.

Version

1.0

Authors

  • Derek Pennycuff

Full source

  1. 1: var demo = function() {
  2. 2: var magic8ball = RollPlayer.simpleTable('It is certain',
  3. 3: 'It is decidedly so', 'Without a doubt', 'Yes definitely',
  4. 4: 'You may rely on it', 'As I see it, yes', 'Most likely',
  5. 5: 'Outlook good', 'Yes', 'Signs point to yes',
  6. 6: 'Reply hazy try again', 'Ask again later',
  7. 7: 'Better not tell you now', 'Cannot predict now',
  8. 8: 'Concentrate and ask again', 'Don\'t count on it',
  9. 9: 'My reply is no', 'My sources say no',
  10. 10: 'Outlook not so good', 'Very doubtful');
  11. 11: return { rollable : magic8ball };
  12. 12: }();

Explanations

We're creating a wrapper object called demo so that the template code can access the inner workings of this particular example.

  1. 1: var demo = function() {

The quickest and easiest way to build a table where every entry has an equal chance of being rolled is RolePlayer.simpleTable().

The constructor for simpleTable() takes a simple list of strings as a paramter. Internally it creates a die() object with faces equal to the number of enries in this list. This Die is used to randomly select an item from the list.

If you need entries where some are more likley than others then you should use RolePlayer.table() instead.

All replies for this demo taken from Wikipedia

  1. 2: var magic8ball = RollPlayer.simpleTable('It is certain',
  2. 3: 'It is decidedly so', 'Without a doubt', 'Yes definitely',
  3. 4: 'You may rely on it', 'As I see it, yes', 'Most likely',
  4. 5: 'Outlook good', 'Yes', 'Signs point to yes',
  5. 6: 'Reply hazy try again', 'Ask again later',
  6. 7: 'Better not tell you now', 'Cannot predict now',
  7. 8: 'Concentrate and ask again', 'Don\'t count on it',
  8. 9: 'My reply is no', 'My sources say no',
  9. 10: 'Outlook not so good', 'Very doubtful');

Return an object so that our template code can access the star of this example as demo.rollable

  1. 11: return { rollable : magic8ball };
  2. 12: }();

Tutorial Builder is © Christian Heilmann, 2008. Licensed under the BSD license.