JAVA collection集合之扑克牌游戏实例
Collection层次结构中的根接口。Collection表示一组对象,这些对象也称为collection的元素。一些collection允许有重复的元素,而另一些则不允许。一些collection是有序的,而另一些则是无序的。JDK不提供此接口的任何直接实现:它提供更具体的子接口(如Set和List)实现。此接口通常用来传递collection,并在需要最大普遍性的地方操作这些collection。
主要内容:这里使用collection集合,模拟香港电影中大佬们玩的扑克牌游戏。
1、游戏规则:两个玩家每人手中发两张牌,进行比较。比较每个玩家手中牌最大的点数,大小由A-2,点数大者获胜。如果点数相同,则比较花色,大小由黑(4)、红(3)、梅(2)、方(1),花色大者获胜。
2、实现步骤:
创建一副扑克牌A-2,四种花色黑(4)、红(3)、梅(2)、方(1)共52张牌;
创建两个玩家包含玩家ID和姓名、所持牌Card信息;
洗牌并向两位玩家各发两张牌;
比较玩家手中牌大小,得出获胜者;
3、程序实现
牌Card类:包含牌的数字和花色
packagecollectiontest.games; publicclassCard{ privateIntegerid;//牌的大小 privateIntegertype;//牌的花色 publicCard(Integerid,Integertype){ this.id=id; this.type=type; } publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicIntegergetType(){ returntype; } publicvoidsetType(Integertype){ this.type=type; } @Override publicStringtoString(){ return"Card[id="+id+",type="+type+"]"; } }
扑克牌Poker类:包含扑克牌CardA-2
packagecollectiontest.games; publicclassPoker{ privateCardid2; privateCardid3; privateCardid4; privateCardid5; privateCardid6; privateCardid7; privateCardid8; privateCardid9; privateCardid10; privateCardJ; privateCardQ; privateCardK; privateCardA; publicPoker(){ } //四个类型:黑--4、红--3、梅--2、方--1 publicPoker(Integertype){ this.id2=newCard(2,type); this.id3=newCard(3,type); this.id4=newCard(4,type); this.id5=newCard(5,type); this.id6=newCard(6,type); this.id7=newCard(7,type); this.id8=newCard(8,type); this.id9=newCard(9,type); this.id10=newCard(10,type); this.J=newCard(11,type); this.Q=newCard(12,type); this.K=newCard(13,type); this.A=newCard(14,type); } publicCardgetId2(){ returnid2; } publicvoidsetId2(Cardid2){ this.id2=id2; } publicCardgetId3(){ returnid3; } publicvoidsetId3(Cardid3){ this.id3=id3; } publicCardgetId4(){ returnid4; } publicvoidsetId4(Cardid4){ this.id4=id4; } publicCardgetId5(){ returnid5; } publicvoidsetId5(Cardid5){ this.id5=id5; } publicCardgetId6(){ returnid6; } publicvoidsetId6(Cardid6){ this.id6=id6; } publicCardgetId7(){ returnid7; } publicvoidsetId7(Cardid7){ this.id7=id7; } publicCardgetId8(){ returnid8; } publicvoidsetId8(Cardid8){ this.id8=id8; } publicCardgetId9(){ returnid9; } publicvoidsetId9(Cardid9){ this.id9=id9; } publicCardgetId10(){ returnid10; } publicvoidsetId10(Cardid10){ this.id10=id10; } publicCardgetJ(){ returnJ; } publicvoidsetJ(Cardj){ J=j; } publicCardgetQ(){ returnQ; } publicvoidsetQ(Cardq){ Q=q; } publicCardgetK(){ returnK; } publicvoidsetK(Cardk){ K=k; } publicCardgetA(){ returnA; } publicvoidsetA(Carda){ A=a; } }
玩家Player类:包含玩家ID和姓名、所持卡片信息
packagecollectiontest.games; importjava.util.ArrayList; importjava.util.List; publicclassPlayer{ //玩家的ID privateStringid; //玩家姓名 privateStringname; //玩家所持牌 privateList<Card>pokerType; publicPlayer(){ } publicPlayer(Stringid,Stringname,List<Card>pokerType){ this.id=id; this.name=name; this.pokerType=newArrayList<>(); } publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicList<Card>getPokerType(){ returnpokerType; } publicvoidsetPokerType(List<Card>pokerType){ this.pokerType=pokerType; } }
扑克牌游戏主类:包含1)扑克牌创建2)玩家创建3)洗牌4)发牌5)比较胜负
packagecollectiontest.games; importjava.util.ArrayList; importjava.util.Arrays; importjava.util.Collections; importjava.util.HashMap; importjava.util.HashSet; importjava.util.List; importjava.util.Map; importjava.util.Scanner; importjava.util.Set; publicclassGamsBegin{ //创建扑克牌 publicSet<Poker>cPoker(){ System.out.println("**********开始创建扑克牌**********"); //创建一副poker //四个类型:黑--4、红--3、梅--2、方--1 Set<Poker>pokers=newHashSet<>(); Poker[]poker={newPoker(1),newPoker(2),newPoker(3), newPoker(4)}; /* *Collections工具类的使用 *Collections.addAll(pokers,newPoker(1),newPoker(2),newPoker(3),newPoker(4)); * **/ pokers.addAll(Arrays.asList(poker)); System.out.println("**********扑克牌创建成功**********"); returnpokers; } //创建两个玩家 publicMap<String,Player>cPlayer(){ System.out.println("**********开始创建玩家**********"); Map<String,Player>map=newHashMap<String,Player>(); //控制数量 Integercontrol=0; System.out.println("创建两名玩家,根据提示创建"); Scannerconsole=newScanner(System.in); while(true){ System.out.println("请输入第"+(control+1)+"个玩家ID:"); StringcourseId=console.next(); if(isNumeric(courseId)){ System.out.println("请输入第"+(control+1)+"个玩家姓名:"); StringcourseName=console.next(); Playerplayers=newPlayer(courseId,courseName,null); //保存数据 map.put(courseId,players); System.out.println("添加第"+(control+1)+"个玩家"+courseName +"成功"); //数量自加 control++; }else{ System.out.println("*****请输入数字ID*****"); continue; } if(control==2){ break; } } System.out.println("**********玩家创建成功**********"); returnmap; } //判断输入是否为数字,Character.isDigit()为java方法 publicbooleanisNumeric(Stringstr){ for(inti=0;i<str.length();i++){ if(!Character.isDigit(str.charAt(i))){ returnfalse; } } returntrue; } /** *洗牌:也可以产生52个不同随机数,实现洗牌 * **/ publicList<Card>wPoker(Set<Poker>pokers){ System.out.println("**********开始洗牌**********"); //利用List的有序排序,洗牌之后保存顺序不变 List<Card>listCard=newArrayList<>(); //利用Set集合的无序排序,实现洗牌 Set<Card>listSet=newHashSet<>(); //保存到Set集合,无序 for(Pokerpk:pokers){ listSet.add(pk.getId2()); listSet.add(pk.getId3()); listSet.add(pk.getId4()); listSet.add(pk.getId5()); listSet.add(pk.getId6()); listSet.add(pk.getId7()); listSet.add(pk.getId8()); listSet.add(pk.getId9()); listSet.add(pk.getId10()); listSet.add(pk.getJ()); listSet.add(pk.getQ()); listSet.add(pk.getK()); listSet.add(pk.getA()); } //保存在List集合,有序 for(Cardcd:listSet){ listCard.add(cd); System.out.println(cd); } System.out.println("**********洗牌成功**********"); returnlistCard; } //发牌 publicMap<String,Player>pushPoker(List<Card>listCard, Map<String,Player>pMap){ System.out.println("**********发牌开始**********"); //控制每人发两张牌后结束 intcontrol=0; for(Map.Entry<String,Player>entry:pMap.entrySet()){ if(control==0){ for(inti=0;i<3;i=i+2){ //发牌 entry.getValue().getPokerType().add(listCard.get(i)); } //更新map对象 pMap.put(entry.getKey(),entry.getValue()); control++; }elseif(control==1){ for(inti=1;i<4;i=i+2){ //发牌 entry.getValue().getPokerType().add(listCard.get(i)); } //更新map对象 pMap.put(entry.getKey(),entry.getValue()); control++; }else{ break; } } System.out.println("**********发牌成功**********"); returnpMap; } publicvoidcompareMatch(Map<String,Player>newMap){ /*比较胜负 *1.首先取得每个玩家手中最大牌的ID和花色ID。 *2.比较俩玩家手中最大牌的ID大小,牌大者获胜。 *3.如果两张牌的ID相等,在比较两张牌的花色ID,花色ID更大着获胜。 * **/ List<Player>players=newArrayList<>(); //获得两个玩家 for(Map.Entry<String,Player>entry:newMap.entrySet()){ players.add(entry.getValue()); } //玩家一信息和所持牌 List<Card>playerOne=players.get(0).getPokerType(); //获得最大牌的ID和花色 IntegeroneMaxId=Math.max(playerOne.get(0).getId(),playerOne.get(1) .getId()); IntegeroneMaxType=(oneMaxId!=playerOne.get(0).getId())?playerOne.get(1).getType():playerOne.get(0).getType(); //玩家二信息和所持牌 List<Card>playerTwo=players.get(1).getPokerType(); //获得最大牌的ID和花色 IntegertwoMaxId=Math.max(playerTwo.get(0).getId(),playerTwo.get(1) .getId()); IntegertwoMaxType=(twoMaxId!=playerTwo.get(0).getId())?playerTwo.get(1).getType():playerTwo.get(0).getType(); if(oneMaxId>twoMaxId){ System.out.println("玩家:"+players.get(0).getName()+"获胜!!"); }elseif(oneMaxId==twoMaxId){ if(oneMaxType>twoMaxType){ System.out .println("玩家:"+players.get(0).getName()+"获胜!!"); }else{ System.out .println("玩家:"+players.get(1).getName()+"获胜!!"); } }else{ System.out.println("玩家:"+players.get(1).getName()+"获胜!!"); } System.out.println("**********************************************"); System.out.println("玩家:"+players.get(0).getName()+"的牌是:" +showName(playerOne.get(0).getType(),0)+"--" +showName(playerOne.get(0).getId(),1)+"" +showName(playerOne.get(1).getType(),0)+"--" +showName(playerOne.get(1).getId(),1)); System.out.println("玩家:"+players.get(1).getName()+"的牌是:" +showName(playerTwo.get(0).getType(),0)+"--" +showName(playerTwo.get(0).getId(),1)+"" +showName(playerTwo.get(1).getType(),0)+"--" +showName(playerTwo.get(1).getId(),1)); } //显示牌的名称 privateStringshowName(Integeri,Integertype){ Stringstr=""; //显示花色 if(type==0){ switch(i){ case1:{ str="方块"; break; } case2:{ str="梅花"; break; } case3:{ str="红桃"; break; } case4:{ str="黑桃"; break; } default:{ break; } } } //显示数字 if(type==1){ if(i<11){ returni.toString(); }else{ switch(i){ case11:{ str="J"; break; } case12:{ str="Q"; break; } case13:{ str="K"; break; } case14:{ str="A"; break; } default:{ break; } } } } returnstr; } publicstaticvoidmain(String[]args){ GamsBegingb=newGamsBegin(); //1、创建扑克牌 Set<Poker>pokers=gb.cPoker(); //2、创建两个玩家 Map<String,Player>pMap=gb.cPlayer(); //3、洗牌 List<Card>listCard=gb.wPoker(pokers); //4、发牌 Map<String,Player>newMap=gb.pushPoker(listCard,pMap); //4、比较胜负 gb.compareMatch(newMap); } }
运行结果:
**********开始创建扑克牌**********
**********扑克牌创建成功**********
**********开始创建玩家**********
创建两名玩家,根据提示创建
请输入第 1个玩家ID:
请输入第 1个玩家姓名:
周星驰
添加第1个玩家 周星驰成功
请输入第 2个玩家ID:
请输入第 2个玩家姓名:
周润发
添加第2个玩家 周润发成功
**********玩家创建成功**********
**********开始洗牌**********
Card[id=9,type=3]
Card[id=11,type=4]
Card[id=13,type=3]
Card[id=8,type=3]
Card[id=5,type=2]
Card[id=6,type=1]
Card[id=4,type=3]
Card[id=5,type=4]
Card[id=2,type=3]
Card[id=9,type=2]
Card[id=9,type=4]
Card[id=14,type=2]
Card[id=9,type=1]
Card[id=2,type=1]
Card[id=2,type=4]
Card[id=7,type=4]
Card[id=11,type=1]
Card[id=10,type=1]
Card[id=14,type=4]
Card[id=14,type=3]
Card[id=12,type=2]
Card[id=2,type=2]
Card[id=10,type=2]
Card[id=7,type=1]
Card[id=7,type=3]
Card[id=8,type=2]
Card[id=4,type=4]
Card[id=13,type=4]
Card[id=14,type=1]
Card[id=12,type=1]
Card[id=5,type=1]
Card[id=6,type=4]
Card[id=12,type=4]
Card[id=11,type=2]
Card[id=10,type=3]
Card[id=3,type=4]
Card[id=12,type=3]
Card[id=4,type=2]
Card[id=4,type=1]
Card[id=6,type=2]
Card[id=5,type=3]
Card[id=8,type=4]
Card[id=3,type=2]
Card[id=13,type=2]
Card[id=7,type=2]
Card[id=3,type=3]
Card[id=3,type=1]
Card[id=6,type=3]
Card[id=8,type=1]
Card[id=11,type=3]
Card[id=13,type=1]
Card[id=10,type=4]
**********洗牌成功**********
**********发牌开始**********
**********发牌成功**********
玩家:周星驰获胜!!
**********************************************
玩家:周星驰的牌是:红桃--9 红桃--K
玩家:周润发的牌是:黑桃--J 红桃--8
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。