如何判断手牌与桌上牌是否一致?

发布于 2024-08-07 13:04:04 字数 1429 浏览 7 评论 0原文

桌子上有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2024-08-14 13:04:04

诀窍是先构建你的牌组,然后在分发牌时将牌从牌组中取出。

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.

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];
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文