本文介绍了洗牌数组的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您的 new_deck 只包含索引,而不包含值,并且在选择下一个随机值之前,不会删除先前选择的值。 .pop()不带参数,因此您无法使用它删除特定索引 - 您必须使用 .splice()。总而言之,由于多种原因,您没有获得新的数值。 如果您只是想简单地随机化一个数组而不重新发明轮子并且知道随机性实际上运作良好,您可以使用这个: 函数fisherYates(myArray){ var i = myArray.length; if(i == 0)return false; (--i){ var j = Math.floor(Math.random()*(i + 1)); var tempi = myArray [i]; var tempj = myArray [j]; myArray [i] = tempj; myArray [j] = tempi; $ / code $ / pre 您可以在这里阅读Fisher-Yates方法: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle 。 您可以阅读为什么其他一些随机化方法不起作用,您可以在这里阅读: http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot .html 。 所以,您可以将其替换为: new_deck.push(randomindex) deck.pop(randomindex)} document.write(deck =) for(r = 0; r document.write(deck [r]); document.write(< / br>) document.write(new deck =) 代表(r = 0; r document.write(new_deck [r]); } 用这个: for(r = 0; r< deck.length; r ++){ document.write (甲板[R]); document.write(< / br>) document.write(shuffled deck =) //洗牌,然后输出 fisherYates(甲板); for(r = 0; r document.write(deck [r]); } <!doctype html><html lang="en"> <head> <meta charset="utf=8" /> <title>Blackjack</title> <link rel="stylesheet" href="blackjack.css" /> <script type="text/javascript"> var H2 = 2; var S2 = 2; var D2 = 2; var C2 = 2; var H3 = 3; var S3 = 3; var D3 = 3; var C3 = 3; var deck = new Array(H2, S2, D2, C2, H3, S3, D3, C3); var new_deck = new Array(); var r; document.write("deck = ") for (r =0; r<deck.length; r++){ document.write(deck[r]); } document.write("</br>") document.write("new deck = ") for (r=0; r<new_deck.length; r++){ document.write(new_deck[r]); } document.write("</br>") for (r=0;r<deck.length;r++){ var randomindex = Math.floor(Math.random()*deck.length); new_deck.push(randomindex) deck.pop(randomindex) } document.write("deck = ") for (r =0; r<deck.length; r++){ document.write(deck[r]); } document.write("</br>") document.write("new deck = ") for (r=0; r<new_deck.length; r++){ document.write(new_deck[r]); } document.write("</br>") </script> </head> <body> </body></html>Obviously this isn't the full Blackjack game here. It's just a test to see if shuffling the array works by printing the contents of both decks (arrays) before and after the shuffle.I'm only using 8 cards at the moment, 4 2's and 4 3's.What I am getting from this is:deck = 22223333new deck = deck = 2222new deck = 7502What I'm hoping to get is:deck = 22223333new deck = deck = new deck = 23232323 (or any of the 8 numbers, generated randomly)So it should be shuffling those 8 cards, what am I doing wrong? I'm only new to javascript but I've used some python before. I've done something similar in python and worked perfectly, but I'm not sure what's wrong here.Thanks for any answers in advance!! 解决方案 Your new_deck just contains indexes, not values and you aren't removing the previously chosen values before you choose the next random value. .pop() does not take an argument so you can't remove a specific index with it - you would have to use .splice() for that. All in all, you aren't getting a new array of your values for several reasons. If you just want an easy way to randomize an array without reinventing the wheel and knowing that the randomness actually works well you can use this:function fisherYates ( myArray ) { var i = myArray.length; if ( i == 0 ) return false; while ( --i ) { var j = Math.floor( Math.random() * ( i + 1 ) ); var tempi = myArray[i]; var tempj = myArray[j]; myArray[i] = tempj; myArray[j] = tempi; }}You can read about the Fisher-Yates method here:http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle.You can read about why some other randomization methods don't work as well you can read here: http://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html.So, you would replace this: for (r=0;r<deck.length;r++){ var randomindex = Math.floor(Math.random()*deck.length); new_deck.push(randomindex) deck.pop(randomindex) } document.write("deck = ") for (r =0; r<deck.length; r++){ document.write(deck[r]); } document.write("</br>") document.write("new deck = ") for (r=0; r<new_deck.length; r++){ document.write(new_deck[r]); }with this: document.write("deck = ") for (r =0; r<deck.length; r++){ document.write(deck[r]); } document.write("</br>") document.write("shuffled deck = ") // shuffle the deck and then output it fisherYates(deck); for (r=0; r<deck.length; r++){ document.write(deck[r]); } 这篇关于洗牌数组的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 03:47