試圖找出我在這裏失蹤的東西!這些卡創建,我可以打印出對象,它的內容使用底部的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