打印出卡片组

时间:2014-10-08 21:38:30

标签: javascript object

试图找出我在这里缺少的东西!卡片创建,我可以使用底部的document.write(JSON.stringify());打印出对象及其内容。主要'底部的方法是我试图调用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);
    }
}


    } ());

2 个答案:

答案 0 :(得分:1)

为了从卡片对象中获取值,您需要将其作为参数传递给cardToString()。由于您的对象名称为Card,因此会将其写为cardToString(Card)

要从卡片对象中提取值,您需要稍微修改cardToString功能。您不必使用this,而是使用名为card的对象。我已经更新了下面的功能,让您了解这可能会如何运作。

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;
}

答案 1 :(得分:0)

我想你忘了为cardToString完成你的功能。你需要制作排名,适合作为输入,并像var v = cardToString(Card.rank,Card.suit)一样传递它们;