Last active
December 11, 2025 09:30
-
-
Save perjo927/6878ee19bf058527cf6c5fd32371df82 to your computer and use it in GitHub Desktop.
Generator-based slot machine game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
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
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.