您好,欢迎访问三七文档
斗地主出牌算法根据斗地主出牌规则.对玩家出的牌进行检验.判断是否符合出牌规则.(关于斗地主的出牌规则网上有很多)思路:将玩家的牌按升序排序.然后将牌进行拆分,分存在4个数组中.拆分规则如下:假设有牌:333\444\555\789则拆分后数组中的数据如下arr[0]:345789arr[1]:345arr[2]:345arr[3]:null可以看出拆分规则是:如果遇到相同数字的牌则存到下一个数组的末尾.拆分完后可以根据各数组的存储情况判定玩家出牌的类型,上面例子arr[3]为空.可以排除掉4带1(2).炸弹.的情况根据arr[2]为顺子且个数大于1,且arr[2]中存放的牌的张数乘以3刚好等于arr[0]的张数+arr[1]的张数.则可以判定是三带一的飞机.其他类型的牌也有相似的规律.以下是该算法的核心源代码.本算法用C#编写.usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceLordLibrary{publicclassCheckType{privatestaticint[][]DiffRow(int[]nums){int[][]list=newint[4][];for(inti=0;ilist.Length;i++){list=newint[20];}int[]rowIndex=newint[4];intcolumIndex=0;for(inti=0;inums.Length;i++){if(i+1nums.Length){if(nums!=0){list[columIndex][rowIndex[columIndex]]=nums;rowIndex[columIndex]++;}if(nums==nums[i+1]){columIndex++;}else{columIndex=0;}}elseif(nums!=0)list[columIndex][rowIndex[columIndex]]=nums;}returnlist;}privatestaticintcheckListCount(int[][]list,introwIndex,intcompStart,introwMaxCount){/*LIST代表单顺.*DOUB代表双顺.*FEI0代表三顺.*FEI1代表三带一的飞机*FEI2代表三带二的飞机*FOR1代表四带1*FOR2代表四带2*ROCK代表大小王*/intlistCount=1;for(inti=compStart;irowMaxCount-1;i++){if(list[rowIndex]+1==list[rowIndex][i+1])listCount++;elselistCount=1;}returnlistCount;}publicstaticstringgetCardType(int[]nums){int[][]list=DiffRow(nums);int[]counts=newint[4];for(intk=0;k4;k++){counts[k]=Array.IndexOf(list[k],0);}intMaxValue=0;intlistCount=0;stringtype=string.Empty;//当第4行牌的数量为1的时候#regionif(counts[3]==1){intindex=Array.IndexOf(list[2],list[3][0]);switch(counts[2]){case1:MaxValue=list[3][0];if(counts[0]==1){type=BOMB:4:+MaxValue;}elseif(counts[0]+counts[1]==4){type=FOR1:6:+MaxValue;}elseif(counts[0]==counts[1]&&counts[0]==3){type=FOR2:8:+MaxValue;}break;case2:if(list[2][0]+1==list[2][1]&&counts[1]==counts[2]&&counts[0]==3){MaxValue=list[2][counts[2]-1];type=FEI1:+counts[2]+:+MaxValue;}break;case3:if(checkListCount(list,2,0,counts[2])==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+counts[2]+:+MaxValue;}elseif(Array.IndexOf(list[2],list[3][0])==0&&counts[0]==counts[2]){if((listCount=checkListCount(list,2,1,counts[2]))==counts[2]-1){MaxValue=list[2][counts[2]-1];type=FEI2:+listCount+:+MaxValue;}}elseif(Array.IndexOf(list[2],list[3][0])==counts[2]-1&&counts[0]==counts[2]){if((listCount=checkListCount(list,2,0,counts[2]-1))==counts[2]-1){MaxValue=list[2][counts[2]-2];type=FEI2:+listCount+:+MaxValue;}}break;case4:if(index==0&&counts[0]==counts[1]&&counts[0]==5){if((listCount=checkListCount(list,2,1,counts[2]))==counts[2]-1){MaxValue=list[2][counts[2]-1];type=FEI2:+listCount+:+MaxValue;}}elseif(index==counts[2]-1&&counts[0]==counts[1]&&counts[0]==5){if((listCount=checkListCount(list,2,0,counts[2]-1))==counts[2]-1){MaxValue=list[2][counts[2]-2];type=FEI2:+listCount+:+MaxValue;}}elseif((listCount=checkListCount(list,2,0,counts[2]))==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}break;case5:if(index==0){if((listCount=checkListCount(list,2,0,counts[2]))==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}elseif(listCount==counts[2]-1&&counts[0]==counts[1]){if(counts[0]+1==2*(counts[2]-1)){MaxValue=list[2][counts[2]-1];type=FEI2:+listCount+:+MaxValue;}elseif(2*(counts[0]+1)==3*(counts[2]-1)){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}}}elseif(index==counts[2]-1){if((listCount=checkListCount(list,2,0,counts[2]))==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}elseif((listCount=checkListCount(list,2,0,counts[2]-1))==counts[2]-1&&counts[0]==counts[1]){if(counts[0]+1==2*(counts[2]-1)){MaxValue=list[2][counts[2]-2];type=FEI2:+listCount+:+MaxValue;}elseif(2*(counts[0]+1)==3*(counts[2]-1)){MaxValue=list[2][counts[2]-2];type=FEI1:+listCount+:+MaxValue;}}}else{if((listCount=checkListCount(list,2,0,counts[2]))==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}}break;case6:if((listCount=checkListCount(list,2,0,counts[2]))==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}elseif(index==0&&listCount==counts[2]-1&&counts[0]+counts[1]+2==3*(counts[2]-1)){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}elseif(index==counts[2]-1&&(listCount=checkListCount(list,2,0,counts[2]-1))==counts[2]-1&&counts[0]+counts[1]+2==3*(counts[2]-1)){MaxValue=list[2][counts[2]-2];type=FEI1:+listCount+:+MaxValue;}break;}}#endregion//当第4行牌的数量为2的时候#regionif(counts[3]==2){switch(counts[2]){default:if(counts[2]=2&&counts[2]6){if((listCount=checkListCount(list,2,0,counts[2]))==counts[2]&&counts[0]+counts[1]+counts[3]==3*counts[2]){MaxValue=list[2][counts[2]-1];type=FEI1:+listCount+:+MaxValue;}}break;case6:intfirstIndex=Array.IndexOf(list[2],list[3][0]);intsecIndex=Array.IndexOf(list[2],list[3][1]);if(secIndex==1){if((listCount=checkL
本文标题:1C#斗地主算法
链接地址:https://www.777doc.com/doc-8036993 .html