新年第一更
for (k in 1:1000){ #万字,筒子,条子 pai=c(1:9,11:19,21:29) mj=c(pai,pai,pai,pai) jiepai=sample(mj,13*4,replace=FALSE) a=sort(jiepai[1:13]) b=sort(jiepai[14:(13*2)]) c=sort(jiepai[27:(13*3)]) d=sort(jiepai[40:(13*4)]) leftpai=function(mj,x){ for (i in 1:length(x)) { mj=mj[-match(x[i],mj)] } return(mj) } mjleft=leftpai(mj,jiepai) #打掉跟其他字差异最大的字 droppai=function(x){ sumdif=c() for (i in 1:length(x)){ sumdif[i]=sum((x[i]-x[-i])**2)+i/100 } tablepai=data.frame(x,sumdif) tablepai[sumdif==max(sumdif),]=NA x=as.numeric(na.omit(tablepai$x)) return(x) } #一个对子,四个句子或者刻子即胡牌 hupai=function(x){ tmp=c() hupai=0 for (i in seq(1,13,3)){ if (((i!=13) & (x[i]==x[i+1]) & (x[i+1] !=x[i+2])) || ((i==13) & (x[i]==x[i+1]))) { tmp=x[c(-i,-(i+1))] hupai=hupai++1 if (hupai>0) { break} } } if (hupai==1) { for (i in seq(2,11,3)){ if ( ((tmp[i]-tmp[i-1]==1) & (tmp[i+1]-tmp[i]==1)) || ((tmp[i]-tmp[i-1]==0) & (tmp[i+1]-tmp[i]==0)) ) {hupai=hupai++1} } } return(hupai) } #开始打牌 for (i in 0:ceiling(length(mjleft)/4-1)){ a=sort(c(a,mjleft[1+4*i])) b=sort(c(b,mjleft[2+4*i])) c=sort(c(c,mjleft[3+4*i])) d=sort(c(d,mjleft[4+4*i])) sumA=sum(a) sumB=sum(b) sumC=sum(c) sumD=sum(d) if ((hupai(a)==5) & (sumA%%3==2)) {print("player a win");print(sort(a));print(i);break} else if ((hupai(b)==5) & (sumB%%3==2)) {print("player b win");print(sort(b));print(i);break} else if ((hupai(c)==5) & (sumC%%3==2)) {print("player c win");print(sort(c));print(i);break} else if ((hupai(d)==5) & (sumD%%3==2)) {print("player d win");print(sort(d));print(i);break} a=droppai(a) b=droppai(b) c=droppai(c) d=droppai(d) if (i==ceiling(length(mjleft)/4-1)) {print("No one wins")} }}
不能吃,不能碰,只能自摸,打了1000盘只有不到20盘胡牌。。。看来打牌还是需要多吃多碰(另外代码肯定需要改进……)
[1] "No one wins"[1] "No one wins"[1] "player b win" [1] 12 12 12 16 17 18 19 19 21 22 23 26 27 28[1] 5[1] "No one wins"[1] "player a win" [1] 13 13 16 17 18 21 22 23 24 25 26 27 27 27[1] 7[1] "No one wins"[1] "No one wins"[1] "No one wins"[1] "No one wins"[1] "player b win" [1] 16 17 18 21 22 23 24 24 24 25 25 26 27 28[1] 11[1] "No one wins"[1] "No one wins"[1] "No one wins"[1] "player a win" [1] 15 15 15 16 17 18 19 19 22 22 22 23 24 25[1] 12[1] "No one wins"...
祝大家新年行大运
by yant07