2014-10-08 69 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