简体   繁体   English

如何检查手中的牌是否与桌上的牌相匹配?

[英]How to check if cards in hand match cards on table?

There are 8 cards on the table, with four faces visible and four hidden.桌子上有 8 张卡片,其中 4 张脸是可见的,4 是隐藏的。 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.问题是,我要么在逻辑上做错了,要么 .concat() 不起作用。 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).整个游戏可能可以重构为适当的对象,但这超出了我目前的水平(我已经学习了一个月的 JS)。 Framework used is RightJS.使用的框架是 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();
    }
}

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.下面是使用classesArray.splice()和 52 卡的一种方法。 You draw a card with Deck.drawCard().你用 Deck.drawCard() 抽一张牌。

Here is a demo.这是一个演示。

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];
  }
}

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

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