初始
cardCount = { chest: [0], chance: [0]};
创建卡片
function createCards() {
cards = {
chance: [{
title: 'Advance to go',
type: 'move',
position: 40
}, {
title: "Advance to London",
type: "move",
position: 39
}, {
title: "Your ass is going to jail",
type: "move",
position: 10
}, {
title: "Advance to Rome",
type: "move",
position: 24
}, {
title: "Advance to Charles de Gaulle",
type: "move",
position: 15
}, {
title: "Advance to Amsterdam",
type: "move",
position: 11
}, {
title: "Go back 3 spaces",
type: "movex",
position: -3
}, {
title: "No drink and driving mate1",
type: "bill",
bill: 20
}, {
title: "Get out of Jail free card",
type: "bill",
bill: 150
}, {
title: "Pay school fees",
type: "bill",
bill: 150
}, {
title: "Speeding fine",
type: "bill",
bill: 150
}, {
title: "Bank pays you dividend",
type: "bonus",
bonus: 40
}, {
title: "You have won the competition",
type: "bonus",
bonus: 200
}, {
title: "Your building loan matures",
type: "bonus",
bonus: 200
}, {
title: "You are assessed for street repairs $40 per house $115 per hotel",
type: "billx"
}, {
title: "House repairs $25 per house $100 per hotel",
type: "billx"
}],
chest: [{
title: "Advance to go",
type: "move",
newposition: 40,
bonus: 200
}, {
title: "Advance to Cairo",
type: "move",
newposition: 1
}, {
title: "Go to Jail",
type: "move",
newposition: 10
}, {
title: "Pay hospital fees",
type: "bill",
bill: 100
}, {
title: "Pay doctor fees",
type: "bill",
bill: 50
}, {
title: "Pay insurance premium",
type: "bill",
bill: 50
}, {
title: "Bank error. Collect $200",
type: "bonus",
bonus: 200
}, {
title: "Annuity matures. Collect $100",
type: "bonus",
bonus: 100
}, {
title: "You inherit $100",
type: "bonus",
bonus: 100
}, {
title: "From sale of stock you get $50",
type: "bonus",
bonus: 50
}, {
title: "Preference shares: $25",
type: "bonus",
bonus: 25
}, {
title: "You have won second prize in a beauty contest. Collect $10.",
type: "bonus",
bonus: 10
}, {
title: "It is your birthday. Collect $10.",
type: "bonus",
bonus: 10
}, {
title: "You win the lottery. Collect $10",
type: "bonus",
bonus: 10
}]
};
}
选取
function pickCard(type) {
var x = Math.floor(Math.random() * (cards[type].length));
var title = cards[type][x].title;
cardCount[type][0]++;
cards[type].splice(x, 1);
if (cards[type].length === 0) {
cardCount[type][0] = 0;
createCards(); // this is the problem code line
}
}
好的家伙我需要一点点调整。有2张牌,我为他们保留了一个计数器。每当一个牌组被完全挑选时,我会重新创建问题所在的牌createCards
,所以我也会重新创建另一个牌组,因为createCards
创建了2个牌组chance
和chest
。如何在不为2层甲板创建不同对象的情况下调整我的代码?
答案 0 :(得分:0)
我认为卡片是全球性的,或者至少是“卡片”。#card;" var card"在更大范围的某个地方?
第一次:
createCards ()
来自pickCard:
createCards ([type])
按如下方式修改createCards:
function createCards (options) {
if (typeof options == "undefined") {options = ['chance', 'chest']; cards = {}}
if (options.indexOf('chance') != -1) {
cards.chance = {...}
}
if (options.indexOf('chest') != -1) {
cards.chest = {...}
}
}
PS:如果你在createCards函数中随机化了这个命令会使事情变得更有趣。