Skip to content

Instantly share code, notes, and snippets.

@perjo927
Last active July 12, 2025 01:11
Show Gist options
  • Save perjo927/6878ee19bf058527cf6c5fd32371df82 to your computer and use it in GitHub Desktop.
Save perjo927/6878ee19bf058527cf6c5fd32371df82 to your computer and use it in GitHub Desktop.
Generator-based slot machine game
const getRandomInt = (range) => Math.floor(Math.random() * range);
// Create an array of symbols for the slot machine
const symbols = ['🍒', '🍋', '🔔', '7️⃣', '🎱'];
// Define a generator function that will yield a random symbol
// from the array when iterated over
function* getReels(noOfReels = 3) {
let i = 0;
while (i++ < noOfReels) {
yield symbols[getRandomInt(symbols.length)];
}
}
function* getSlotMachine() {
let balance = 0;
let betSize = 10;
let round = 1;
while (balance === 0) {
console.log('Please deposit coins!');
const deposit = yield balance;
balance += isNaN(deposit) ? 0 : deposit;
}
console.log('Game started!');
console.log(`Balance: ${balance}`);
while (balance > 0) {
// Consume any new coins
const deposit = yield balance;
balance += isNaN(deposit) ? 0 : deposit;
// Play game round, if enough balance
if (balance - betSize < 0) {
break;
}
console.log(`ROUND ${round++} BEGIN`);
balance -= betSize;
const result = [...getReels()];
// If we land three symbols in a row, we win
// A set can not contain duplicates
// A set with size 1 means there are 3 duplicates, hence we win
const areSame = new Set(result).size === 1;
// Print reader-friendly result
console.log(result.join(' | ')); // 7️⃣ | 🍒 | 🍒
if (areSame) {
// Find the weight of the winning symbol
const winSize = symbols.indexOf(result[0]);
const winInCoins = betSize * (winSize + 1);
// Calculate win using the weight of the index of the symbol
console.log(`You won ${winInCoins} coins!`);
balance += winInCoins;
} else {
console.log('No win');
}
console.log(`Balance: ${balance}`);
}
console.log('Game over!');
return 0;
}
let state;
const game = getSlotMachine();
game.next(); // "Please deposit coins"
state = game.next(30); // "Game started!"
// Autoplay
while (state.value > 0) {
state = game.next();
}
@renfri897
Copy link

I always appreciate when developers take the time to write clean and maintainable code. Have you considered adding a free spins feature? As someone who enjoys playing online slots in my free time, I’ve noticed that free spins are quite common in many of these games. Polish online casinos frequently offer them as part of their bonus systems, sometimes even without requiring a deposit. Talking about free spins, this site provides a helpful overview of Polish online casinos that offer 50 free spins. It also highlights the variety of games where these free spins can be used, ensuring players have a wide selection to choose from. The free spin mechanic allows players to spin the reels without betting their own money, typically activated by certain symbols or actions within the game. It’s a feature commonly found in video slots, where it can be further enhanced with things like multipliers or special symbols to boost the player’s chances and overall experience.

@amnazeeshan12
Copy link

Hey! Generator-based slot machine games are super exciting because they use random number generators to ensure every spin is fair and unpredictable. It keeps the gameplay fresh and thrilling. By the way, if you’re into games or gambling sites, check out 100PASARAN — they offer cool info and services related to that world! Worth a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment