斗地主源码(字符界面)--单机游戏(2)

     PlaySqueezer.java(剩余部分)
 public void computerPlay(SPlayers s,Squeezer[] sq){
        System.out.println(" ");
        System.out.println("玩家"+s.getpid()+"出牌");
        cpPlay(s,sq);
          this.displayCardFace(sq);//显示牌面
          if(s.getpid()==2)
          token=3;
          else token=1;
      }    
     //cpPlay()method
    public void cpPlay(SPlayers s,Squeezer[] sq){
        //分析手中的牌
        analyse(s);
        //电脑第一次出牌
        if(ring_token==s.getpid()){
        //出连子
        if(searchLianZi(tmp))
           playLianZi(s);
        //出单张
        else if(searchDanZhang(tmp))
        playDanZhang(s);    
        //出对子
        else if(searchDuiZi(tmp))
        playDuiZi(s);    
        //出三张
        else if(searchSanZhang(tmp))
        playSanZhang(s);
        //出单王
        else if(searchDanJoker(s))
        playDanJoker(s);
         //出炸弹
         else if(searchZhaDan(tmp)){
        playZhaDan(s);
        s.setscore(100);
        }
        //出双王
        else if(searchJoker(s)){
        playJoker(s);
        s.setscore(100);
        }
       }//电脑第一次出牌
        
        
   //电脑非第一次出牌
        
        else if(token==s.getpid()){
         //单张
         if(card_face.length==1){
               if(playDanZhang2(s)) {ring_token=s.getpid();}
                 else {
                   if(searchZhaDan(tmp))    //有炸弹出之
                     {playZhaDan(s);
                      s.setscore(100);
                      ring_token=s.getpid();
                      }
                    else if(searchJoker(s))//有双王出之
                      {playJoker(s);     s.setscore(100); ring_token=s.getpid();}
                      else  System.out.println("过......"); 
                 }
         }
        //对子
         else if(card_face.length==2&&(sq[card_face[0]].getsqpoint()).equals(sq[card_face[1]].getsqpoint())){
           if(playDuiZi2(s)){ring_token=s.getpid();}
             else {
                 if(searchZhaDan(tmp))    //有炸弹出之
                     {playZhaDan(s);     s.setscore(100); ring_token=s.getpid();}
                    else if(searchJoker(s))//有双王出之
                      {playJoker(s);     s.setscore(100); ring_token=s.getpid();}
                      else  System.out.println("过......"); 
                 }
         }
        //三张
         else if(card_face.length==3){
             if(playSanZhang2(s)){ring_token=s.getpid();}
               else{
                   if(searchZhaDan(tmp))    //有炸弹出之
                     {playZhaDan(s);     s.setscore(100); ring_token=s.getpid();}
                    else if(searchJoker(s))//有双王出之
                      {playJoker(s);     s.setscore(100); ring_token=s.getpid();}
                      else  System.out.println("过......"); 
                   
                   }
         }
        //三带一
         else if(card_face.length==4&&(sq[card_face[0]].getsqpoint()).equals(sq[card_face[3]].getsqpoint())==false){
             if(playSanDaiYi(s)){ring_token=s.getpid();}
               else{
                   if(searchZhaDan(tmp))    //有炸弹出之
                     {playZhaDan(s);     s.setscore(100);  ring_token=s.getpid();}
                    else if(searchJoker(s))//有双王出之
                      {playJoker(s);     s.setscore(100); ring_token=s.getpid();}
                      else  System.out.println("过......"); 
                   }
             
         }     
        //连子
         else if(card_face.length>=5&&(sq[card_face[0]].getsqpoint()).equals(sq[card_face[1]].getsqpoint())==false){
             if(playLianZi2(s)){ring_token=s.getpid();}
               else{
                    if(searchZhaDan(tmp))    //有炸弹出之
                     {playZhaDan(s);     s.setscore(100); ring_token=s.getpid();}
                    else if(searchJoker(s))//有双王出之
                      {playJoker(s);     s.setscore(100); ring_token=s.getpid();}
                      else  System.out.println("过......"); 
                   }
          
             
         }    
        //四带二
         else if(card_face.length==6&&(sq[card_face[0]].getsqpoint()).equals(sq[card_face[1]].getsqpoint())){
             if(playSiDai2(s)){ring_token=s.getpid();}
               else{
                   if(searchZhaDan(tmp))    //有炸弹出之
                     {playZhaDan(s);     s.setscore(100); ring_token=s.getpid();}
                    else if(searchJoker(s))//有双王出之
                      {playJoker(s);     s.setscore(100); ring_token=s.getpid();}
                      else  System.out.println("过......"); 
                   }
             
         }
        //炸弹
         else if(card_face.length==4&&(sq[card_face[0]].getsqpoint()).equals(sq[card_face[3]].getsqpoint())){
             if(playZhaDan2(s)){     s.setscore(100); ring_token=s.getpid();}
               else{
                   if(searchJoker(s))//有双王出之
                     { playJoker(s);     s.setscore(100);  ring_token=s.getpid();}
                    else  System.out.println("过......"); 
                   }
             
         }
         //双王
         else if(card_face.length==2&&(sq[card_face[0]].getsqpoint()).equals(sq[card_face[1]].getsqpoint())==false){
             System.out.println("过......"); 
             
         } 
         
        }
        
        
        
        
    }
    //确定i所在的组号--i是牌所在的序号
    public int judgeLocation(int i){
        if(i<4) return 0;
        else if(4<=i&&i<8) return 1;
        else if(8<=i&&i<12) return 2;
        else if(12<=i&&i<16) return 3;
        else if(16<=i&&i<20) return 4;
        else if(20<=i&&i<24) return 5;
        else if(24<=i&&i<28) return 6;
        else if(28<=i&&i<32) return 7;
        else if(32<=i&&i<36) return 8;
        else if(36<=i&&i<40) return 9;
        else if(40<=i&&i<44) return 10;
        else if(44<=i&&i<48) return 11;
        else if(48<=i&&i<52) return 12;
        else if(52<=i&&i<54) return 13;
        else return 100;
        
    }
    
    //playDanZhang2()包括单王
    public boolean playDanZhang2(SPlayers s){
      for(int i=judgeLocation(card_face[0])+1;i<13||searchDanJoker(s);i++){
        if(i<13){
          if(tmp[i][0]==1) {
              int[] a1=new int[card_face.length];
              a1[0]=tmp[i][1];
              setCardFaceArray(card_face.length,a1);
            //出牌后对玩家手中的牌进行相应清理
            s.clearCardIDarray(card_face[0]);
            //设置未出牌数
              s.setsqunout(card_face.length);    
               return true;
              }
         }
         else if(searchDanJoker(s)){
             if(card_face[0]!=53){
               playDanJoker(s);
             return true;
             }
             else return false;
             
         } 
       }
       return false;
    }
    
    //playDuiZi2(s)
    public boolean playDuiZi2(SPlayers s){
      for(int i=judgeLocation(card_face[0])+1;i<13;i++){
            if(tmp[i][0]==2){
                int[] a1=new int[card_face.length];
                int ans=tmp[i][1];
                for(int j=0;j<2;j++)
                  a1[j]=ans++;
                setCardFaceArray(card_face.length,a1); 
                //出牌后对玩家手中的牌进行相应清理
            s.clearCardIDarray(card_face[0]);
            s.clearCardIDarray(card_face[1]);
            //设置未出牌数
              s.setsqunout(card_face.length);    
               return true;
                }
          }
       return false;    
        
    }
    
    //playSanZhang2(s)
    public boolean playSanZhang2(SPlayers s){
       for(int i=judgeLocation(card_face[0])+1;i<13;i++){
            if(tmp[i][0]==3){
                int[] a1=new int[card_face.length];
                int ans=tmp[i][1];
                for(int j=0;j<3;j++)
                  a1[j]=ans++;
                setCardFaceArray(card_face.length,a1); 
                //出牌后对玩家手中的牌进行相应清理
            s.clearCardIDarray(card_face[0]);
            s.clearCardIDarray(card_face[1]);
            s.clearCardIDarray(card_face[2]);
            //设置未出牌数
              s.setsqunout(card_face.length);    
              return true;
                }
           }
      return false;         
     
     }
    
    //playSanDaiYi(s)--三带一,其中单张不包括王
    public boolean playSanDaiYi(SPlayers s){
       for(int i=judgeLocation(card_face[0])+1;i<13;i++){
         if(tmp[i][0]==3){
              int j=0;
              boolean bt=false;
             while(!bt){
                if(tmp[j][0]>=1&&j!=i){
                  int[] a1=new int[card_face.length];
                  int ans=tmp[i][1];    
                     for(int k=0;k<3;k++)
                     a1[k]=ans++;    
                     a1[3]=tmp[j][1];
                     setCardFaceArray(card_face.length,a1); 
                     //出牌后对玩家手中的牌进行相应清理
                s.clearCardIDarray(card_face[0]);
                s.clearCardIDarray(card_face[1]);
                s.clearCardIDarray(card_face[2]);
                s.clearCardIDarray(card_face[3]);
                //设置未出牌数
                  s.setsqunout(card_face.length);    
                bt=true;
                return true;
                }
               else {
                   j++;
                   if(j>12) {bt=true; return false;}//没有访问到王(待改进)
                   }       
               }//while    
           }//if
        }//for
     return false;     
    }
    
    //playSiDai2(s)--四带二只能带对子
    public boolean playSiDai2(SPlayers s){
       for(int i=judgeLocation(card_face[0])+1;i<13;i++){
            if(tmp[i][0]==4){
                int j=0;
             boolean bt=false;
             while(!bt){
                if(tmp[j][0]==2){
                  int[] a1=new int[card_face.length];
                  int ans=tmp[i][1];    
                     for(int k=0;k<4;k++)
                      a1[k]=ans++;    
                     a1[4]=tmp[j][1];
                     a1[5]=tmp[j][1]+1;
                     setCardFaceArray(card_face.length,a1); 
                     //出牌后对玩家手中的牌进行相应清理
                for(int x=0;x<card_face.length;x++)
                 s.clearCardIDarray(card_face[x]);
                //设置未出牌数
                  s.setsqunout(card_face.length);     
                bt=true;
                return true;
                }
               else {
                   j++;
                   if(j>12) {bt=true; return false;} 
                   }       
               } 
            }
           
       }    
     return false;
        
    }
    
    //playZhaDan2(s)
    public boolean playZhaDan2(SPlayers s){
        for(int i=judgeLocation(card_face[0])+1;i<13;i++){
             if(tmp[i][0]==4){
                int[] a1=new int[card_face.length];
              int ans=tmp[i][1];    
              for(int k=0;k<4;k++)
                  a1[k]=ans++;    
                 setCardFaceArray(card_face.length,a1); 
                 //出牌后对玩家手中的牌进行相应清理
             for(int x=0;x<card_face.length;x++)
              s.clearCardIDarray(card_face[x]);
              //设置未出牌数
               s.setsqunout(card_face.length);         
               return true;
              }
            
            }
      return false;
    }
    
    //    playLianZi2()
    public boolean playLianZi2(SPlayers s){
      int lens=1;
      int end=0;
      for(int i=judgeLocation(card_face[0])+1;i<13&&lens<=card_face.length;i++){
              for(int j=0;j<card_face.length;j++,i++)
                 if(tmp[i][0]>0){
                  lens++;
                  if(j==card_face.length-1) end=i;
                  
                  }
                 else {
                     lens=1;
                     break;
                 }
          }//for
      //找到匹配的连子
      if(lens>card_face.length){
            int start=-1;
          start=end-card_face.length+1;//连子开始组号
            int[] a1=new int[card_face.length];
            for(int i=0;i<card_face.length;i++)
               a1[i]=tmp[start++][1];
            setCardFaceArray(card_face.length,a1);
            ring_token=s.getpid();
           //出牌后对玩家手中的牌进行相应清理
          for(int j=0;j<card_face.length;j++)
           s.clearCardIDarray(card_face[j]);
          //设置未出牌数
           s.setsqunout(card_face.length);    
           return true;
          }
      //未找到匹配的连子
      else   return false;         
          
     }//playLianZi2()
  

你可能感兴趣的:(斗地主源码(字符界面)--单机游戏(2))