总共七个类packagecom;importjava.awt.Point;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.ImageIcon;importjavax.swing.JLabel;publicclassCardextendsJLabelimplementsMouseListener{Mainmain;//Main类的引用Stringname;//图片url名字booleanup;//是否正反面booleancanClick=false;//是否可被点击booleanclicked=false;//是否点击过publicCard(Mainm,Stringname,booleanup){this.main=m;this.name=name;this.up=up;if(this.up)this.turnFront();else{this.turnRear();}this.setSize(71,96);this.setVisible(true);this.addMouseListener(this);}//正面publicvoidturnFront(){this.setIcon(newImageIcon(images/+name+.gif));this.up=true;}//反面publicvoidturnRear(){this.setIcon(newImageIcon(images/rear.gif));this.up=false;}@OverridepublicvoidmouseClicked(MouseEvente){//TODOAuto-generatedmethodstubif(canClick){Pointfrom=this.getLocation();intstep;//移动的距离if(clicked)step=-20;else{step=20;}clicked=!clicked;//反向//当被选中的时候,向前移动一步/后退一步Common.move(this,from,newPoint(from.x,from.y-step));}}publicvoidmouseEntered(MouseEventarg0){}publicvoidmouseExited(MouseEventarg0){}publicvoidmousePressed(MouseEventarg0){}publicvoidmouseReleased(MouseEventarg0){}}packagecom;publicenumCardType{c1,//单牌。c2,//对子。c3,//3不带。c4,//炸弹。c31,//3带1。c32,//3带2。c411,//4带2个单,或者一对c422,//4带2对c123,//连子。c1122,//连队。c111222,//飞机。c11122234,//飞机带单排.c1112223344,//飞机带对子.c0//不能出牌}packagecom;importjava.awt.Point;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.List;importjava.util.concurrent.Delayed;publicclassCommon{//判断牌型publicstaticCardTypejugdeType(ListCardlist){//因为之前排序过所以比较好判断intlen=list.size();//单牌,对子,3不带,4个一样炸弹if(len=4){//如果第一个和最后个相同,说明全部相同if(list.size()0&&Common.getValue(list.get(0))==Common.getValue(list.get(len-1))){switch(len){case1:returnCardType.c1;case2:returnCardType.c2;case3:returnCardType.c3;case4:returnCardType.c4;}}//双王,化为对子返回if(len==2&&Common.getColor(list.get(1))==5)returnCardType.c2;//当第一个和最后个不同时,3带1if(len==4&&((Common.getValue(list.get(0))==Common.getValue(list.get(len-2)))||Common.getValue(list.get(1))==Common.getValue(list.get(len-1))))returnCardType.c31;else{returnCardType.c0;}}//当5张以上时,连字,3带2,飞机,2顺,4带2等等if(len=5){//现在按相同数字最大出现次数Card_indexcard_index=newCard_index();for(inti=0;i4;i++)card_index.a[i]=newArrayListInteger();//求出各种数字出现频率Common.getMax(card_index,list);//a[0,1,2,3]分别表示重复1,2,3,4次的牌//3带2-----必含重复3次的牌if(card_index.a[2].size()==1&&card_index.a[1].size()==1&&len==5)returnCardType.c32;//4带2(单,双)if(card_index.a[3].size()==1&&len==6)returnCardType.c411;if(card_index.a[3].size()==1&&card_index.a[1].size()==2&&len==8)returnCardType.c422;//单连,保证不存在王if((Common.getColor(list.get(0))!=5)&&(card_index.a[0].size()==len)&&(Common.getValue(list.get(0))-Common.getValue(list.get(len-1))==len-1))returnCardType.c123;//连队if(card_index.a[1].size()==len/2&&len%2==0&&len/2=3&&(Common.getValue(list.get(0))-Common.getValue(list.get(len-1))==(len/2-1)))returnCardType.c1122;//飞机if(card_index.a[2].size()==len/3&&(len%3==0)&&(Common.getValue(list.get(0))-Common.getValue(list.get(len-1))==(len/3-1)))returnCardType.c111222;//飞机带n单,n/2对if(card_index.a[2].size()==len/4&&((Integer)(card_index.a[2].get(len/4-1))-(Integer)(card_index.a[2].get(0))==len/4-1))returnCardType.c11122234;//飞机带n双if(card_index.a[2].size()==len/5&&card_index.a[2].size()==len/5&&((Integer)(card_index.a[2].get(len/5-1))-(Integer)(card_index.a[2].get(0))==len/5-1))returnCardType.c1112223344;}returnCardType.c0;}//移动效果的函数,用于发牌publicstaticvoidmove(Cardcard,Pointfrom,Pointto){if(to.x!=from.x){doublek=(1.0)*(to.y-from.y)/(to.x-from.x);doubleb=to.y-to.x*k;intflag=0;//判断向左还是向右移动步幅if(from.xto.x)flag=20;else{flag=-20;}for(inti=from.x;Math.abs(i-to.x)20;i+=flag){doubley=k*i+b;//这里主要用的数学中的线性函数card.setLocation(i,(int)y);try{Thread.sleep(5);//延迟,可自己设置}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}//位置校准card.setLocation(to);}//对list排序publicstaticvoidorder(ListCardlist){Collections.sort(list,newComparatorCard(){@Overridepublicintcompare(Cardo1,Cardo2){//TODOAuto-generatedmethodstubinta1=Integer.parseInt(o1.name.substring(0,1));//花色inta2=Integer.parseInt(o2.name.substring(0,1));intb1=Integer.parseInt(o1.name.substring(2,o1.name.length()));//数值intb2=Integer.parseInt(o2.name.substring(2,o2.name.length()));intflag=0;//如果是王的话if(a1==5)b1+=100;if(a1==5&&b1==1)b1+=50;if(a2==5)b2+=100;if(a2==5&&b2==1)b2+=50;//如果是A或者2if(b1==1)b1+=20;if(b2==1)b2+=20;if(b1==2)b1+=30;if(b2==2)b2+=30;flag=b2-b1;if(flag==0)returna2-a1;else{returnflag;}}});}//重新定位flag代表电脑1,2或者是我publicstaticvoidrePosition(Mainm,ListCardlist,intflag){Pointp=newPoint();if(flag==0){p.x=50;p.y=(450/2)-(list.size()+1)*15/2;}if(flag==1){//我的排序_y=450width=830p.x=(800/2)-(list.size()+1)*21/2;p.y=450;}if(flag==2){p.x=700;p.y=(450/2)-(list.size()+1)*15/2;}intlen=list.size();for(inti=0;ilen;i++){Cardcard=list.get(i);Common.move(card,card.getLocation(),p);m.container.setComponentZOrder(card,0);if(flag==1)p.x+=21;elsep.y+=15;}}//地主牌权值,看是否抢地主publicstaticintgetScore(ListCardlist){intcount=0;for(inti=0,len=list.s