如何判断手牌与桌上牌是否一致?
桌子上有 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();
}
}
There are 8 cards on the table, with four faces visible and four hidden. Click on a card to turn it and if there's a pip-match or suit-match, show sparks around the associated cards.
Problem is, I'm either doing something wrong logic-wise, or .concat() is not working. Because some sparks show and some do not.
The whole game could probably be refactored into proper objects but that is beyond my current level (I've been learning JS for a month now). Framework used is RightJS. Posted the whole function for clarity's sake and a bit of context.
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
诀窍是先构建你的牌组,然后在分发牌时将牌从牌组中取出。
The trick is to build your deck of cards first, and then remove cards from the deck as you hand them out. ????
Below is one way of doing it with classes, Array.splice() and 52 cards. You draw a card with Deck.drawCard().
Here is a demo.