javascript - How do I take data from one array, pick half at random without the same card being picked twice, then move them to another array? -
i'm trying make basic/simple war card game, , i'm working on javascript part. i'm stuck on how take half of card values @ random, , not picking same 1 on , over, move them array simulate shuffling (picking @ random) , moving them second array (simulating dealing more or less).
var deckofcards = new array('2d', '2h', '2s', '2c', '3d', '3h', '3s', '3c', '4d', '4h', '4s', '4c', '5d', '5h', '5s', '5c', '6d', '6h', '6s', '6c', '7d', '7h', '7s', '7c', '8d', '8h', '8s', '8c', '9d', '9h', '9s', '9c', '10d', '10h', '10s', '10c', '11d', '11h', '11s', '11c', '12d', '12h', '12s', '12c', '13d', '13h', '13s', '13c', '14d', '14h', '14s', '14c')
how write code want right now? also, please try keep answer code simple possible can understand lot more easily.
using shuffling method given here, try this
var deckofcards = new array('2d', '2h', '2s', '2c', '3d', '3h', '3s', '3c', '4d', '4h', '4s', '4c', '5d', '5h', '5s', '5c', '6d', '6h', '6s', '6c', '7d', '7h', '7s', '7c', '8d', '8h', '8s', '8c', '9d', '9h', '9s', '9c', '10d', '10h', '10s', '10c', '11d', '11h', '11s', '11c', '12d', '12h', '12s', '12c', '13d', '13h', '13s', '13c', '14d', '14h', '14s', '14c'); function shuffle(array) { var currentindex = array.length, temporaryvalue, randomindex; // while there remain elements shuffle... while (0 !== currentindex) { // pick remaining element... randomindex = math.floor(math.random() * currentindex); currentindex -= 1; // , swap current element. temporaryvalue = array[currentindex]; array[currentindex] = array[randomindex]; array[randomindex] = temporaryvalue; } return array; } var output = shuffle(deckofcards).slice(deckofcards.length/2); console.log(output);
Comments
Post a Comment