java实现斗地主游戏

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

玩家类:packagecom.rh.lianxi;importjava.util.ArrayList;publicclassPlayer{privateStringname;privateStringscore;privateStringsex;privateArrayListStringlist;publicPlayer(Stringname,Stringscore,Stringsex){super();this.name=name;this.score=score;this.sex=sex;this.list=list;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicStringgetScore(){returnscore;}publicvoidsetScore(Stringscore){this.score=score;}publicStringgetSex(){returnsex;}publicvoidsetSex(Stringsex){this.sex=sex;}publicArrayListStringgetList(){returnlist;}publicvoidsetList(ArrayListStringlist){this.list=list;}}游戏:packagecom.rh.lianxi;importjava.util.ArrayList;importjava.util.Random;publicclassPokerGame{/**新牌*/publicstaticArrayListStringbuyPoker(){ArrayListStringpoker=newArrayListString();String[]color={方片,草花,红桃,黑桃};String[]value={A,2,3,4,5,6,7,8,9,10,J,Q,K};String[]wangpai={大王,小王};poker.add(wangpai[0]);poker.add(wangpai[1]);for(inti=0;icolor.length;i++){for(intj=0;jvalue.length;j++){poker.add(color[i]+value[j]);}}returnpoker;}/**洗牌param:ArrayListpokerlist**@returnlist业务:如果这张牌已经存在了,不做处理;反之,将新牌添加进去注意:牌数=54张*/publicstaticArrayListStringwashPoker(ArrayListStringpokerlist){Randomrandom=newRandom();ArrayListStringlist=newArrayListString();intcount=0;while(countpokerlist.size()){inta=random.nextInt(54);if(!list.contains(pokerlist.get(a))){//不存在的牌把它放进去list.add(pokerlist.get(a));count++;}//否则不做处理}returnlist;}/**摸牌param:洗好的牌return:3副牌和底牌=obj[]第一个人是拿的1,4,7,10---3(x-1)+1*第二个人是拿的2,5,8,11---3(x-1)+2第三个人是拿的2,5,8,11---3(x-1)+3*/publicstaticObject[]groupPoker(ArrayListStringwashpokerlist){Object[]obj=newObject[4];ArrayListStringlist1=newArrayListString();ArrayListStringlist2=newArrayListString();ArrayListStringlist3=newArrayListString();ArrayListStringlist4=newArrayListString();for(inti=1;iwashpokerlist.size()/3;i++){list1.add(washpokerlist.get(3*(i-1)+1));list2.add(washpokerlist.get(3*(i-1)+2));list3.add(washpokerlist.get(3*(i-1)+3));}for(inti=51;iwashpokerlist.size();i++){list4.add(washpokerlist.get(i));}obj[0]=list1;obj[1]=list2;obj[2]=list3;obj[3]=list4;returnobj;}/**码牌:牌面param:ArrayListstringreturn:arraylist*/publicstaticArrayListStringorderPoker2(ArrayListStringgrouplist){String[]str={大王,小王,A,2,3,4,5,6,7,8,9,10,J,Q,K};ArrayListStringarraylist=newArrayListString();for(inti=0;istr.length;i++){for(intj=0;jgrouplist.size();j++){//先遍历手牌,再去匹配strif(grouplist.get(j).contains(str[i])){arraylist.add(grouplist.get(j));}}}returnarraylist;}/**叫牌param:object[],Stringname业务:根据name的值,给对应的player的牌,三个玩家player*/publicstaticPlayer[]qiangDiZhu(Object[]pokers,Stringname,Player[]players){//1.牌组分配给玩家,随机数组Randomrandom=newRandom();int[]num=newint[3];for(inti=0;inum.length;i++){num[i]=random.nextInt(3);for(intj=0;ji;j++){if(num[i]==num[j]){num[i]=random.nextInt(3);i--;}}}for(inti=0;iplayers.length;i++){players[i].setList((ArrayListString)pokers[num[i]]);}//2.地主码牌for(inti=0;iplayers.length;i++){if(name.equals(players[i].getName())){players[i].getList().addAll((ArrayListString)pokers[pokers.length-1]);}}//3.排好序返回给players数组中for(inti=0;iplayers.length;i++){players[i].setList(orderPoker2(players[i].getList()));}returnplayers;}/**针对牌面*/publicstaticvoidprintPoker(ArrayListStringlist){System.out.println(list.size());for(inti=0;ilist.size();i++){System.out.print(list.get(i)+);}}/**针对牌组*/publicstaticvoidprintPoker(Object[]obj){for(inti=0;iobj.length;i++){printPoker((ArrayListString)obj[i]);System.out.println();}}/**针对玩家*/publicstaticvoidprintPoker(Player[]players){for(inti=0;iplayers.length;i++){System.out.println(玩家+players[i].getName()+手里的牌是+players[i].getList());System.out.println();}}publicstaticvoidmain(String[]args){//System.out.println(新牌);//printPoker(buyPoker());//System.out.println();//System.out.println(洗牌后);//printPoker(washPoker(buyPoker()));//System.out.println();//System.out.println(摸牌后);//printPoker(groupPoker(washPoker(buyPoker())));Playerp1=newPlayer(小蓝,0,男);Playerp2=newPlayer(小红,0,女);Playerp3=newPlayer(小黄,0,男);Player[]players={p1,p2,p3};printPoker(qiangDiZhu(groupPoker(washPoker(buyPoker())),小红,players));}}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功