[英]How can I assign unique and linear key for each object within an array that's in an object within another array?
[英]How do I assign an object to an array within another object?
我正在接受二十一點挑戰,並提出了自己的代碼。 所以這就是到目前為止我所擁有的...我的卡生成器...
var card = {
suit: null,
face: null,
value: null,
generateSuit: function (x) {
if (x == 0) this.suit = 'Clubs';
if (x == 1) this.suit = 'Diamonds';
if (x == 2) this.suit = 'Hearts';
if (x == 3) this.suit = 'Spades';
},
generateFace: function (y) {
if (y > 1 && y < 11) this.face = y,
this.value = y;
else {
if (y == 1) this.face = 'Ace', this.value = 1;
if (y == 11) this.face = 'Jack', this.value = 10;
if (y == 12) this.face = 'Queen', this.value = 10;
if (y == 13) this.face = 'King', this.value = 10;
};
},
generateCard: function () {
var x = Math.floor(Math.random() * 4);
var y = Math.floor(Math.random() * 13 + 1);
this.generateSuit(x);
this.generateFace(y);
},
}
這樣做很好,我只是將其包括在內,因為下一部分讓我感到難過,所以我認為自己將一無所獲。
這就是問題的根源-在我的“手”中(下貼),我正在使用“ generateCard()”函數設置“ card”的值,然后將其存儲在名為“ storecard []”的數組中。
var hand = {
storecard: [],
count: 0,
total: 0,
hitCard: function () {
this.count += 1;
card.generateCard();
this.storecard[this.count] = Object.create(card);
this.total += this.storecard[this.count].value;
this.logHand();
},
logHand: function () {
console.log(this.storecard[this.count].face + ' of ' + this.storecard[this.count].suit);
console.log('Value = ' + this.storecard[this.count].value);
console.log('Count = ' + this.count);
console.log('Hand Total = ' + this.total);
}
}
這樣的想法是,每次打牌(添加到手牌)時,“手牌”會將其(及其所有屬性-西服,價值等)存放在storecard[index == hand count, clever eh?]
這樣,我可以隨時獲取手中任何卡的任何屬性,但是我可以通過訪問storecard[index]
。
好吧,不是很多。 運行代碼,該數組是唯一可以解決的問題...
var me = Object.create(hand);
var dealer = Object.create(hand);
me.hitCard();
me.hitCard();
我的'logHand()'函數表明一切正常!
"5 of Hearts"
"Value = 5"
"Count = 1"
"Hand Total = 5"
"2 of Clubs"
"Value = 2"
"Count = 2"
"Hand Total = 7"
但是可惜,“存儲卡”陣列失敗了。 經過進一步檢查,我們發現:
me.storecard[1].value
2
me.storecard[2].value
2
storecard[1]
應該握住我的5個心,但已經被2個俱樂部覆蓋! 如果那還不夠令人恐懼...
dealer.storecard[1].value
2
dealer.storecard[1].value
2
我什至還沒有碰過經銷商對象!
現在,我手中和庄家手中的所有其他變量都與現實一致-數組是唯一的問題。
我在這里做錯了什么?
問題在於您對Object.create()
期望。
它創建對象的淺表副本。 數組是按引用而不是值分配的。
例如
var arrayOne = [];
var arrayTwo = arrayOne;
arrayOne.push('3');
alert(arrayTwo[0]); // This will give you 3. Both variables are pointing to the same array
所以:
var handTemplate = {
childArray: []
};
var handOne = Object.create(handTemplate);
var handTwo = Object.create(handTemplate);
// At this point, handTemplate, handOne and handTwo all point to the same array
handOne.childArray.push('3');
alert(handTwo.childArray([0])); // Again, you'll see 3, because there's only one array
一種解決方案是使用專用功能創建hand
:
function createHand() {
return {
storecard: [], // Now we get a brand new array every time createHand is called
count: 0,
total: 0,
hitCard: function () {
this.count += 1;
card.generateCard();
this.storecard[this.count] = Object.create(card);
this.total += this.storecard[this.count].value;
this.logHand();
},
logHand: function () {
console.log(this.storecard[this.count].face + ' of ' + this.storecard[this.count].suit);
console.log('Value = ' + this.storecard[this.count].value);
console.log('Count = ' + this.count);
console.log('Hand Total = ' + this.total);
}
};
}
var me = createHand();
var dealer = createHand();
嗯,這是一種不同的方法,但是是一種很好的方法。
我已經以一種您可以學到一些東西的方式編寫了代碼。
例如,您可以完全擺脫count變量,因為它等於storecard.length。
但是請記住,您仍然需要避免使用雙面卡。 您可以輕松地做到這一點,方法是先生成所有卡,將其放入陣列,然后隨機將其拼接出來,直到堆棧為空。
var Card = function () {
this.suit = null;
this.face = null;
this.value = null;
this.generateCard();
};
Card.prototype = {
generateSuit: function (x) {
this.suit = ["Clubs", "Diamonds", "Hearts", "Spades"][x];
},
generateFace: function (y) {
this.value = Math.min(10, y);
switch (y) {
case 1:
this.face = "Ace";
break;
case 11:
this.face = "Jack";
break;
case 12:
this.face = "Queen";
break;
case 13:
this.face = "King";
break;
default:
this.face = y;
break;
}
},
generateCard: function () {
this.generateSuit(Math.floor(Math.random() *4));
this.generateFace(Math.floor(Math.random() *13 +1));
}
}
var Hand = function () {
this.storecard = [];
this.count = 0;
this.total = 0;
};
Hand.prototype = {
hitCard: function () {
this.count++;
var card = new Card();
this.storecard.push(card);
this.total += card.value;
this.logHand();
},
logHand: function () {
var card = this.storecard[this.storecard.length -1];
console.log(card.face + ' of ' + card.suit);
console.log('Value = ' + card.value);
console.log('Count = ' + this.count);
console.log('Hand Total = ' + this.total);
}
}
結果將是:
> var me = new Hand();
undefined
> var dealer = new Hand();
undefined
> me.hitCard();
Ace of Hearts
Value = 1
Count = 1
Hand Total = 1
undefined
> me.hitCard();
6 of Diamonds
Value = 6
Count = 2
Hand Total = 7
undefined
> me.storecard
[ { suit: 'Hearts',
face: 'Ace',
value: 1 },
{ suit: 'Diamonds',
face: 6,
value: 6 } ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.