Skip to content

Instantly share code, notes, and snippets.

@perjo927
Last active December 11, 2025 09:30
Show Gist options
  • Select an option

  • Save perjo927/6878ee19bf058527cf6c5fd32371df82 to your computer and use it in GitHub Desktop.

Select an option

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();
}
@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.

@jackbacklang
Copy link

The 1win https://1win-bet.com.in/ platform is designed as a multifunctional online service where users can access a wide range of digital entertainment. Its interface focuses on clarity, offering organized menus, account settings, and support options. Visitors can explore different sections to understand what the platform provides, while various tools help them manage their activity responsibly. The site also highlights its security features and account protection measures, giving users a better sense of how the system works and what to expect when navigating through its features.

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