Javascript 垄断拾取随机卡和pop阵列

Javascript 垄断拾取随机卡和pop阵列,javascript,jquery,Javascript,Jquery,单击 function click() { createCards(); pickCard(); } 挑选卡片 function pickCard() { var x = Math.floor(Math.random() * ((15 - 0) + 1) + 0); var title = cards.chance[x].title; console.log(x + ". " + title);

单击

function click() {

        createCards();
        pickCard();

    }
挑选卡片

function pickCard() {
        var x = Math.floor(Math.random() * ((15 - 0) + 1) + 0);
        var title = cards.chance[x].title;
        console.log(x + ". " + title);
        //pop the array we just picked
        //adjust Math.floor since there are only 15 cards to pick from instead of  16

    }
创建卡片

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"
    }]
};
}


好了,伙计们,我试着选择一张随机卡,然后我想弹出它,但是因为我使用随机生成器,我必须调整最小值,最大值,因为数组中会少一张卡。我也会接受一个更好的答案,一个更有效的方法。比如洗牌?我不知道这是怎么回事。

你可以使用
拼接
弹出元素并使用
数组。长度
而不是使用固定的数字

   // if there are no more cards, create them

   var cardsLeft = cards.chance.length;
   if(cardsLeft == 0){
     createCards();
   }

    // Math  
    var x = Math.floor(Math.random() * cardsLeft);

    // pop
    cards.chance.splice(x, 1);  

首先用索引填充数组:

var i, indexes, pickedIndex;

indexes = [];
for (i = 0; i < 15; i++) // 16 cards from 0 to 15
{
  indexes[i] = i;
}
然后,通过覆盖阵列中最后一个拾取的索引来删除消耗的索引:

i = indexes.pop();

if (pickedIndex <= indexes.length)
{
    indexes[pickedIndex] = i;
}
i=index.pop();

如果(pickedIndex我会在开始时“洗牌”:

function shuffle(cards) {

    var shuffled = [],
        i = cards.length,
        j = 0;

    while (i--) {
        j = Math.floor(Math.random() * (i+1));
        shuffled.push(cards[j]);
        cards.splice(j,1);    
    }

    return shuffled;

}

var shuffledCards = shuffle(cards.chance);
shuffledCards
阵列现在将以随机顺序保存所有卡。然后,您可以在使用时从
shuffledCards
中弹出卡,或者使用计数器来处理卡。后者意味着您可以稍后重新洗牌


注意:这只是对我在这里写的内容的修改:

这个答案使用索引方法对机会卡进行随机排序,然后从0到15依次排列

<!DOCTYPE HTML>
<html>
<head>
<title>card shuffle</title>

<script type="text/javascript">
function set() {
        pack = new createCards();
        pack.shuffle();

    }

function click() {
    pack.pickCard();
}    

function pickCard() {
        var title = this.cards.chance[this.pick++].title;
        console.log((this.pick-1) + ". " + title);

    } 

function shuffle() {
    for (var i=0; i<16; i++) {
        this.cards.chance[i].place=Math.random();
    }
    //sort chance cards on place order which is now random 
    this.cards.chance.sort(function(a,b) {return a.place - b.place;});      
}

function createCards() {
this.cards = {

    chance: [{
        place:0,
        title: 'Advance to go',
        type: 'move',
        position: 40
    }, {
        place:0,
        title: "Advance to London",
        type: "move",
        position: 39
    }, {
        place:0,
        title: "Your ass is going to jail",
        type: "move",
        position: 10
    }, {
        place:0,
        title: "Advance to Rome",
        type: "move",
        position: 24
    }, {
        place:0,
        title: "Advance to Charles de Gaulle",
        type: "move",
        position: 15
    }, {
        place:0,
        title: "Advance to Amsterdam",
        type: "move",
        position: 11
    }, {
        place:0,
        title: "Go back 3 spaces",
        type: "movex",
        position: -3
    }, {
        place:0,
        title: "No drink and driving mate1",
        type: "bill",
        bill: 20
    }, {
        place:0,
        title: "Get out of Jail free card",
        type: "bill",
        bill: 150
    }, {
        place:0,
        title: "Pay school fees",
        type: "bill",
        bill: 150
    }, {
        place:0,
        title: "Speeding fine",
        type: "bill",
        bill: 150
    }, {
        place:0,
        title: "Bank pays you dividend",
        type: "bonus",
        bonus: 40
    }, {
        place:0,
        title: "You have won the competition",
        type: "bonus",
        bonus: 200
    }, {
        place:0,
        title: "Your building loan matures",
        type: "bonus",
        bonus: 200
    }, {
        place:0,
        title: "You are assessed for street repairs $40 per house $115 per hotel",
        type: "billx"
    }, {
        place:0,
        title: "House repairs $25 per house $100 per hotel",
        type: "billx"
    }]
}    
this.pick=0;    

this.pickCard=pickCard;
this.shuffle=shuffle;
};

set();
for(var i=0;i<16;i++) {
    click();
}

</script>
</head>
<body>  
</body>
</html>

洗牌
函数集(){
pack=newcreatecards();
pack.shuffle();
}
函数单击(){
pack.pickCard();
}    
函数pickCard(){
var title=this.cards.chance[this.pick++].title;
console.log((this.pick-1)+“+”标题);
} 
函数shuffle(){

对于(var i=0;如果对我来说,到目前为止,这个答案似乎是最有效的。一旦所有卡片都被挑选出来,我只需要再次
创建卡片
。只需要查看其他答案就可以做出决定。添加了一个额外的控件,在挑选一张之前检查是否还有剩余的卡片。我只需要再做一点调整。在垄断中游戏中有两种类型的牌要抽。机会牌和胸卡。我想重复使用你的答案,而不是创建另一个函数。那么我如何实现像
牌。[机会牌或胸卡]。title
?我可以将卡类型作为参数传递。你知道Javascript对象的数据结构是如何工作的吗?var cards={chance:[],chest:[]}不完全是。我边学边学。但我在这里问了最后一个问题
<!DOCTYPE HTML>
<html>
<head>
<title>card shuffle</title>

<script type="text/javascript">
function set() {
        pack = new createCards();
        pack.shuffle();

    }

function click() {
    pack.pickCard();
}    

function pickCard() {
        var title = this.cards.chance[this.pick++].title;
        console.log((this.pick-1) + ". " + title);

    } 

function shuffle() {
    for (var i=0; i<16; i++) {
        this.cards.chance[i].place=Math.random();
    }
    //sort chance cards on place order which is now random 
    this.cards.chance.sort(function(a,b) {return a.place - b.place;});      
}

function createCards() {
this.cards = {

    chance: [{
        place:0,
        title: 'Advance to go',
        type: 'move',
        position: 40
    }, {
        place:0,
        title: "Advance to London",
        type: "move",
        position: 39
    }, {
        place:0,
        title: "Your ass is going to jail",
        type: "move",
        position: 10
    }, {
        place:0,
        title: "Advance to Rome",
        type: "move",
        position: 24
    }, {
        place:0,
        title: "Advance to Charles de Gaulle",
        type: "move",
        position: 15
    }, {
        place:0,
        title: "Advance to Amsterdam",
        type: "move",
        position: 11
    }, {
        place:0,
        title: "Go back 3 spaces",
        type: "movex",
        position: -3
    }, {
        place:0,
        title: "No drink and driving mate1",
        type: "bill",
        bill: 20
    }, {
        place:0,
        title: "Get out of Jail free card",
        type: "bill",
        bill: 150
    }, {
        place:0,
        title: "Pay school fees",
        type: "bill",
        bill: 150
    }, {
        place:0,
        title: "Speeding fine",
        type: "bill",
        bill: 150
    }, {
        place:0,
        title: "Bank pays you dividend",
        type: "bonus",
        bonus: 40
    }, {
        place:0,
        title: "You have won the competition",
        type: "bonus",
        bonus: 200
    }, {
        place:0,
        title: "Your building loan matures",
        type: "bonus",
        bonus: 200
    }, {
        place:0,
        title: "You are assessed for street repairs $40 per house $115 per hotel",
        type: "billx"
    }, {
        place:0,
        title: "House repairs $25 per house $100 per hotel",
        type: "billx"
    }]
}    
this.pick=0;    

this.pickCard=pickCard;
this.shuffle=shuffle;
};

set();
for(var i=0;i<16;i++) {
    click();
}

</script>
</head>
<body>  
</body>
</html>