标签:ons -- 表示 [] floor length rand lan dom
扑克
1.定义一个数组,存放1-52(分别表示52张扑克,不含大、小王)
其中1表示黑桃A,2表示黑桃2...,13表示 黑桃K,14表示红桃A......)
2.随机排列该数组
3.再将牌平均分给4个玩家(分成4个数组,按照发牌规则)
4.排序输出每个玩家的扑克(先按照花色:黑桃、红桃、梅花,方块;再按照大小)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>0406练习1</title> <script type="text/javascript"> var arr=[]; //1.在arr数组里存放1-52张牌 for(var i=1;i<=52;i++){ arr[arr.length]=i; } //2.洗牌 arr.sort(function(){ return Math.random()-0.5; }); //3.定义玩家数组 var players=[]; for(var i=0;i<4;i++){ players[i]=[]; } //4.发牌 for(var i=0;i<52;i++){ //0,4,8 //1,5,9 players[i%4].push(arr[i]); } //5.玩家排序 for(var i=0;i<4;i++){ players[i].sort(function(a,b){ return a-b; }); } // console.log(arr); // console.log(players); //6.输出 var types=["黑桃","红桃","梅花","方块"]; var nums=["A","2","3","4","5","6","7","8","9","10","J","Q","K"]; for(var i=0;i<4;i++){ console.log(‘玩家‘+(i+1)+"的牌:"); for(var j=0;j<13;j++){ var n=players[i][j]; var type=Math.floor((n-1)/13);//n-1对应下标0123 var sno=(n-1)%13;//n-1对应下标0-13 console.log(types[type]+nums[sno]); } console.log(‘==========‘); } </script> </head> <body> </body> </html>
标签:ons -- 表示 [] floor length rand lan dom
原文地址:http://www.cnblogs.com/xinxinxinxin/p/6683070.html