我怎样才能让我的牌组发出正确的花色?

我试图让当你点击一个按钮时,它会显示一副纸牌。我为每种花色都有一个按钮,所以当你点击它时,它就会处理该花色。我已经用它来处理红心,但当我尝试处理钻石时,它仍然显示红心。我究竟做错了什么?我该如何展示我的钻石?

var heart = true;

var diamond = true;

var suit = "";


function card(name,suit,value) {

    this.name = name;

    this.suit = suit;

    this.value = value;

}


var deck = [

    new card('Ace', 'Hearts',11),

    new card('Two', 'Hearts',2),

    new card('Three', 'Hearts',3),

    new card('Four', 'Hearts',4),

    new card('Five', 'Hearts',5),

    new card('Six', 'Hearts',6),

    new card('Seven', 'Hearts',7),

    new card('Eight', 'Hearts',8),

    new card('Nine', 'Hearts',9),

    new card('Ten', 'Hearts',10),

    new card('Jack', 'Hearts',10),

    new card('Queen', 'Hearts',10),

    new card('King', 'Hearts',10),

    new card('Ace', 'Diamonds',11),

    new card('Two', 'Diamonds',2),

    new card('Three', 'Diamonds',3),

    new card('Four', 'Diamonds',4),

    new card('Five', 'Diamonds',5),

    new card('Six', 'Diamonds',6),

    new card('Seven', 'Diamonds',7),

    new card('Eight', 'Diamonds',8),

    new card('Nine', 'Diamonds',9),

    new card('Ten', 'Diamonds',10),

    new card('Jack', 'Diamonds',10),

    new card('Queen', 'Diamonds',10),

    new card('King', 'Diamonds',10),

    new card('Ace', 'Clubs',11),

    new card('Two', 'Clubs',2),

    new card('Three', 'Clubs',3),

    new card('Four', 'Clubs',4),

    new card('Five', 'Clubs',5),

    new card('Six', 'Clubs',6),

    new card('Seven', 'Clubs',7),

    new card('Eight', 'Clubs',8),

    new card('Nine', 'Clubs',9),

    new card('Ten', 'Clubs',10),

    new card('Jack', 'Clubs',10),

    new card('Queen', 'Clubs',10),

];


慕慕森
浏览 138回答 1
1回答

桃花长相依

如果您不想为了更优雅的解决方案而重写所有内容,您可以在处理钻石时将牌组的索引添加 12。但是,如果您最终洗牌,则此方法不会起作用。function dealDiamonds(d) {&nbsp; &nbsp; if (d < 13) {&nbsp; &nbsp; &nbsp; &nbsp; displayDiamonds = deck[d + 12];&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; }}一个更优雅的解决方案是拥有一个函数来获取某套特定的牌,而不依赖于牌组的特定顺序。function dealDiamonds(d) {&nbsp; &nbsp; if (d < 13) {&nbsp; &nbsp; &nbsp; &nbsp; displayDiamonds = getCardOfSuit('Diamonds', d);&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; //...&nbsp; &nbsp; }}function getCardOfSuit(suit, index) {&nbsp; &nbsp; //filter the deck to only the deck of a certain suit&nbsp; &nbsp; let suitDeck = deck.filter((card) => card.suit == suit);&nbsp; &nbsp; //returns the card (0 - 12) based on the second parameter&nbsp; &nbsp; return suitDeck[index];}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript