//洗牌函数 shuffle() 接收一个数组
let shuffle = function(pokers){
for(let i=0;i<pokers.length;i++)
{
let random = parseInt(Math.floor(Math.random()*54));//随机生成54张牌的一个随机数
//两张牌进行交换
let temp = pokers[i];
pokers[i] = pokers[random];
pokers[random] = temp;
}
}
//对玩家的牌进行排序 sort()函数 接收一个数组
let sort = function(pokers){
pokers.sort(function(a,b){
if(a.pointValue == b.pointValue)
{
return a.typeValue - b.typeValue;
}
else{
return a.pointValue - b.pointValue;
}
});
}
//打印扑克牌的方法 printCard() 接收一个数组
let printCard = function(pokers){
let str = "";//该字符串用于拼接扑克牌信息
for(let i=0;i<pokers.length;i++)
{
//判断是否为最后一张牌,决定了是否加逗号(,)
if(i == pokers.length-1)
{
if(pokers[i].type)
{
str += pokers[i].type + pokers[i].point;
}
else{
str += pokers[i].point
}
}
else{
if(pokers[i].type)
{
str += pokers[i].type + pokers[i].point + ',';
}
else{
str += pokers[i].point + ',';
}
}
}
return str;
}
//发牌函数 send() 接收一个数组
let send = function(pokers){
let player1 = pokers.slice(0,17);//玩家1的牌
let player2 = pokers.slice(17,34);//玩家2的牌
let player3 = pokers.slice(34,51);//玩家3的牌
let other = pokers.slice(51);//底牌
//接下来对每个玩家的扑克牌进行排序
sort(player1);
sort(player2);
sort(player3);
sort(other);
//将每个玩家的扑克牌打印出来
console.log("玩家1的牌为:",printCard(player1));
console.log("玩家2的牌为:",printCard(player2));
console.log("玩家3的牌为:",printCard(player3));
console.log("底牌为:",printCard(other));
}
//主函数
let main = function(){
//组合扑克牌对象
let points = ["3","4","5","6","7","8","9","10","J","Q","K","A","2"];//扑克牌的点数
let types = ["黑桃","红桃","梅花","方块"];//扑克牌的花色
let pokers = [];//声明一个空数组用于存放生成的扑克牌
for(let i=0;i<points.length;i++)
{
for(let j=0;j<types.length;j++)
{
//对象的属性依次为: 点数值 花色值 显示的点数 显示的花色
pokers.push({pointValue:i,typeValue:j,point:points[i],type:types[j]});
}
}
//从3-K 点数值依次为 0-10点 A 11 2 12 小王 13 大王 14
pokers.push({pointValue:13,point:"小王"});
pokers.push({pointValue:14,point:"大王"});
shuffle(pokers);//洗牌函数,将pokers数组传入进行洗牌
send(pokers);//发牌函数,将pokers数组传入发牌函数
}
main();