简体   繁体   English

JavaScript老虎机:选择获胜者的简单算法是什么?

[英]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. 我正在用JavaScript开发老虎机,该老虎机有3个转盘。

Each reel is identical and contains 4 symbols: A bar, a 7, a bell, and a grape. 每个卷轴都是相同的,并包含4个符号:一个条形,一个7个,一个铃铛和一个葡萄。 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. 条形图在卷轴上显示一次,七次显示两次,钟形显示三次,葡萄显示四次-每个卷轴总共显示10个符号。 And each symbol corresponds with a number from 0 - 9: 每个符号对应一个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. 然后,我随机生成一个0到999之间的数字,并希望根据该数字选择一个获胜者。 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. 例如,如果数字是999,即三个小节和第一个大奖,如果数字是210,则是三个小葡萄和第四个大奖。 If the number is 359 that is grape/bell/bar and no jackpot. 如果数字是359,则为葡萄/铃铛/酒吧,且没有累积奖金。

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 / else来选择获胜者,但意识到它没有考虑到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? 有人对我如何设计一种简单的算法来选择获胜者(而不是一堆if / else)有任何想法吗? 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. 另外,每个卷轴总共需要有10种可能的结果,因为头奖的概率应为10 ^ 3 => 1 / 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. 注意:返回值0表示没有累积奖金。 Any other number (1..4) denotes the kind of jackpot. 其他任何数字(1..4)表示累积奖金的类型。

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() 然后将数字转换为String并使用every()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. 您还可以使用适用于所有浏览器的简单JavaScript来检查以下代码。

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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