[桌子上有8张卡,可见四个脸,四个隐藏着。单击卡片以将其打开,如果有点子匹配或西服匹配,请在关联的卡片周围显示火花。
问题是,我在逻辑上做错了,或者.concat()无法正常工作。因为有些火花显示,有些则没有。
整个游戏可能可以重构为适当的对象,但这超出了我目前的水平(我已经学习JS一个月了。)使用的框架是RightJS。为了清晰起见和上下文,发布了整个函数。
function pick(card) {
var matches = [],
pip = [],
suit = [];
//Check for matches
['card1', 'card2', 'card3', 'card4'].each(function (el) {
if (hand[el].charAt(0) == 'j') {
matches.push(card);
matches.push(el);
} //Joker
else if (hand[card].charAt(1) == hand[el].charAt(1) || hand[card].charAt(0) == 'j') {
matches.push(card);
pip.push(el);
} //Pip match
else if (hand[card].charAt(0) == hand[el].charAt(0) || hand[card].charAt(0) == 'j') {
matches.push(card);
suit.push(el);
} //Suit match
});
if (pip.length > suit.length) {
matches.concat(pip);
} else {
matches.concat(suit);
}
//Hide old bling
$$('.bling').each(function (el) {
el.hide();
});
//Show bling
if (matches.length > 0) {
matches.each(function (el) {
$(el).firstChild.show();
});
}
//Show the card from hand
$(card).setClass(hand[card]);
turned++;
// New turn if all have been clicked
if (turned == 4) {
turned = 0;
newturn();
}
}
诀窍是先建立卡片组,然后在分发卡片时从卡片组中取出卡片。 😊
以下是使用classes,Array.splice()和52张卡的一种方法。您使用Deck.drawCard()绘制一张牌。
class Card {
constructor(suit, value) {
this.suit = suit;
this.value = value;
}
}
class Deck {
constructor() {
const suits = ["H", "D", "S", "C"];
const values = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"];
this.cards = [];
suits.forEach(suit =>
values.forEach(value => {
const newCard = new Card(suit, value);
this.cards.push(newCard);
})
);
}
drawCard() {
if (!this.cards.length) return false;
const cardIndex = Math.floor(Math.random() * this.cards.length);
return this.cards.splice(cardIndex, 1)[0];
}
}