Slot machine mathematics in JavaScript

Written by Vincent Bruijn

About two years ago I got the idea of creating an online slot machine. The purpose was not to earn any money with it, the goal was to create an online artwork that consists of a slot machine. I got inspired by a work from Jeff Koons called β€œThree Ball Total Equilibrium Tank”: a glass tank filled with some fluid in which three Spalding basketball balls are floating in total equilibrium. I wanted to have the math correct. This lead me to research how slot machine math actually works, and how I would need to implement this in JavaScript, using React.

The three balls in a row in Jeff Koons work can be seen as the three slots of a slot machine. I noticed that emoji has a nice set of pictures of all kinds of sports balls: πŸ€, 🏐, 🎾, 🏈, πŸ‰, ⚽️, ⚾️ and 🎱. Could I use these as alternatives to the fruity images on regular slot machines?

Slot machine reels

The research lead me to a couple of websites about slot machine math. Most informative was Edward Shoreβ€˜s blog post on slot machine probability, but we also learned from Rob Dixonβ€˜s blog. Parallel to this research, I found images online of slot machine reels, see for example the above. And there are actually online stores that sell vintage reels. Counting the unique images on them, I noticed that it should be possible to make a working slot machine with the ball emojis.

I came to know that the odds of slot machines are officially described in what is called a par sheet. I set up a par sheet in Google Sheets and added several functions and winning combos. By tweaking the number of combos, in relation to the win value and the payoff, I tweaked the slot machine to a payout value of about 91%.

I noticed that playing around with the pay table, you can create an experience in which one wins often (higher hit frequency), but just not with too much value (the pays), and still be on a loosing path, as long as the total payout is lower than the total amount of possibilities (in the above case 7323 over 8000).

The score definition in JavaScript is eventually a simple switch statement with some string comparison. You only need to be aware to put the cases in the correct order to mitigate the risk of having a more relaxed result be matched earlier than a stricter one.

defineScore(result) {
  const res = result.join('');
  let score = 0;

  switch(true) {
    case(res === 'πŸ€πŸ€πŸ€'):
      score = 400;
      break;
    case(res.startsWith('πŸ€πŸ€')):
      score = 2;
      break;
    case(res.startsWith('πŸ€')):
      score = 1;
      break;
    case(res === '⚽⚽⚽'):
      score = 14;
      break;
    case(res === '🏈🏈🏈'):
      score = 18;
      break;
    case(res === '⚾⚾⚾'):
      score = 10;
      break;
    case(res === '🎾🎾🎾'):
      score = 50;
      break;
    case(/^🏐🏐/.test(res)):
      score = 5;
      break;
    case(res === 'πŸ‰πŸ‰πŸ‰'):
      score = 100;
      break;
    case(res === '🎱🎱🎱'):
      score = 200;
      break;
    case(/^🏐/.test(res)):
      score = 2;
      break;
    case(res === 'πŸˆπŸˆπŸ€'):
      score = 18;
      break;
    case(res === 'βš½βš½πŸ€'):
      score = 14;
      break;
    case(res === 'βšΎβšΎπŸ€'):
      score = 10;
      break;
    case(/^🎾🎾/.test(res)):
      score = 5;
      break;
    case(/^πŸ‰/.test(res)):
      score = 2;
      break;
  }

  console.log('score:',score, score * 1e4);

  return score;
}

The resulting work is published in collaboration with ax710 and can be found at http://πŸ€πŸ€πŸ€.infrath.in/. Hit that red button!