您的位置:程序门 -> web 开发 -> javascript



谁能帮我解释一下 这个扑克牌代码


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


谁能帮我解释一下 这个扑克牌代码[已结贴,结贴人:o__________________k]
发表于:2007-03-07 11:46:49 楼主
<script>
var   array1=new   array(55);

array.prototype.random=function()     {
      for(var   i=1;i <this.length;i++){
          var   rnd=math.floor(math.random()*(i+0.99999))
          this[i]=this[rnd];//不太明白
          this[rnd]=i;//不太明白
      }
return   this;
}

document.write(array1.random())
</script>
发表于:2007-03-07 12:53:541楼 得分:20
1。看为明白可以多看几遍
2。乱用prototype
3。上面代码洗出来的是55张牌,其中有一张白牌

附:

<html>

<head>
<meta   http-equiv= "content-type "   content= "text/html;   charset=gb2312 ">
<title> jk:支持民族工业,尽量少买x货 </title>
</head>

<body>
<pre>
洗牌算法:
部分代码来自:
http://community.csdn.net/expert/topic/3858/3858964.xml?temp=.5909693
fason的回贴。

</pre>

<script   language= "javascript ">
<!--

function   getrandomarray(m,n)       //得到m张牌里的n张牌   (m> =n> 0)
{
if(n==null   ¦ ¦   n==m   )
return   getrandomplaycard(m);
var   array1=new   array(m);
for(var   i=0;i <m;i++)
array1[i]=i;
var   m2=m-0.00001;
for(var   i=0;i <n;i++)
{
var   rnd=math.floor(math.random()*m2);
var   swap=array1[i];array1[i]=array1[rnd];array1[rnd]=swap;
}
return(array1.slice(0,n));
}


function   getrandomplaycard(m)   //洗牌
{
var   array1=new   array(m);
for(var   i=0;i <m;i++){
var   rnd=math.floor(math.random()*(i+0.99999))
array1[i]=array1[rnd];
array1[rnd]=i;
}
return   array1;
}


document.write(getrandomplaycard(100)+ " <br> <br> "+getrandomarray(100,5));

-->  
</script>

</body>
</html>


快速检索

最新资讯
热门点击