营销网站 需求说明,泰安网约车,千助做网站怎么样,小米路由3g wordpress目录
前言#xff1a;
1.初始化牌 2.洗牌
3.揭牌
总代码#xff1a;
Card类#xff1a;
CardGame类#xff1a;
Main类#xff1a;
结语#xff1a; 前言#xff1a;
斗地主是全国范围内的一种桌面游戏#xff0c;本节我们来实现一下斗地主中的简单初始化牌、…目录
前言
1.初始化牌 2.洗牌
3.揭牌
总代码
Card类
CardGame类
Main类
结语 前言
斗地主是全国范围内的一种桌面游戏本节我们来实现一下斗地主中的简单初始化牌、发牌和看牌功能。按照斗地主的规则完成洗牌发牌的动作。具体规则为使用 52 张牌不含大小王打乱顺序3 个玩家参与游戏3 人交替摸牌每人 5可以自己修改 张牌。
实现思路步骤 1.初始化牌 2.洗牌 3.揭牌 4.剩余牌 效果展示 实现如下
1.初始化牌
牌有数字和花色故我们要自己创建一个类来进行操作。toString方法要记得重写不然后面println不能直接输出。
public class Card{public String suit;//花色public int num;public Card(String suit,int num){this.suit suit;this.num num;}Overridepublic String toString(){return suit num;}
}用suits来存储花色。ListCard可以看成是一个存储Card的一维数组。利用java自带的ArrayLIst的add函数实现尾插。
public static final String[] suits {♥,♣,♦,♠};//初始化牌public ListCard buyCard(){ListCard cardList new ArrayList();for(int i 0;i 4;i){for(int j 1;j 13;j){String suit suits[i];Card card new Card(suit,j);cardList.add(card);}}return cardList;} 效果如下 2.洗牌
利用random来生成随机数这里有个小细节i是从最后一个数来进行遍历的利用rand来生成i之前的随机数进行交换这样就能保证每个牌都有被洗到。
//洗牌public void shuffle(ListCard cardList){Random random new Random();for(int i cardList.size()-1;i 0;i--){swap(cardList,i,random.nextInt(i));}}public void swap(ListCard cardList,int i,int j){Card temp cardList.get(i);cardList.set(i,cardList.get(j));cardList.set(j,temp);}
效果如下 3.揭牌
这里解释一下ListListCard可以看成一个二维数组如图ListCard是一个泛型最外面的List里面存储了一个ListCard。 //揭牌public ListListCard getCard(ListCard cardList){ListCard hand1 new ArrayList();ListCard hand2 new ArrayList();ListCard hand3 new ArrayList();ListListCard hand new ArrayList();hand.add(hand1);hand.add(hand2);hand.add(hand3);for(int i 0;i 5;i){for(int j 0;j 3;j){Card card cardList.remove(0);hand.get(j).add(card);}}return hand;}
总代码
Card类
public class Card{public String suit;//花色public int num;public Card(String suit,int num){this.suit suit;this.num num;}Overridepublic String toString(){return suit num;}
}
CardGame类
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
public class CardGame{public static final String[] suits {♥,♣,♦,♠};//初始化牌public ListCard buyCard(){ListCard cardList new ArrayList();for(int i 0;i 4;i){for(int j 1;j 13;j){String suit suits[i];Card card new Card(suit,j);cardList.add(card);}}return cardList;}//洗牌public void shuffle(ListCard cardList){Random random new Random();for(int i cardList.size()-1;i 0;i--){swap(cardList,i,random.nextInt(i));}}public void swap(ListCard cardList,int i,int j){Card temp cardList.get(i);cardList.set(i,cardList.get(j));cardList.set(j,temp);}//揭牌public ListListCard getCard(ListCard cardList){ListCard hand1 new ArrayList();ListCard hand2 new ArrayList();ListCard hand3 new ArrayList();ListListCard hand new ArrayList();hand.add(hand1);hand.add(hand2);hand.add(hand3);for(int i 0;i 5;i){for(int j 0;j 3;j){Card card cardList.remove(0);hand.get(j).add(card);}}return hand;}
}
Main类
import java.util.List;
public class Main {public static void main(String[] args){CardGame cardgame new CardGame();ListCard ret cardgame.buyCard();System.out.println(初始化牌);System.out.println(ret.subList(0,13));System.out.println(ret.subList(13,26));System.out.println(ret.subList(26,39));System.out.println(ret.subList(39,52));System.out.println(洗牌);cardgame.shuffle(ret);System.out.println(ret.subList(0,13));System.out.println(ret.subList(13,26));System.out.println(ret.subList(26,39));System.out.println(ret.subList(39,52));System.out.println(揭牌);ListListCard hand cardgame.getCard(ret);for(int i 0;i 3;i){System.out.println(第 (i1)个人的牌:hand.get(i));}System.out.println(剩下的牌:);System.out.println(ret);}
}
结语
其实写博客不仅仅是为了教大家同时这也有利于我巩固自己的知识点和一个学习的总结由于作者水平有限对文章有任何问题的还请指出接受大家的批评让我改进如果大家有所收获的话还请不要吝啬你们的点赞收藏和关注这可以激励我写出更加优秀的文章。
文章转载自: http://www.morning.gbfuy28.cn.gov.cn.gbfuy28.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.lwhsp.cn.gov.cn.lwhsp.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.ykyfq.cn.gov.cn.ykyfq.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.pynzj.cn.gov.cn.pynzj.cn http://www.morning.wqmpd.cn.gov.cn.wqmpd.cn http://www.morning.tqbw.cn.gov.cn.tqbw.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.xtqr.cn.gov.cn.xtqr.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.dlgjdg.cn.gov.cn.dlgjdg.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.rbmnq.cn.gov.cn.rbmnq.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.fmrrr.cn.gov.cn.fmrrr.cn http://www.morning.kfbth.cn.gov.cn.kfbth.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.homayy.com.gov.cn.homayy.com http://www.morning.ltrz.cn.gov.cn.ltrz.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.dndk.cn.gov.cn.dndk.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.ylsxk.cn.gov.cn.ylsxk.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.xtlty.cn.gov.cn.xtlty.cn http://www.morning.wfjyn.cn.gov.cn.wfjyn.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.crrmg.cn.gov.cn.crrmg.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.gtmgl.cn.gov.cn.gtmgl.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.pqyms.cn.gov.cn.pqyms.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.dspqc.cn.gov.cn.dspqc.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.mxtjl.cn.gov.cn.mxtjl.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.jlrym.cn.gov.cn.jlrym.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn