在 JavaScript 中为我的套牌生成卡片时出现随机错误
I get a random error when generating cards for my deck in JavaScript
我的目标是制作一款简单的纸牌游戏。我有这个错误,有时它不会将其中一个对象推入数组。首先,我认为选择的数字不适合用于声明对象值的 if 语句。
我试图在获得随机值后立即手动重新定义 pickedNumber。然后它起作用了。我遇到问题的数字是:36、38、24、25、37,当它是随机的时,但是当我手动定义 var pickedNumber 时,它可以正常工作。
我该如何解决这个问题?
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);
您的 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) {
...
}
一副纸牌的更完整表示非常简单且明智。
在下面的代码片段中,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
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`)
我的目标是制作一款简单的纸牌游戏。我有这个错误,有时它不会将其中一个对象推入数组。首先,我认为选择的数字不适合用于声明对象值的 if 语句。
我试图在获得随机值后立即手动重新定义 pickedNumber。然后它起作用了。我遇到问题的数字是:36、38、24、25、37,当它是随机的时,但是当我手动定义 var pickedNumber 时,它可以正常工作。
我该如何解决这个问题?
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);
您的 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) {
...
}
一副纸牌的更完整表示非常简单且明智。
在下面的代码片段中,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
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`)