试图找出我在这里失踪的东西!这些卡创建,我可以打印出对象,它的内容使用底部的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);
}
}
}());
那是愚蠢的我......谢谢。我已经修好了,通过很好,但是我似乎得到了一堆像这样undefined未定义的回报。我尝试将对象解析为字符串,但这没有什么区别。我更新了上面的代码。 – TheCrownedPixel 2014-10-08 22:03:07
您需要退回TheSuit和theSuit,而不是等级和套装。不要使用“这个”。并确保你也宣布了TheSuit和TheSuit。 – user227353 2014-10-08 22:08:39