在 JavaScript 中为我的牌组生成卡片时出现随机错误
Posted
技术标签:
【中文标题】在 JavaScript 中为我的牌组生成卡片时出现随机错误【英文标题】:I get a random error when generating cards for my deck in JavaScript 【发布时间】:2022-01-11 15:02:52 【问题描述】:我的目标是做一个简单的纸牌游戏。我有这个错误,有时它不会将其中一个对象推入数组。首先,我认为选择的数字不适合用于声明对象值的 if 语句。
我尝试在获得随机值后立即手动重新定义pickedNumber。然后它起作用了。我遇到问题的数字是:36、38、24、25、37,当它是随机的,但当我手动定义 var PickNumber 时,它可以正常工作。
我该如何解决这个问题?
picture of when the code fails
picture of when it works
function log(txt)
console.log(txt);
let cards = [];
let hand = [];
// fill card deck
for (let i = 1; i < 53; i++)
cards.push(i);
// index for to make the random math not to choose a number over the highest index of cards[]
// loop for picking some random card with a value
for (let i = 0; i < 3; i++)
// random index to choose
let randomNumber = Math.floor(Math.random() * cards.length);
log(randomNumber);
// random number
let pickedNumber = cards[randomNumber];
log(pickedNumber);
// remove the picked card
const index = cards.indexOf(pickedNumber);
if (index > -1)
cards.splice(index, 1);
let finalValue;
let card =
value: finalValue,
suit: "",
;
// these if statements are for deviding the cards from 52 to 4x13
if (pickedNumber < 14)
card.value = pickedNumber;
card.suit = "♥";
hand.push(card);
else if (pickedNumber > 13 && pickedNumber < 26)
card.value = pickedNumber -= 13;
card.suit = "♣";
hand.push(card);
else if (pickedNumber > 26 && pickedNumber < 39)
card.value = pickedNumber -= 26;
card.suit = "♦";
hand.push(card);
else if (pickedNumber > 39 && pickedNumber < 53)
card.value = pickedNumber -= 39;
card.suit = "♠";
hand.push(card);
// reduce maxIndex to dont overpick index
log(hand);
【问题讨论】:
这个循环for (let i = 1; i < 53; i++)
进行了 51 次迭代。
【参考方案1】:
一副纸牌的更完整表示非常简单且明智。
在下面的 sn-p 中,freshDeck
枚举了一个完整的标准牌组,使用push()
来添加卡片。 Fisher-Yates shuffle()
洗牌,slice()
偷看卡片,splice()
将卡片从一堆(阵列)移到另一堆。
function freshDeck()
const suits = [ "♥", "♣", "♦","♠"];
const values = ["A","2","3","4","5","6","7","8","9", "10","J","Q","K"]
let deck = []
for (const suit of suits)
for (const value of values)
deck.push( suit, value )
return deck
// fy shuffle, thanks to https://***.com/a/2450976/294949
function shuffle(array)
let currentIndex = array.length, randomIndex;
while (currentIndex != 0)
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
return array;
// create a deck
let deck = freshDeck()
console.log(`the deck has $deck.length cards`)
console.log(`the first few cards are $JSON.stringify(deck.slice(0,3))`)
// suffle the deck
shuffle(deck)
console.log(`\nafter a shuffle, the first few cards are $JSON.stringify(deck.slice(0,3))`)
// deal a hand of cards
let hand = deck.splice(0, 5)
console.log(`\ndealt a hand with $JSON.stringify(hand, null, 0)`)
console.log(`\nafter dealing, the deck has $deck.length cards`)
【讨论】:
【参考方案2】:您的 IF 语句中有错误。您在每组中都缺少一个数字
if (pickedNumber < 14)
...
else if (pickedNumber > 13 && pickedNumber < 26)
...
else if (pickedNumber > 26 && pickedNumber < 39)
...
else if (pickedNumber > 39 && pickedNumber < 53)
...
看看,在上面的代码块中,如果数字是 26 或 39,它将不会被拾取。当我们将< 26
更改为<= 26
时,它就可以被检测到。
if (pickedNumber <= 14) //
...
else if (pickedNumber > 14 && pickedNumber <= 26)
...
else if (pickedNumber > 26 && pickedNumber <= 39)
...
else if (pickedNumber > 39 && pickedNumber <= 53)
...
【讨论】:
以上是关于在 JavaScript 中为我的牌组生成卡片时出现随机错误的主要内容,如果未能解决你的问题,请参考以下文章