使用ArrayList实现斗地主案例(无序版) | 您所在的位置:网站首页 › 斗地主打牌的顺序 › 使用ArrayList实现斗地主案例(无序版) |
案例介绍
按照斗地主的规则,完成洗牌发牌的动作。 具体规则: 使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。 案例分析1. 准备牌: 每张扑克牌牌由花色和数字两部分组成。可以使用花色集合与数字集合嵌套迭代完成扑克牌的组装。 2. 发牌 扑克牌组转完毕后由Collections类的shuffle方法打乱重排,最后3张当作底牌,剩余牌通过对3取模依次发牌。 3. 看牌 打印集合。 代码演示 import java.util.ArrayList; import java.util.Collections; /** * @author layman */ public class Poker { //牌堆 private static ArrayList pokerBox = new ArrayList(); //花色集合 private static ArrayList colors = new ArrayList(); //创建数字集合 private static ArrayList numbers = new ArrayList(); //三名玩家 private static ArrayList playerOne = new ArrayList(); private static ArrayList playerTwo = new ArrayList(); private static ArrayList playerThree = new ArrayList(); private static ArrayList diPai = new ArrayList(); public static void main(String[] args) { createPoker(); faPai(); showCards(); } /** * 创建扑克牌并洗牌 */ public static void createPoker(){ //4种花色 colors.add("♥"); colors.add("♦"); colors.add("♠"); colors.add("♣"); //13个数字 for(int i = 2;i for(String number : numbers){ String card = color+number; pokerBox.add(card); } } pokerBox.add("小王"); pokerBox.add("大王"); //洗牌(随机打乱扑克牌顺序) Collections.shuffle(pokerBox); } /** * 发牌 */ public static void faPai(){ //发牌 for(int i = 0;i //最后三张作为底牌 diPai.add(card); } else { if(i%3 == 0){ playerOne.add(card); }else if(i%3 == 1){ playerTwo.add(card); }else{ playerThree.add(card); } } } } /** * 看牌 */ public static void showCards(){ System.out.println("赌圣:" + playerOne); System.out.println("赌侠:" + playerTwo); System.out.println("赌王:" + playerThree); System.out.println("底牌:" + diPai); } }传送门: 使用HashMap实现斗地主案例(有序版) |
CopyRight 2018-2019 实验室设备网 版权所有 |