Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 垄断游戏-在拾取牌后重新创建牌组_Javascript_Jquery - Fatal编程技术网

Javascript 垄断游戏-在拾取牌后重新创建牌组

Javascript 垄断游戏-在拾取牌后重新创建牌组,javascript,jquery,Javascript,Jquery,首字母 cardCount = { chest: [0], chance: [0]}; 创建卡片 function createCards() { cards = { chance: [{ title: 'Advance to go', type: 'move', position: 40 }, { title: "Advance to London",

首字母

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
    }
}

好了,伙计们,我需要稍微调整一下。抽了两张牌,我给他们留了一个柜台。每当一个牌组被完全拾取时,我会重新创建牌组
createCards
问题在于这样做时我也会重新创建另一个牌组,因为
createCards
创建了两个牌组
chance
cast
。我应该如何调整代码而不为这两个组创建不同的对象?

我假设cards是一个全局的,或者至少您在更大范围内的某个地方使用“var cards”

第一次:

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函数中随机化顺序,会使事情变得更有趣。

是的,cards是一个全局变量。当您从pickCard调用
createCards
时,通过
type
。到那里我和你在一起。然后在createCards中,您以if语句开始…您检查它是否未定义…如果未定义,您将做什么抱歉?此代码不会传递
类型
,而是传递数组中的
[type]
类型
。如果没有给出任何选项,它会将选项设置为
['chance','cost']
数组,将
卡设置为空对象(
{}
)。好吧,你完全失去了我。我会在早上查看你的代码,它肯定不会在现在下沉。谢谢。有什么特别的部分需要我详细说明吗?[link](我已经按照那里的建议重新构造了代码。我使用了一个随机函数。