我正在创建一个二十一点游戏,其中我的牌组由数组中的对象(卡名:值)组成。
我想知道是否有一个用于javascript对象的函数,我可以在其中获取对象值(在我的情况下,每张卡的数字分数)而不必命名键(例如:“aced”)?
请参考我下面的代码,特别是在 for 循环中;我不知道用什么替换“myHand[card[value]]”。
谢谢
function shuffle(array) {
var m = array.length, t, i;
// While there remain elements to shuffle…
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
var deck = [{"aced": 11}, {"twod": 2}, {"threed": 3}, {"fourd": 4}, {"fived": 5}, {"sixd":6}, {"sevend": 7}, {"eightd": 8}, {"nined": 9}, {"tend": 10}, {"jackd": 10}, {"queend": 10}, {"kingd": 10},
{"acec": 11}, {"twoc": 2}, {"threec": 3}, {"fourc": 4}, {"fivec": 5}, {"sixc":6}, {"sevenc": 7}, {"eightc": 8}, {"ninec": 9}, {"tenc": 10}, {"jackc": 10}, {"queenc": 10}, {"kingc": 10},
{"aceh": 11}, {"twoh": 2}, {"threeh": 3}, {"fourh": 4}, {"fiveh": 5}, {"sixh":6}, {"sevenh": 7}, {"eighth": 8}, {"nineh": 9}, {"tenh": 10}, {"jackh": 10}, {"queenh": 10}, {"kingh": 10},
{"aces": 11}, {"twos": 2}, {"threes": 3}, {"fours": 4}, {"fives": 5}, {"sixs":6}, {"sevens": 7}, {"eights": 8}, {"nines": 9}, {"tens": 10}, {"jacks": 10}, {"queens": 10}, {"kings": 10}];
shuffle(deck);
function userHand() {
var myHand = [deck.pop(), deck.pop()];
}
function countScore(myHand) {
var total = 0;
for (var card in myHand) {
total += myHand[card[value]];
}
}