<canvas id=can width=800 height=800></canvas>
<div id=ps1>1</div>
<div id=ps2>2</div>
<div id=ps3>3</div>
<script>////alert("1");
  bRand=function(a,b){
    return Math.floor(a+Math.random()*(b-a+1));
  }
  //*i1=0;i2=0;i3=0;for(i=0;i<10000;i++){r=bRand(1,3);if(r==1)i1++;if(r==2)i2++;if(r==3)i3++;if(r<1||r>3)alert("its err");}alert("1:"+i1+",2:"+i2+",3:"+i3);
  //*a与b的概率一样。
  sort1=function(a,b){
    return b.power-a.power;
  }
  sort2=function(a,b){
    return a-b;
  }
  //*test=[4,2,7,4,9,10];test.sort(sort2);alert("test"+test);
  //*返回从小到大
clong=function(array){
_array=[];
    for(i=0;i<array.length;i++){
      _array.push(array[i]);
    }
return _array;
}
isStraight=function(nums){
  for(var i=1;i<nums.length;i++)if(nums[i]-nums[i-1]!=1)return false;
  return true;
}
//alert(isStraight([4,5,6,7,9]));
</script>
<script>////alert("2");
can=document.getElementById("can");
ps1=document.getElementById("ps1");
ps2=document.getElementById("ps2");
ps3=document.getElementById("ps3");
cxt=can.getContext("2d");
var POKER=[];
var btns=[];
var LORDIDX=0;
var PLAYIDX=0;
var dealnum=0;
var score=0;
var LASTTYPE=null;
var LASTGIVER=PLAYIDX; var dealPanel;
 var panel1;
 var panel2;
 var panel3;
 var able;
 var BTNS;
</script>
<script>////alert("3");
Poker=function(num,strnum){
this.num=num;this.strnum=strnum;
this.str1=String(num);
if(num==1)this.str1="A";
if(num==11)this.str1="J";
if(num==12)this.str1="Q";
if(num==13)this.str1="K";
if(num==16||num==17)this.str1="S";
this.str2;
this.color;
this.power=num;
if(num<3)this.power=num+13;
if(strnum==1){this.str2="黑桃";this.color="black";}
if(strnum==2){this.str2="红桃";this.color="red";}
if(strnum==3){this.str2="梅花";this.color="black";}
if(strnum==4){this.str2="方片";this.color="red";}
if(strnum==16){this.str2="小王";this.color="black";}
if(strnum==17){this.str2="大王";this.color="red";}this.x=0;this.y=0;
this.width=100;this.height=150;
this.isBack=false;this.isSelect=false;
this.drawFornt=function(cxt){
  cxt.fillStyle="#f9f9f9";
  cxt.fillRect(this.x,this.y,this.width,this.height);
  cxt.fillStyle=this.color;
  cxt.font="20px Arial";
  cxt.fillText(this.str1,this.x+4,this.y+20);
  cxt.fillText(this.str2.charAt(0),this.x+1,this.y+43);  
  cxt.fillText(this.str2.charAt(1),this.x+1,this.y+63);
  }
  this.drawBack=function(cxt){
    cxt.fillStyle="#a9a9f9";
  cxt.fillRect(this.x,this.y,this.width,this.height);
    cxt.strokeStyle="red";
  cxt.strokeRect(this.x+20,this.y+20,this.width-40,this.height-40);
  }
  this.draw=function(cxt){
    if(this.isBack){this.drawBack(cxt);}
    else{this.drawFornt(cxt);}
  }
  
  this.onclick=function(ie){
    if(ie.x>this.x&&ie.x<this.x+20&&ie.y>this.y&&ie.y<this.y+this.height){
this.isSelect=!this.isSelect;}
  }
return this;
}Div=function(x,y,w,h){
  this.x=x;this.y=y;
  this.width=w;this.height=h;
  this.childs=[];
  this.draw=null;
  this.isBack=false;
  this.onclick=function(ie){
    if(ie.x>this.x&&ie.x<this.x+this.width&&ie.y>this.y&&ie.y<this.y+this.height){
   
      for(i=0;i<this.childs.length;i++){
        this.childs[i].onclick(ie);
      }
    }
  }
}
Btn=function(str,fuc){
  this.str=str;
  this.fuc=fuc;0
  this.data=0;
  this.isBack;
  this.x=0;this.y=0;
  this.width=100;this.height=80;
  this.draw=function(cxt){
    cxt.fillStyle="#a9f9f9";
 cxt.fillRect(this.x,this.y,this.width,this.height);
 cxt.font="20px Arial";
 cxt.fillStyle="#ffaa33";
 cxt.fillText(this.str,this.x+(this.width-this.str.length*15)/2,this.y+(this.height-20)/2+20);
  }
  this.onclick=function(ie){
   if(ie.x>this.x&&ie.x<this.x+this.width&&ie.y>this.y&&ie.y<this.y+this.height)this.fuc();
  }
}</script>
<script>////alert("4");
initPoker=function(){
  POKER[0]=[];POKER[1]=[];
  POKER[2]=[];POKER[3]=[];
  POKER[4]=[];
  POKER[0][0]=new Poker(17,17);
  POKER[0][1]=new Poker(16,16); 
  for(i=0;i<13;i++){
    for(j=0;j<4;j++){
     POKER[0][i*4+j+2]=new Poker(i+1,j+1);
    }
  }
  
}
initPanel=function(){
  dealPanel=new Div(100,0,600,200);
  panel1=new Div(0,100,150,500);
  panel2=new Div(0,600,800,200);
  panel3=new Div(650,100,150,500);
  able=new Div(200,350,400,200);
  BTNS=new Div(100,500,600,100);
  Hdraw=function(cxt){
    if(this.childs){
     for(i=0;i<this.childs.length;i++){
        this.childs[i].x=(this.width-(15*this.childs.length+100))/2+this.x+i*15;
        this.childs[i].y=(this.height-150)/2+this.y;
this.childs[i].isBack=this.isBack;
        this.childs[i].draw(cxt);
     }
    }
   }
  Vdraw=function(cxt){
    if(this.childs){
     for(i=0;i<this.childs.length;i++){
         this.childs[i].x=(this.width-100)/2+this.x; this.childs[i].y=(this.height-15*this.childs.length-150)/2+this.y+i*15;
        this.childs[i].isBack=this.isBack;
this.childs[i].draw(cxt);
     }
    }
   }
  mdraw=function(cxt){
     if(this.childs){
     for(i=0;i<this.childs.length;i++){
         this.childs[i].x=(this.width-(30*this.childs.length+100))/2+this.x+i*30;
        this.childs[i].y=(this.height-150)/2+this.y;
this.childs[i].isBack=this.isBack;
        if(this.childs[i].isSelect){
          this.childs[i].y-=30;
        }
        this.childs[i].draw(cxt);
     }
     }
  }
  BTNSdraw=function(cxt){
     if(this.childs){
     for(i=0;i<this.childs.length;i++){
tt=(this.width-(this.childs.length*100))/(this.childs.length+1);
        this.childs[i].x=tt*(1+i)+i*100+this.x;
        this.childs[i].y=(this.height-150)/2+this.y;
        this.childs[i].draw(cxt);
     }
    }
  }
  dealPanel.draw=Hdraw;
  panel1.draw=Vdraw;
  panel2.draw=mdraw;
  panel3.draw=Vdraw;
  able.draw=Hdraw;
  BTNS.draw=BTNSdraw;
dealPanel.isBack=true;
panel1.isBack=true;
panel3.isBack=true;}
  drawOther=function(cxt){
    //**score
    cxt.fillStyle="red";
    cxt.fillText("score:"+score,700,50);
    var lx,ly,px,py;
    if(LORDIDX==1){lx=100;ly=100;}
    else if(LORDIDX==2){lx=150;ly=550;}
    else if(LORDIDX==3){lx=650;ly=100;}
    cxt.fillText("地主",lx,ly);    if(PLAYIDX==1){px=100;py=120;}
    else if(PLAYIDX==2){px=150;py=570;}
    else if(PLAYIDX==3){px=650;py=120;}
    cxt.fillText("操作",px,py);    cxt.fillText("上次出牌者"+LASTGIVER,200,350);
}
  drawAll=function(cxt){
    cxt.clearRect(0,0,can.width,can.height);
    dealPanel.childs=POKER[0];
    panel1.childs=POKER[1];
    panel2.childs=POKER[2];
    panel3.childs=POKER[3];
    able.childs=POKER[4];
    BTNS.childs=btns;
   dealPanel.draw(cxt);
   panel1.draw(cxt);
   panel2.draw(cxt);
   panel3.draw(cxt);
   able.draw(cxt);
   BTNS.draw(cxt);
    drawOther(cxt);
  }
</script>
<script>////alert("5");
  start=function(){
    initPoker();
    initPanel();
    dealPanel.isBack=true;
    btns[0]=new Btn("开始",function(){btns=[];
PLAYIDX=bRand(1,3);dealing();});
    can.onclick=function(e){BTNS.onclick(e);}
    drawAll(cxt);
}
  dealing=function(){
    if(dealnum>=51){getlord();}
    else{
    if(PLAYIDX>3)PLAYIDX=1;
      r=bRand(0,POKER[0].length-1);
      POKER[PLAYIDX].push(POKER[0][r]);
      POKER[0].splice(r,1);
      POKER[PLAYIDX].sort(sort1);
      drawAll(cxt);
    PLAYIDX++;dealnum++;
      setTimeout(dealing,30);
    }
}
  getlord=function(){
btns=[];
    if(score>=3){
       LORDIDX=PLAYIDX-1;
       PLAYIDX=LORDIDX;
       POKER[LORDIDX].push(new Poker(POKER[0][0].num,POKER[0][0].strnum));
       POKER[LORDIDX].push(new Poker(POKER[0][1].num,POKER[0][1].strnum));      
       POKER[LORDIDX].push(new Poker(POKER[0][2].num,POKER[0][2].strnum));
       POKER[LORDIDX].sort(sort1);
   dealPanel.isBack=false;
       playing();
     }
     else{drawAll(cxt);
       if(PLAYIDX>3)PLAYIDX=1;
       if(PLAYIDX==2){
         for(i=0;i<3-score;i++){
         btns[i]=new Btn((score+i+1)+"分",function(){score=this.data;PLAYIDX++;getlord();});
         btns[i].data=score+i+1;
         }
         btns[i]=new Btn("不叫",function(){PLAYIDX++;getlord();});
         drawAll(cxt);
        can.onclick=function(e){BTNS.onclick(e);}
       }
       else{
          score=bRand(score,3);
          PLAYIDX++;i
          setTimeout(getlord,1500);
       }
     }    }
  playing=function(){    btns=[];drawAll(cxt);
    if(POKER[1].length==0||POKER[2].length==0||POKER[3].length==0){
    gameover();
    }
    else{
       if(PLAYIDX>3)PLAYIDX=1;
   if(LASTGIVER==PLAYIDX)LASTTYPE=null;
       if(PLAYIDX==2){
         btns[0]=new Btn("出牌",function(){
    var testnum=[];
    for(i=0;i<POKER[PLAYIDX].length;i++){
      if(POKER[PLAYIDX][i].isSelect){
        testnum.push(POKER[PLAYIDX][i].power);
      }
    }
    testnum.sort(sort2);
     
    if(testnum.length>0&&checkPoker(testnum)){ givePoker();
for(var i=POKER[PLAYIDX].length-1;i>=0;i--){
       if(POKER[PLAYIDX][i].isSelect)POKER[PLAYIDX][i].isSelect=false;
      }
 PLAYIDX++;playing();}
else{alert("出牌不能");playing();}
});
         btns[1]=new Btn("不出",function(){
if(LASTTYPE!=null){PLAYIDX++;setTimeout(playing,500);}else{alert("首轮出牌不能跳过");}
});
         btns[2]=new Btn("提示",function(){
aiSelect()||alert("提示不能");
});
         can.onclick=function(e){
            panel2.onclick(e);
            BTNS.onclick(e);
            drawAll(cxt);
         }
         drawAll(cxt);
       }
       else{
         aiSelect()&&givePoker();
         drawAll(cxt);
         PLAYIDX++;setTimeout(playing,1500);
       }
    }
}
  gameover=function(){
  panel1.isBack=false;
panel3.isBack=false;
drawAll(cxt);
alert("gameover");
}</script>

解决方案 »

  1.   

    <script>////alert("6");  Types={//扑克牌类型
            "1":{weight:1,allNum:1,minL:5,maxL:12}
            ,"11":{weight:1,allNum:2,minL:3,maxL:10}
            ,"111":{weight:1,allNum:3,minL:1,maxL:6}
            ,"1111":{weight:2,allNum:4,minL:1,maxL:1}
            ,"1112":{weight:1,zcy:"111",fcy:"1",fcyNum:1,allNum:4,minL:1,maxL:5}
            ,"11122":{weight:1,zcy:"111",fcy:"11",fcyNum:1,allNum:5,minL:1,maxL:4}
            ,"111123":{weight:1,zcy:"1111",fcy:"1",fcyNum:2,allNum:6,minL:1,maxL:1}
            ,"11112233":{weight:1,zcy:"1111",fcy:"11",fcyNum:2,allNum:8,minL:1,maxL:1}
            ,"12":{weight:3,allNum:2,minL:1,maxL:1}
        }  splitPoker=function(num,gess){
        var _num=clong(num);
        if(gess==null)gess=3;
        var type={"1":[],"12":[],"11":[],"111":[],"1111":[]};    if(_num[_num.length-1]==17&&_num[_num.length-2]==16){type["12"].push(17);_num.splice(_num.length-2,2);}
       for(i=gess;i>=0;i--){
         str="1";
         for(j=0;j<i;j++)str+="1";
         for(k=_num.length-1;k>=i;k--){
           if(_num[k]==_num[k-i]){
             type[str].push(_num[k]);
             _num.splice(k-i,i+1);
           }
         }
       }
     type["1"].sort(sort2);
     type["11"].sort(sort2);
     type["111"].sort(sort2);
     type["1111"].sort(sort2);
       return type;
      
      //sp="";for(i in type)sp+=","+i+":"+type[i];document.write(_num+"<br>");document.write("=>"+sp);
      }  getTypeByPoker=function(num,gess){
       num.sort(sort2);
       var _num=clong(num);
       var  pokertype={type:null,power:0,length:_num.length}
         splitpoker=splitPoker(_num,gess);
         if(gess==null)gess=3;
         if(splitpoker["12"].length>0){
           if(pokertype.length==2){pokertype.type="12";}
           else pokertype.type=null;
         }
         else if(splitpoker["1111"].length>0){
           if(splitpoker["1111"].length==1){
             pokertype.power=splitpoker["1111"][0];
             if(pokertype.length==4)pokertype.type="1111";
             else if(pokertype.length==6)pokertype.type="111123";
             else if(pokertype.length==8&&splitpoker["11"].length==2)pokertype.type="11112233";
             else pokertype.type=null;
           }
           else pokertype.type=null;
         }
         else if(splitpoker["111"].length>0){
           var l=splitpoker["111"].length;
           if(l==1||isStraight(splitpoker["111"])){
         pokertype.power=splitpoker["111"][0]; 
             if(pokertype.length==3*l)pokertype.type="111";
             else if(pokertype.length==4*l)pokertype.type="1112";
             else if(pokertype.length==5*l&&splitpoker["11"].length==l)pokertype.type="11123";         else pokertype.type=null;
           }       else pokertype.type=null;
         }
         else if(splitpoker["11"].length>0){
            var l=splitpoker["11"].length;
            if(l==1||(l>=3&&isStraight(splitpoker["11"]))){
              pokertype.power=splitpoker["11"][0]; if(pokertype.length==l*2)pokertype.type="11";         else pokertype.type=null;
            }else pokertype.type=null;
         }
         else if(splitpoker["1"].length>0){
            var l=splitpoker["1"].length;
    //alert("1类型牌数组为"+splitpoker["1"]+isStraight(splitpoker["1"]))
            if(l==1||(l>=5&&isStraight(splitpoker["1"]))){
              pokertype.power=splitpoker["1"][0];
              pokertype.type="1";
            }else pokertype.type=null;
         }else pokertype.type=null;
     if(pokertype.type==null&&gess>0){
    //alert("gess"+gess);
    pokertype=getTypeByPoker(_num,gess-1);}
         return pokertype;
      }  checkPoker=function(num){
        var _num=clong(num);
        var pokertype=getTypeByPoker(_num);
    //alert("扑克类型为"+pokertype.type+",扑克大小为"+pokertype.power+",扑克数量为"+pokertype.length);
         if(pokertype.type==null)return false;
         else if(LASTTYPE==null)return true;
         else if(Types[pokertype.type].weight>Types[LASTTYPE.type].weight)return true;
           else if(Types[pokertype.type].weight==Types[LASTTYPE.type].weight&&pokertype.type==LASTTYPE.type&&pokertype.length==LASTTYPE.length&&pokertype.power>LASTTYPE.power){alert("条件三");return true;}
         else return false;
      }  givePoker=function(){
          POKER[4]=[];
          var testnum=[];
          for(var i=POKER[PLAYIDX].length-1;i>=0;i--){
          if(POKER[PLAYIDX][i].isSelect){
            POKER[4].push(POKER[PLAYIDX][i]);
            testnum.push(POKER[PLAYIDX][i].power);
            POKER[PLAYIDX].splice(i,1);
          }
        }
    if(LASTTYPE!=null)ps1.innerHTML="last.type:"+LASTTYPE.type+",power:"+LASTTYPE.power+",length:"+LASTTYPE.length;  LASTTYPE=getTypeByPoker(testnum); 
      LASTGIVER=PLAYIDX;
          ps2.innerHTML="this.  type:"+LASTTYPE.type+",power:"+LASTTYPE.power+",length:"+LASTTYPE.length;
      }
    </script><script>////alert("7");
    //返回大于type.power的type.type类型
    getPokerByType=function(num,type){    var _num=[];
        var SPN=[];
        if(num.length>=type.length){
            _num=clong(num);//_num从小到大
            if(type.type=="12"){//王炸 
    if(_num[_num.length-1]==17&&_num[_num.length-2]==16){
                    SPN.splice(0,0,17);
                    SPN.splice(0,0,16);
                }
            }
             else if(type.type=="1"||type.type=="11"||type.type=="111"||type.type=="1111"){//非组合类型
                var c=Types[type.type].allNum-1;
                for(var j=c;j<_num.length;j++){//从小到大取数值
      while(j<_num.length&&_num[j]>type.power&&_num[j]==_num[j-c]){
                        if(SPN.length>0){
                            if(_num[j]==SPN[0])break;
                            else if(_num[j]>SPN[0]+1)SPN=[];//如果不能连续则清空已选数
                        }
                        for(var k=j;k>=j-c;k--) {
                            SPN.splice(0,0,_num[k]);//选取数值
                            _num.splice(j,1);//删除数值
                        }
                        if(SPN.length==type.length)break;//选取完成
                    }
                    if(SPN.length==type.length)break;
                }
            }
            else if(type.type=="1112"||type.type=="11122"||type.type=="111123"||type.type=="11112233"){//组合类型
                var zcy=Types[type.type].zcy;
                var fcy=Types[type.type].fcy;
                var fcyNum=Types[type.type].fcyNum;
                var l=type.length/Types[type.type].allNum;
                SPN=getPokerByType(_num,{type:zcy,power:type.power,length:l*Types[zcy].allNum});//先选主类型
                if(SPN.length>0){
                    for(var i=0;i<SPN.length;i++){
                        for(var j=0;j<_num.length;j++){
                            if(SPN[i]==_num[j]){
                                _num.splice(j,1);//删除已选数值
                                break;
                            }
                        }
                    }
                    while(fcyNum>0){
                        var spn1=getPokerByType(_num,{type:fcy,power:0,length:Types[fcy].allNum});
                        for(var i=0;i<spn1.length;i++) SPN[SPN.length]=spn1[i];
                        fcyNum--;
                    }
                }
            }
        }
        if(SPN.length!=type.length)SPN=[];//如果选取不成功,则清空选取数据    return SPN;
    }
    </script>
    <script>////alert("8");
    aiSelect=function(){
        var num=[];
        for(var i=POKER[PLAYIDX].length-1;i>=0;i--)num[num.length]=POKER[PLAYIDX][i].power;
        var SPN=[];
        var str="aiSelect提示:<br>";
        if(checkPoker(num)){
    str+="一次出完确定;<br>";
    for(var i=POKER[PLAYIDX].length-1;i>=0;i--)SPN[SPN.length]=POKER[PLAYIDX][i].power;
    }
        else{str+="不能一次出完确定;<br>";
          if(LASTGIVER==0||LASTGIVER==PLAYIDX)
    {str+="首轮出牌确定;<br>";
            splitpoker=splitPoker(num);
            //①有3条情况
            if(splitpoker["111"].length>0){str+="有3条情况确定;<br>";
              if(splitpoker["111"][0]<7||(splitpoker["11"].length==0&&splitpoker["1"].length==0)){
               
           if(splitpoker["11"].length>0&&(splitpoker["11"][0]<7||splitpoker["1"].length==0)){
     
    for(var i=Types["11122"].maxL;i>0;i--){
    SPN=getPokerByType(num,{type:"11122",power:0,length:5*i});
    if(SPN.length>0)break;}}
                if(SPN.length==0&&splitpoker["1"].length>0&&splitpoker["1"][0]<15){
     
    for(var i=Types["1112"].maxL;i>0;i--){
    SPN=getPokerByType(num,{type:"1112",power:0,length:4*i});
    if(SPN.length>0)break;}}
                if(SPN.length==0){
          //(splitpoker["11"].length==0&&splitpoker["1"].length==0)情况3
    for(var i=Types["111"].maxL;i>0;i++){SPN=getTypeByPoker(num,{type:"111",power:0,length:3*i});if(SPN.length>0)break;}}
              }
            }
            //②没3条情况
            if(SPN.length==0){str+="没有3条情况确定;<br>";
              var nn=[];
              for(var x=1;x<=3;x++){
                if(POKER[x].length==1||POKER[x].length==2){if(PLAYIDX==LORDIDX){if(PLAYIDX!=x)nn[POKER[x].length]=true;}else{if(LORDIDX==x)nn[POKER[x].length]=true;}}     
              }
             if((splitpoker["11"].length>0&&splitpoker["1"].length>0&&(splitpoker["11"][0]<splitpoker["1"][0]||nn[1]))||(splitpoker["11"].length>0&&splitpoker["1"].length==0)){
     
    for(var i=Types["11"].maxL;i>2;i--){
    SPN=getPokerByType(num,{type:"11",power:0,length:2*i});
    if(SPN.length>0)break;}
    if(SPN.length==0)SPN=getPokerByType(num,{type:"11",power:0,length:2});
    }
    else{         if(SPN.length==0&&splitpoker["1"].length>0)SPN[0]=splitpoker["1"][0];}
    //alert("11>0,情况2结束");          }
            if(SPN.length==0)SPN=getPokerByType(num,{type:"1111",power:0,length:4});//出炸弹
            if(SPN.length==0)SPN=getPokerByType(num,{type:"12",power:0,length:2});//出王炸
    }
    else{str+="不是首轮出牌确定;<br>";
     
              if(LASTTYPE.type!="12"){
                if(PLAYIDX==LORDIDX||LASTGIVER==LORDIDX){
    //alert("type:"+LASTTYPE.type+",power:"+LASTTYPE.power+",length:"+LASTTYPE.length);
    SPN=getPokerByType(num,LASTTYPE);
    //alert(SPN.length);
    if(SPN.length==0&&LASTTYPE.type!="1111")SPN=getPokerByType(num,{type:"1111",power:0,length:4});if(SPN.length==0)SPN=getPokerByType(num,{type:"12",power:0,length:2});}
              
                else{if(POKER[LASTGIVER].length>5){if((LASTTYPE.type=="1"&&LASTTYPE.length==1)||(LASTTYPE.type=="11"&&LASTTYPE.length==2)
    ){if(SPN.length>0&&SPN[0]>10)SPN=[];}}}
              }
    }
          }
     ps3.innerHTML=str;
        if(SPN.length>0){for(var i=0;i<SPN.length;i++){for(var j=POKER[PLAYIDX].length-1;j>=0;j--){
                    if(!POKER[PLAYIDX][j].isSelect&&POKER[PLAYIDX][j].power==SPN[i]){
                POKER[PLAYIDX][j].isSelect=true;
                        break;
                    }}}return true;
        }
        else return false;    
      }
    </script><script>
    start();
    </script>