参考网上的斗地主发牌模拟,改了成桥牌模拟
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> </title>
<link href="./static/css/jquery-ui-1.11.4.css" rel="stylesheet" type="text/css">
<style type="text/css">
#head{
height:200px;
background-color:#0033CC;
margin:0;
}
#h1{
width:45%;
height:200px;
float:left;
margin:0;
}
#h2{
width:10%;
height:200px;
margin:0;
float:left;
background-color:#0033CC;
}
#h3{
width:45%;
height:200px;
float:right;
margin:0;
}
#content{
background:#CCCCCC;
height:200px;
margin:0;
}
#left{
width:40%;
height:200px;
background:#CCCC66;
float:left;
margin:0;
}
#left>p{
margin-left:200px;
}
#c{
width:20%;
height:200px;
background:grey;
margin:0;
float:left;
}
#right{
width:40%;
height:200px;
background:#339966;
float:right;
margin:0;
}
#right>p{
margin-left:230px;
}
#foot{
background:#0099CC;
height:200px;
margin:0;
}
#f1{
width:45%;
height:200px;
float:left;
margin:0;
}
#f2{
width:10%;
height:200px;
margin:0;
float:left;
}
#f3{
width:45%;
height:200px;
float:right;
margin:0;
}
#c>button{
width: 100px;
height: 100px;
background: red;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin-left:80px;
margin-top:50px;
font-size: 16px;
font-weight: bold;
}
</style>
<script type="text/javascript">
window.onload = function() {
var oBtn = document.getElementsByTagName("button");
var str1 = ["♣", "♦", "♥", "♠"];
var str2 = ["2","A","K","Q","J","10","9","8","7","6","5","4","3"];
oBtn[0].onclick = function () {
var str3 = [];
var str4 = [];
var str5 = [];
var player1 = [];
var player2 = [];
var player3 = [];
var player4 = [];
var p1s=[];
var p1h=[];
var p1d=[];
var p1c=[];
var p2s=[];
var p2h=[];
var p2d=[];
var p2c=[];
var p3s=[];
var p3h=[];
var p3d=[];
var p3c=[];
var p4s=[];
var p4h=[];
var p4d=[];
var p4c=[];
for (var i = 0 ; i <str1.length;i++){
for (var j = 0; j<str2.length;j++){
str3 += str1[i]+str2[j]+"/";
}
}
str3 = str3.slice(0,str3.length-1);
str3 = str3.split("/");
/*
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 <=51;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 <=12;a++){
player1 += str5[a]+"/";
}
for(var a = 13 ;a <=25;a++){
player2 += str5[a]+"/";
}
for(var a = 26 ;a <=38;a++){
player3 += str5[a]+"/";
}
for(var a = 39 ;a <=51;a++){
player4 += str5[a]+"/";
}
player1 = player1.slice(0,player1.length-1);
player1 = player1.split("/");
for(var a=0; a<=12;a++){
var tt = player1[a].slice(1,player1[a].length);
if(player1[a][0] == "♣")
{
p1c.push(tt);
}
if(player1[a][0] == "♦")
{
p1d.push(tt);
}
if(player1[a][0] == "♥")
{
p1h.push(tt);
}
if(player1[a][0] == "♠")
{
p1s.push(tt);
}
}
player2 = player2.slice(0,player2.length-1);
player2 = player2.split("/");
for(var a=0; a<=12;a++){
var tt = player2[a].slice(1,player2[a].length);
if(player2[a][0] == "♣")
{
p2c.push(tt);
}
if(player2[a][0] == "♦")
{
p2d.push(tt);
}
if(player2[a][0] == "♥")
{
p2h.push(tt);
}
if(player2[a][0] == "♠")
{
p2s.push(tt);
}
}
player3 = player3.slice(0,player3.length-1);
player3 = player3.split("/");
for(var a=0; a<=12;a++){
var tt = player3[a].slice(1,player3[a].length);
if(player3[a][0] == "♣")
{
p3c.push(tt);
}
if(player3[a][0] == "♦")
{
p3d.push(tt);
}
if(player3[a][0] == "♥")
{
p3h.push(tt);
}
if(player3[a][0] == "♠")
{
p3s.push(tt);
}
}
player4 = player4.slice(0,player4.length-1);
player4 = player4.split("/");
for(var a=0; a<=12;a++){
var tt = player4[a].slice(1,player4[a].length);
if(player4[a][0] == "♣")
{
p4c.push(tt);
}
if(player4[a][0] == "♦")
{
p4d.push(tt);
}
if(player4[a][0] == "♥")
{
p4h.push(tt);
}
if(player4[a][0] == "♠")
{
p4s.push(tt);
}
}
sort_local(p1c);
sort_local(p1d);
sort_local(p1h);
sort_local(p1s);
sort_local(p2c);
sort_local(p2d);
sort_local(p2h);
sort_local(p2s);
sort_local(p3c);
sort_local(p3d);
sort_local(p3h);
sort_local(p3s);
sort_local(p4c);
sort_local(p4d);
sort_local(p4h);
sort_local(p4s);
var oPN = document.getElementById("h2");
oPN.innerHTML = "<div id=\"N\" align=\"center\">N</div><p>♠ " + p1s +"</p><p><font color=\"red\">♥ " + p1h+"</font></p><p>♣ " + p1c + "</p><p><font color=\"red\">♦ "+p1d+"</font></p>";
var oPE = document.getElementById("left");
oPE.innerHTML = "<div id=\"E\" align=\"center\">E</div><p>♠ " + p2s +"</p><p><font color=\"red\">♥ " + p2h+"</font></p><p>♣ " + p2c + "</p><p><font color=\"red\">♦ "+p2d+"</font></p>";
var oPS = document.getElementById("f2");
oPS.innerHTML = "<div id=\"S\" align=\"center\">S</div><p>♠ " + p3s +"</p><p><font color=\"red\">♥ " + p3h+"</font></p><p>♣ " + p3c + "</p><p><font color=\"red\">♦ "+p3d+"</font></p>";
var oPW = document.getElementById("right");
oPW.innerHTML = "<div id=\"W\" align=\"center\">W</div><p>♠ " + p4s +"</p><p><font color=\"red\">♥ " + p4h+"</font></p><p>♣ " + p4c + "</p><p><font color=\"red\">♦ "+p4d+"</font></p>";
};
function sort_local(pl) {
for(var a=0; a<=12;a++){
if (pl[a] == "A")
{
pl[a] = "95";
}
if (pl[a] == "K")
{
pl[a] = "94";
}
if (pl[a] == "Q")
{
pl[a] = "93";
}
if (pl[a] == "J")
{
pl[a] = "92";
}
if (pl[a] == "10")
{
pl[a] = "91";
}
}
pl.sort(function(a,b){
return b - a;
})
for(var a=0; a<=12;a++){
if (pl[a] == "95")
{
pl[a] = "A";
}
if (pl[a] == "94")
{
pl[a] = "K";
}
if (pl[a] == "93")
{
pl[a] = "Q";
}
if (pl[a] == "92")
{
pl[a] = "J";
}
if (pl[a] == "91")
{
pl[a] = "10";
}
}
}
};
</script>
</head>
<body>
<div id="head">
<div id="h1"></div>
<div id="h2" align="left">
<div id="N" align="center">N</div>
<p>♠ A K Q 10 9</p>
<p><font color="red">♥ Q 9 7</font></p>
<p>♣ 7 8</p>
<p><font color="red">♦ A 10 9</font></p>
</div>
<div id="h3"></div>
</div>
<div id="content">
<div id="left">
<div id="W" align="center">W</div>
<p>♠ A K Q 10 9</p>
<p><font color="red">♥ Q 9 7</font></p>
<p>♣ 7 8</p>
<p><font color="red">♦ A 10 9</font></p>
</div>
<div id="c">
<button>派牌</button>
</div>
<div id="right">
<div id="E" align="center">E</div>
<p>♠ A K Q 10 9</p>
<p><font color="red">♥ Q 9 7</font></p>
<p>♣ 7 8</p>
<p><font color="red">♦ A 10 9</font></p>
</div>
</div>
<div id="foot">
<div id="f1"></div>
<div id="f2" align="left">
<div id="S" align="center">S</div>
<p>♠ A K Q 10 9</p>
<p><font color="red">♥ Q 9 7</font></p>
<p>♣ 7 8</p>
<p><font color="red">♦ A 10 9</font></p>
</div>
<div id="h3"></div>
</div>
</body>
</html>