本文主要是介绍js-实现一副牌的初始化,洗牌,发牌,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<script>var card = ['2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , 'J' , 'Q' , 'K' , 'A']; //13种var flower = ['方块' , '梅花' , '红桃' , '黑桃' ]; //花色var card_all = new Array(54); //初始一副牌 54张var n = 0; //牌的下标for( var i = 0 ; i < card.length ; i++) {for (var j = 0; j < flower.length; j++) {card_all[n] = flower[j] + card[i];n++;}}//添加大小王card_all[52] = '大王';card_all[53] = '小王';document.writeln("--------新牌如下:---------" + "<br />");for (var i = 0; i < card_all.length; i++) {document.writeln(card_all[i]);}//进行100次交换var temp ; //交换中间变量for( var i = 0 ; i < 100 ; i++){var num1 = Math.floor((Math.random()*54));var num2 = Math.floor((Math.random()*54)); //产生 0-53之间的整数temp = card_all[num1] ;card_all[num1] = card_all[num2];card_all[num2] = temp;} document.writeln('<br />' + "--------洗牌后如下:---------" + "<br />");for (var i = 0; i < 54; i++) {document.writeln(card_all[i]);}//给三个人,一人18张牌var card_one = new Array(18); var card_two = new Array(18); var card_three = new Array(18);//三个人的下标 var one = 0;var two = 0;var three = 0 ;/*第一个人取下标 0 3 6 ... (0 + x * 3)第二个人取下标 1 4 7 ... (1 + x * 3)第三个人取下标 2 5 8 ... (2 + x * 3)*/for (var i = 0; i < 54; i++) {if( i % 3 == 0){card_one[one] = card_all[i];one++;}else if( i % 3 == 1){card_two[two]=card_all[i];two++;}else{card_three[three]=card_all[i];three++;}}//下面就是输出三个人的牌了document.writeln('<br />' + "--------第一个人的牌:---------" + "<br />");for (var i = 0; i < 18; i++) {document.writeln(card_one[i]);}document.writeln('<br />' + "--------第二个人的牌:---------" + "<br />");for (var i = 0; i < 18; i++) {document.writeln(card_two[i]);}document.writeln('<br />' + "--------第三个人的牌:---------" + "<br />");for (var i = 0; i < 18; i++) {document.writeln(card_three[i]);}
</script>
这篇关于js-实现一副牌的初始化,洗牌,发牌的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!