简体   繁体   中英

JavaScript Slot Machine: What Is A Simple Algorithm For Picking A Winner?

I'm developing a slot machine in JavaScript, and this slot machine has 3 reels.

Each reel is identical and contains 4 symbols: A bar, a 7, a bell, and a grape. The bar is displayed on the reel one time, the 7 two times, the bell three times, and the grape four times - for a total of 10 symbols per reel. And each symbol corresponds with a number from 0 - 9:

| bar | -> 9
|  7  | -> 8 
|  7  | -> 7
| bell| -> 6
| bell| -> 5
| bell| -> 4
|grape| -> 3
|grape| -> 2
|grape| -> 1
|grape| -> 0

I'm then randomly generating a number from 0 - 999, and based on that number I would like to potentially select a winner. For example, if the number is 999 that is three bars and the first jackpot, if the number is 210 that is three grapes and the fourth jackpot. If the number is 359 that is grape/bell/bar and no jackpot.

I had created an if/else to select the winner but realized it does not take into account the second winning situation of 210:

if (number == 999) {
    // Jackpot 1
} else if (number == 888) {
    // Jackpot 2
} else if (number == 777) {
    // Jackpot 2
} else if (number == 666) {
    // Jackpot 3
} else if (number == 555) {
    // Jackpot 3
} else if (number == 444) {
    // Jackpot 3
} else if (number == 333) {
    // Jackpot 4
} else if (number == 222) {
    // Jackpot 4
} else if (number == 111) {
    // Jackpot 4
} else if (number == 0) {
    // Jackpot 4
} else {
    // No jackpot
}

Does anyone have any ideas for how I might devise a simple algorithm for selecting a winner, as opposed to a bunch of if/else's? Also, there needs to be a total of 10 possible outcomes per reel, as the top jackpot should have a probability of 10^3 => 1 in 1,000.

I think doing the arithmetic with a numeric lookup-table will be the most efficient:

 function isJackPot(number) { const symbols = [4,4,4,4,3,3,3,2,2,1]; const winner = symbols[number % 10]; // get symbol for rightmost digit for (let i = 0; i < 2; i++) { number = Math.floor(number/10); // shift digits to get next symbol if (symbols[number % 10] !== winner) return 0; // bail out when not the same } return winner; } const tests = [999, 210, 359]; for (const test of tests) { console.log(`${test} gives jackpot ${isJackPot(test)}`); } 

NB: a return value 0 means there is no jackpot. Any other number (1..4) denotes the kind of jackpot.

Create an array of arrays.

[[9], [7,8], [4,5,6] ,[0,1,2,3]]

Then convert number to String and use every() and includes()

Working snippet:

 function play(num){ let number = num || Math.floor(Math.random() * 899) + 100; console.log(`Random Number: ${number}`) number = number.toString().split('') let arr = [[0,1,2,3],[4,5,6],[7,8],[9]]; let jackpot = arr.findIndex(x => number.every(a => x.includes(+a))) + 1 console.log(`jackpot:${jackpot}`) } play(999) play(123) play(446) play(129) 
 <button onclick="play()">click</button> 

You can check below code also using simple JavaScript which work for all browsers.

 function checkJackpot(digit) { var jackpot = { "9": "1", "8": "2", "7": "2", "6": "3", "5": "3", "4": "3", "3": "4", "2": "4", "1": "4", "0": "4" } var number = digit.toString().split(''); var count = []; for (var i = 0; i < number.length; i++) { var j = jackpot[number[i]] if (count.indexOf(j) == -1) count.push(jackpot[number[i]]) } if (count.length == 1) { alert("Jackpot " + count[0]) } else { alert("No jackpot") } } checkJackpot(219) checkJackpot(999) checkJackpot(110) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM