java实现多人多牌数比较游戏
本文实例为大家分享了java实现多人多牌数比较游戏的具体代码,供大家参考,具体内容如下
具体实现步骤如下:
- 实现扑克Card类,用于储存扑克牌,1-13代表点数,4-1代表花色(黑桃,红桃,梅花,分块)
- 实现扑克列表CardList类,用于实现生成一副牌,洗牌,发牌的功能
- 实现玩家Player类,用于储存玩家信息
- 实现玩家列表PlayerList类,用于储存所有玩家信息,并且实现玩家洗牌、发牌、比较牌大小的方法
- 主方法Index05类,用于启动扑克游戏
Card.java
/** * */ packagedk.card; /** *@authorAdministrator *扑克类 */ publicclassCardimplementsComparable{ privateintnumber;//牌的点数--1到13 privateinttype;//牌的花色--4种花色:♠(黑桃):4♥(红桃):3♣(梅花):2方块:1 publicCard(){ super(); } publicCard(intnumber,inttype){ if(number==1) number=14; this.number=number; this.type=type; } publicintgetNumber(){ returnnumber; } publicvoidsetNumber(intnumber){ this.number=number; } publicintgetType(){ returntype; } publicvoidsetType(inttype){ this.type=type; } @Override publicinthashCode(){ finalintprime=31; intresult=1; result=prime*result+number; result=prime*result+type; returnresult; } @Override publicbooleanequals(Objectobj){ if(this==obj) returntrue; if(obj==null) returnfalse; if(getClass()!=obj.getClass()) returnfalse; Cardother=(Card)obj; if(number!=other.number) returnfalse; if(type!=other.type) returnfalse; returntrue; } @Override publicintcompareTo(Cardo){ if(this.number>o.number) return-1; elseif(this.number o.type) return-1; elseif(this.type CardList.java
packagedk.card; importjava.util.ArrayList; importjava.util.List; importjava.util.Random; /** *@authorAdministrator *牌表操作类 */ publicclassCardList{ publicstaticListcardlist=newArrayList (52);//储存扑克牌 publicstaticList >poker;//储存发牌结果 //生成一副牌 publicstaticvoidgenerateCard(){ for(inti=1;i<=13;i++){ for(intj=1;j<=4;j++){ Cardc=newCard(i,j); cardlist.add(c); } } } //打印一副牌 publicstaticvoidprintCardList(){ System.out.print("["); for(Cardc:cardlist){ System.out.print(c.toString()+""); } System.out.println("]"); } //洗牌 publicstaticvoidshuffleCard(){ List scard=newArrayList (52); Cardc; Randomrandom=newRandom(); for(inti=1;i<=52;i++){ do{ c=cardlist.get(random.nextInt(52)); }while(scard.contains(c)); scard.add(c); } cardlist.clear(); cardlist=scard; printCardList(); } /** *发牌 *@paraminumberofplayer *@paramjnumberofcard *@return */ publicstaticList >dealCard(inti,intj){ try{ if(i<=1){ thrownewException("发牌规则有误:玩家至少为2人,请重新定义规则!"); } if(i*j>52){ thrownewException("发牌规则有误:牌数超出扑克张数,请重新定义规则!"); } poker=newArrayList >(i);//储存玩家发到的牌 //生成玩家手中的牌列表 for(inth=0;hplayer_card_list=newArrayList (j); poker.add(player_card_list); } //给每个玩家发牌 for(intk=0;k player_card_list=poker.get(d); Cardcard=getCard(); player_card_list.add(card); } } }catch(Exceptione){ e.printStackTrace(); } returnpoker; } //抓取一张牌 publicstaticCardgetCard(){ returncardlist.remove(0); } //获取一位玩家的牌 publicstaticArrayList getPoker(){ returnpoker.remove(0); } publicstaticvoidclear(){ cardlist.clear(); poker.clear(); printCardList(); } } Player.java
/** * */ packagedk.card; importjava.util.ArrayList; importjava.util.Collections; importjava.util.List; /** *@authorAdministrator *玩家类 */ publicclassPlayerimplementsComparable{ privateStringid;//玩家id privateStringname;//玩家姓名 privateList poker;//玩家手牌 privateCardmax;//玩家最大的牌 publicPlayer(Stringid,Stringname){ this.id=id; this.name=name; this.setPoker(newArrayList ()); } publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicList getPoker(){ returnpoker; } publicCardgetMax(){ returnmax; } publicvoidsetMax(Cardmax){ this.max=max; } publicvoidsetPoker(List poker){ this.poker=poker; Collections.sort(this.poker); for(Cardc:this.poker){ this.setMax(c); break; } } publicStringtoString(){ return"玩家Id:"+this.id+"\t玩家姓名:"+this.name; } //玩家摊牌 publicStringshowCard(){ Stringmsg=""; for(Cardc:this.poker){ msg+=c.toString()+""; } returnthis.toString()+"\t\n"+"玩家牌信息:\t\n"+msg; } @Override publicintcompareTo(Playero){ returnthis.max.compareTo(o.getMax()); } } PlayerList.java
packagedk.card; importjava.util.ArrayList; importjava.util.Collections; importjava.util.List; /** *@authorAdministrator *储存玩家列表 */ publicclassPlayerList{ publicstaticListplayerlist=newArrayList ();//储存扑玩家列表 publicstaticintsum; publicstaticList rules=newArrayList (); publicPlayerList(){ } publicstaticvoidsetRule(Stringrule){ rules.add(rule); } publicstaticStringgetRules(){ Stringstr=""; for(Strings:rules){ str+=s+"\t\n"; } returnstr; } publicstaticvoidprintRules(){ show(getRules()); } //添加玩家 publicstaticbooleanaddplayer(Playerplayer){ show("添加玩家:"+player.toString()); show("----------------------------------------"); sum++; returnplayerlist.add(player); } //给所有玩家发牌 publicstaticvoidshuffleCardToPlayer(){ for(Playerplayer:playerlist){ player.setPoker(CardList.getPoker()); } } //显示发牌结果 publicstaticvoidshowCard(){ for(Playerplayer:playerlist){ show(player.showCard()); show("----------------------------------------"); } } //比较牌点大小并且显示赢家信息 publicstaticvoidshowWinner(){ Collections.sort(playerlist); Playerp=playerlist.get(0); show(p.showCard()); show("本局赢家是:"+p.getName()); } //玩家人数 publicstaticintsize(){ returnsum; } publicstaticvoidshow(Objectobj){ System.out.println(obj.toString()); } publicstaticvoidclear(){ playerlist.clear(); sum=0; } } Index05.java
/** * */ packagedk.main; importjava.util.Scanner; importdk.card.CardList; importdk.card.Player; importdk.card.PlayerList; /** *@authorAdministrator * */ publicclassIndex05extendsShow{ publicstaticScannerscanner=newScanner(System.in); /** *调用的主方法 *@paramargs */ publicstaticvoidmain(String[]args){ show("是否开始游戏?0/1"); intisStart=scanner.nextInt(); if(isStart==1){ startGame(); } else{ show("退出游戏!"); } } //启动扑克游戏 publicstaticvoidstartGame(){ show("游戏规则如下:"); PlayerList.setRule("1.至少需要两名玩家"); PlayerList.setRule("2.所有玩家的牌数加起来不超过52张牌"); PlayerList.setRule("3.比较所有玩家手中最大的牌点,最大的牌点数的玩家获胜"); PlayerList.setRule("4.总的牌数只有52张,没有大小王"); PlayerList.setRule("5.黑桃牌A最大,方块3最小,点数相同时,比较花色,黑桃较大。"); PlayerList.printRules(); show("请输入玩家人数:"); intplayers=2;//默认玩家数目 intcardofplay=2;//默认每个玩家的手牌数目 try{ players=scanner.nextInt(); while(players<2){ show("玩家人数至少是两人"); show("请重新输入人数:"); players=scanner.nextInt(); } show("请输入每个玩家持有的牌数:"); cardofplay=scanner.nextInt(); while(cardofplay*players>52){ show("玩家*牌数超出范围!请重新输入牌数"); cardofplay=scanner.nextInt(); } show("开始输入玩家信息:"); for(inti=0;iShow.java(辅助显示类)
packagedk.main; publicabstractclassShow{ publicstaticvoidshow(Objectobj){ System.out.println(obj.toString()); } }实验结果:
是否开始游戏?0/1 1 游戏规则如下: 1.至少需要两名玩家 2.所有玩家的牌数加起来不超过52张牌 3.比较所有玩家手中最大的牌点,最大的牌点数的玩家获胜 4.总的牌数只有52张,没有大小王 5.黑桃牌A最大,方块3最小,点数相同时,比较花色,黑桃较大。 请输入玩家人数: 5 请输入每个玩家持有的牌数: 5(资质代办) 开始输入玩家信息: 请输入第1个玩家姓名: DekingChen 添加玩家:玩家Id:000玩家姓名:DekingChen ---------------------------------------- 请输入第2个玩家姓名: czq 添加玩家:玩家Id:001玩家姓名:czq ---------------------------------------- 请输入第3个玩家姓名: chenzhenquan 添加玩家:玩家Id:002玩家姓名:chenzhenquan ---------------------------------------- 请输入第4个玩家姓名: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。