斗地主简单版

案例分析:
1.准备牌: 54张牌存储到一个集合中
特殊牌:大王,小王
其他52张牌:

  • 定义一个数组/集合,存储种花色
  • 定义一个数组/集合,存储13个序号
    循环遍历两个数组/集合,组装52张牌

2.洗牌

  • 使用集合工具类Collections的方法
  • static void shuffle(List<?> list)使用指定的随机源对指定列表进行置换
  • 会随机的打乱集合中元素的顺序

3.发牌
要求:1人17张牌,剩余3张作为底牌,一人一张轮流发牌:集合的索引%3

  • 定义四个集合,存储三个玩家和底牌
  • 索引>=51改底牌发牌

4.看牌
直接打印集合,遍历存储玩家和底牌的集合

public class DouDiZhu {
    public static void main(String[] args) {
        //1.准备牌
        //定义一个存储54张牌的ArrayList集合
        ArrayList<String> poker=new ArrayList<>();
        //定义两个数组,一个数组存储牌的花色,一个数组存储牌的序号
        String[] colors={"♠","♥","♣","♦"};
        String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        //先把大王和小王存储到poker集合中
        poker.add("大王");
        poker.add("小王");
        for(String number:numbers){
            for(String color:colors){
                poker.add(color+number);
            }
        }
        //2.洗牌
        //static void shuffle(List<?> list)
        Collections.shuffle(poker);
        //3.发牌
        ArrayList<String> player1=new ArrayList<>();
        ArrayList<String> player2=new ArrayList<>();
        ArrayList<String> player3=new ArrayList<>();
        ArrayList<String> dipai=new ArrayList<>();

        for (int i = 0; i < poker.size(); i++) {
            String p=poker.get(i);
            if(i>=51){
                dipai.add(p);
            }else if(i%3==0){
                player1.add(p);
            }else if(i%3==1){
                player2.add(p);
            }else{
                player3.add(p);
            }
        }
        //4.看牌
        System.out.println("1"+player1);
        System.out.println("2"+player2);
        System.out.println("3"+player3);
        System.out.println("dipai"+dipai);

    }
}
发布了118 篇原创文章 · 获赞 12 · 访问量 2588

猜你喜欢

转载自blog.csdn.net/qq_42174669/article/details/104084184