介绍
这篇文章给大家分享的是有关利用java如何实现扑克牌小游戏的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。
扑克牌小游戏:
游戏玩法简介:定义五个人们在玩扑克牌游戏,玩法是5个人从一副没有大小王的扑克牌中各抽取4张牌的类型有四种:?,?,?,?,牌的大小为从1 ~ 13。(此处为了简单,只取1 - 5)。
1,黑桃吗?一个在哪名玩家手中,哪名玩家就获胜。
包扑克; 进口java.util.ArrayList; 进口java.util.Collections; 进口并不知道; 进口java.util.Random; 进口java.util.concurrent.Callable; 公共类游戏{ 公共静态void main (String [] args) {//定义5名玩家,使用列表 List,playerList=new ArrayList<的在(); playerList。添加(新球员(“于美人“)); playerList。添加(新球员(“小婷子“)); playerList。添加(新球员(“小晨子“)); playerList。添加(新球员(“小艾”)); playerList。添加(新球员(“小珍子“));//定义扑克牌顺序表 List ,cardList=new ArrayList<的在();//初始化扑克牌 initializeCards (cardList); System.out.println (cardList);//调用集合下的洗牌(洗牌)方法//进行洗牌 Collections.shuffle (cardList);//System.out.println(“抽牌前,牌组中的牌:“); System.out.println (cardList);//发牌 int n=2;//每名玩家发几张牌 for (int i=0; i 卡){ (字符串套装:新String [] {“?“,“?“,“?“,“?“}) { (int排名=1;rank<=5;等级+ +){ 卡牌=新卡(西装、排名);//把扑克牌放入牌组中 cards.add(卡); } } }
2,让每名玩家,依次抽取他的下家的一张手牌,然后再找?,找到吗?一个就获胜。
//添加打印手牌的方法 公共静态孔隙printPlayerHandCard (ListplayerList) { (球员球员:playerList) { System.out.printf(“玩家(% s)的手牌是:% s % n", player.name, player.cardList); } }//集体实现交换的过程//交换牌//每名玩家随机抽取下家的一张牌 随机随机=new随机(); for (int i=0; i< playerList.size();我+ +){ 球员currentPlayer=playerList.get(我);//下家就是按照前后关系的下一个,最后一名玩家抽取第0个 球员nextPlayer=playerList.get(我!=playerList.size () 1 ? i + 1:0);//要取的牌的下标 int toDrawIndex=random.nextInt (nextPlayer.cardList.size ());//取牌 卡卖座的节目=nextPlayer.cardList.remove (toDrawIndex);//放入当前玩家的手中 currentPlayer.cardList.add(卖座的节目); } System.out.println(“交换牌之后:“); printPlayerHandCard (playerList);
3,于美人是赌神,她有变牌能力:她手中一旦没有?,她就可以把第一张牌变成吗?(交换牌之前,有机会变一次,交换牌之后,有机会变一次)
卡toFoundCard=新卡(“?“,1);//已知于美人的下标为0 球员于美人=playerList.get (0); 如果(!于美人.cardList.contains (toFoundCard)) { 于美人.cardList.set (0, toFoundCard); } System.out.println(“于美人第一次发功:“); printPlayerHandCard (playerList); System.out.println(“交换牌之后:“); printPlayerHandCard (playerList);//此处交换牌过程省略.............. 如果(!于美人.cardList.contains (toFoundCard)) { 于美人.cardList.set (0, toFoundCard); } System.out.println(“于美人第二次发功:“); printPlayerHandCard (playerList);利用java如何实现扑克牌小游戏