原生js实现斗地主发牌
派牌
玩家一:
玩家三:
玩家二:
底牌:
var oBtn = document.getElementsByTagName("button")[0];
var oP = document.getElementsByTagName("p");
var str1 = ["红桃","方片","梅花","黑桃"];
var str2 = ["2","1","K","Q","J","10","9","8","7","6","5","4","3"];
oBtn.onclick = function () {
var str3 = [];
var str4 = [];
var str5 = [];
var player1 = [];
var player2 = [];
var player3 = [];
for (var i = 0 ; i <str1.length;i++){
for (var j = 0; j<str2.length;j++){
str3 += str1[i]+str2[j]+"/";
}
}
str3+="大王/"+"小王";
str3 = str3.split("/");
//抽三张底牌,同时在str3中删除这三张牌
for(var x = 0 ;x <=2;x++){
console.log(str3.length);
var n = parseInt(Math.random() * str3.length-1);
str4 += str3[n]+"/";
str3.splice(n, 1);
}
str4 = str4.slice(0,str4.length-1);
str4 = str4.split("/");
//用抽下标的方式打乱牌的顺序 str3--->str5
for(var y = 0; y <=50;y++){
var m = parseInt(Math.random() * str3.length-1);
str5 += str3[m]+"/";
str3.splice(m, 1)
}
str5 = str5.slice(0,str5.length-1);
str5 = str5.split("/");
//给三个玩家派牌
for(var a = 0 ;a <=16;a++){
player1 += str5[a]+"/";
}
for(var a = 17 ;a <=33;a++){
player2 += str5[a]+"/";
}
for(var a = 34 ;a <=50;a++){
player3 += str5[a]+"/";
}
player1 = player1.slice(0,player1.length-1);
player1 = player1.split("/");
player2 = player2.slice(0,player2.length-1);
player2 = player2.split("/");
player3 = player3.slice(0,player3.length-1);
player3 = player3.split("/");
oP[0].innerHTML = "玩家一:"+player1;
oP[1].innerHTML = "玩家二:"+player2;
oP[2].innerHTML = "玩家三:"+player3;
oP[3].innerHTML = "底牌:"+str4;
};