用java实现斗地主发牌过程
本文实例为大家分享了java实现斗地主发牌过程的具体代码,供大家参考,具体内容如下
一、牌类实现
定义牌的相关属性,点数、花色、大小
packagecom.softeem.lesson18.doudizhu; importjava.util.Objects; publicclassCard{ //牌面值 privateStringname; //花色 privateStringflower; //点数 privateintnum; publicCard(){ //TODOAuto-generatedconstructorstub } publicCard(Stringname,Stringflower,intnum){ super(); this.name=name; this.flower=flower; this.num=num; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetFlower(){ returnflower; } publicvoidsetFlower(Stringflower){ this.flower=flower; } publicintgetNum(){ returnnum; } publicvoidsetNum(intnum){ this.num=num; } @Override publicStringtoString(){ if(Objects.nonNull(flower)){ returnname+"-"+num+"-"+flower; } returnname+"-"+num; } }
二、玩家类
玩家类中定义了万家的相关属性,名字,ID,还定义了一个牌类数组,将每位玩家的牌加入到数组中。
packagecom.softeem.lesson18.doudizhu; importjava.util.ArrayList; publicclassPlayer{ privateintid; privateStringname; privatebooleanboss; privateArrayListcards=newArrayList<>(); publicPlayer(){ } publicPlayer(intid,Stringname,booleanboss,ArrayList cards){ super(); this.id=id; this.name=name; this.boss=boss; this.cards=cards; } publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicbooleanisBoss(){ returnboss; } publicvoidsetBoss(booleanboss){ this.boss=boss; } publicArrayList getCards(){ returncards; } publicvoidsetCards(ArrayList cards){ this.cards=cards; } @Override publicStringtoString(){ returnname+(boss?"(地主)":"")+cards; } }
三、功能类
功能类中实现了牌的分发,每人17张牌,然后地主多三张牌的实现,以及对玩家进行随机分配地主等功能。
packagecom.softeem.lesson18.doudizhu; importjava.util.ArrayList; importjava.util.Random; importjava.util.Scanner; publicclassGameManage{ privatestaticArrayListall=newArrayList<>(); privatestaticString[]names={"3","4","5","6","7","8","9","10","J","Q","K","A","2","Queen","King"}; privatestaticString[]flowers={"红桃","黑桃","方块","梅花"}; privateArrayList players=newArrayList (); privatestaticRandomrandomGen=newRandom(); static{ //初始化所有牌 inti=0; for(;i 更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏玩不停
java经典小游戏汇总
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。