# 使用javascript随机生成斗地主玩家手牌

[1, "黑桃", 11, "K"]


//创建卡牌
function CreatePoker() {
let color = [[1, "黑桃",], [2, "红桃"], [3, "梅花"], [4, "方块"]];
let number = [[1, "3"], [2, "4"], [3, "5"], [4, "6"], [5, "7"], [6, "8"], [7, "9"], [8, "10"], [9, "J"], [10, "Q"], [11, "K"], [12, "A"], [13, "2"]];
//创建卡牌数组，每种颜色对应一种数值
let pokerList = [];
for (let i = 0; i !== color.length; i++) {
for (let j = 0; j !== number.length; j++) {
pokerList.push([...color[i], ...number[j]]);
}
}
//添加大小王
pokerList.push(["", "", 14, "小 王"]);
pokerList.push(["", "", 15, "大 王"]);
//将卡牌数组打乱，随机返回-1或者是1就可以乱排序
pokerList.sort(() => {
return Math.random() > 0.5 ? -1 : 1;
});
return pokerList;//最后将得到的卡牌数组返回
}
function GivePoker(pokerList) {
//直接从乱序数组中剔除得到17项，然后排序
let sortArr = SortPoker(pokerList.splice(0, 17));
//格式化输出
let str = "";
for (let poker of sortArr) {
str += poker[1] + poker[3] + "\t";
}
return str;
}
//排序数组
function SortPoker(pokerList) {
pokerList.sort((poker1, poker2) => {
//卡牌数值相同比较花色
if (poker1[2] === poker2[2]) {
return poker1[0] - poker2[0];
}
//卡牌数值不同比较数值
return poker1[2] - poker2[2];
});
return pokerList;
}
let pokerList = CreatePoker();
let player1Poker = GivePoker(pokerList);
let player2Poker = GivePoker(pokerList);
let player3Poker = GivePoker(pokerList);
let lastPoker = GivePoker(pokerList);
console.log("玩家1：" + player1Poker);
console.log("玩家2：" + player2Poker);
console.log("玩家3：" + player3Poker);
console.log("底 牌：" + lastPoker);