亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎樣才能讓我的牌組發出正確的花色?

我怎樣才能讓我的牌組發出正確的花色?

慕慕森 2023-07-20 17:24:07
我試圖讓當你點擊一個按鈕時,它會顯示一副紙牌。我為每種花色都有一個按鈕,所以當你點擊它時,它就會處理該花色。我已經用它來處理紅心,但當我嘗試處理鉆石時,它仍然顯示紅心。我究竟做錯了什么?我該如何展示我的鉆石?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),];
查看完整描述

1 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

如果您不想為了更優雅的解決方案而重寫所有內容,您可以在處理鉆石時將牌組的索引添加 12。但是,如果您最終洗牌,則此方法不會起作用。


function dealDiamonds(d) {

    if (d < 13) {

        displayDiamonds = deck[d + 12];

        //...

    } else {

        //...

    }

}

一個更優雅的解決方案是擁有一個函數來獲取某套特定的牌,而不依賴于牌組的特定順序。


function dealDiamonds(d) {

    if (d < 13) {

        displayDiamonds = getCardOfSuit('Diamonds', d);

        //...

    } else {

        //...

    }

}


function getCardOfSuit(suit, index) {

    //filter the deck to only the deck of a certain suit

    let suitDeck = deck.filter((card) => card.suit == suit);


    //returns the card (0 - 12) based on the second parameter

    return suitDeck[index];

}


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號