2014-10-08 51 views
-1

試圖找出我在這裏失蹤的東西!這些卡創建,我可以打印出對象,它的內容使用底部的document.write(JSON.stringify());。底部的'main'方法是我試圖調用cardToString,它應該獲取對象的內容,然後返回一個字符串,然後輸出。似乎無法弄清楚我在這裏錯過了什麼。打印卡片組

(function() { 

function Card (rank, suit) { 

    this.rank = rank; 
    this.suit = suit; 

}; 

function Deck() { 

    this.deck = new Array(); 

    this.makeDeck = makeDeck; 
    this.shuffle = shuffle; 
    this.deal = deal; 
} 
function makeDeck() { 

    var ranks = new Array("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
        "J", "Q", "K"); 
    var suits = new Array("C", "D", "H", "S"); 

    this.deck = new Array(52); 

    var i, j; 
    for (i = 0; i < 4; i++) { 
     for (j = 0; j < 13; j++) { 
      this.deck[i*ranks.length + j] = new Card(ranks[j], suits[i]); 
     } 
    } 
}; 

function shuffle() { 
    var i, j, temp; 
    var n = 10; 
    for (i = 0; i < n; i++) { 
     for (j = 0; j < this.deck.length; j++) { 
      k = Math.floor(Math.random() * this.deck.length); 
      temp = this.deck[j]; 
      this.deck[j] = this.deck[k]; 
      this.deck[k] = temp; 
     } 
    } 
}; 

function deal() { 

    if (this.deck.length > 0) { 

     return this.deck.shift(); 
    } 
    else return null; 
}; 

function cardToString(rank, suit) { 

    document.write(rank + suit + " "); 
    var strRank, strSuit; 

    strRank = String(rank); 
    strSuit = String(suit); 

    var theRank, theSuit; 

    switch (strRank) { 
     case "A" : 
     theRank = "Ace"; 
     break; 
     case "2" : 
     theRank = "Two"; 
     break; 
     case "3" : 
     theRank = "Three"; 
     break; 
     case "4" : 
     theRank = "Four"; 
     break; 
     case "5" : 
     theRank = "Five"; 
     break; 
     case "6" : 
     theRank = "Six"; 
     break; 
     case "7" : 
     theRank = "Seven"; 
     break; 
     case "8" : 
     theRank = "Eight"; 
     break; 
     case "9" : 
     theRank = "Nine"; 
     break; 
     case "10" : 
     theRank = "Ten"; 
     break; 
     case "J" : 
     theRank = "Jack"; 
     break; 
     case "Q" : 
     theRank = "Queen"; 
     break; 
     case "K" : 
     theRank = "King"; 
     break; 
     default : 
     theRank = null; 
     break; 
    } 

    switch (strSuit) { 
     case "C" : 
     theSuit = "Clubs"; 
     break; 
     case "D" : 
     theSuit = "Diamonds"; 
     break; 
     case "H" : 
     theSuit = "Hearts"; 
     break; 
     case "S" : 
     theSuit = "Spades"; 
     break; 
     default : 
     theSuit = null; 
     break; 
    } 

    if (rank == null || suit == null) { 
     return ""; 
    } 

    return this.rank + " of " + this.suit; 
} 

var deck = new Deck(); 

deck.makeDeck(); 
deck.shuffle(); 
for (i = 0; i < 2; i++) { 
    for (j = 0; j < 4; j++) { 
     var Card; 
     Card = deck.deal(); 
     //document.write(Card.rank); 
     var v = cardToString(Card.rank, Card.suit); 
     document.write(v); 
    } 
} 


    }()); 

回答

0

我覺得你忘了完成你的cardToString函數。你需要進行排名,作爲輸入,並將它們傳入像var v = cardToString(Card.rank,Card.suit);

+0

那是愚蠢的我......謝謝。我已經修好了,通過很好,但是我似乎得到了一堆像這樣undefined未定義的回報。我嘗試將對象解析爲字符串,但這沒有什麼區別。我更新了上面的代碼。 – TheCrownedPixel 2014-10-08 22:03:07

+0

您需要退回TheSuit和theSuit,而不是等級和套裝。不要使用「這個」。並確保你也宣佈了TheSuit和TheSuit。 – user227353 2014-10-08 22:08:39

1

爲了從卡對象中獲取值,您需要將它作爲參數傳遞給cardToString()。由於您的對象名稱是Card,因此它將被編寫爲cardToString(Card)

要拉出卡對象的值,您需要稍微修改cardToString函數。您將使用通過名爲card的對象,而不是使用this。我已經更新了下面的功能,給你和這個如何工作的想法。

function cardToString(card) { 

    var rank, suit; 

    switch (card.rank) { 
     case "A" : 
     rank = "Ace"; 
     break; 
     case "2" : 
     rank = "Two"; 
     break; 
     case "3" : 
     rank = "Three"; 
     break; 
     case "4" : 
     rank = "Four"; 
     break; 
     case "5" : 
     rank = "Five"; 
     break; 
     case "6" : 
     rank = "Six"; 
     break; 
     case "7" : 
     rank = "Seven"; 
     break; 
     case "8" : 
     rank = "Eight"; 
     break; 
     case "9" : 
     rank = "Nine"; 
     break; 
     case "10" : 
     rank = "Ten"; 
     break; 
     case "J" : 
     rank = "Jack"; 
     break; 
     case "Q" : 
     rank = "Queen"; 
     break; 
     case "K" : 
     rank = "King"; 
     break; 
     default : 
     rank = null; 
     break; 
    } 

    switch (card.suit) { 
     case "C" : 
     suit = "Clubs"; 
     break; 
     case "D" : 
     suit = "Diamonds"; 
     break; 
     case "H" : 
     suit = "Hearts"; 
     break; 
     case "S" : 
     suit = "Spades"; 
     break; 
     default : 
     suit = null; 
     break; 
    } 

    if (rank == null || suit == null) { 
     return ""; 
    } 

    return rank + " of " + suit; 
} 
+0

對不起,這沒有奏效。我對上面的代碼做了一些修改,但是它似乎沒有吸引任何案例。 – TheCrownedPixel 2014-10-08 22:10:44