模拟斗地主(发牌有序和无序)

1、模拟斗地主(发牌无序)

package com.rookie.cards;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by RookieLi on 2019/4/18.
 */
public class PlayCards2 {
    public static void main(String[] args) {
        //牌盒
        List<String> pokerBox = new ArrayList<>();

        //花色和数字
        List<String> colors = new ArrayList<>();
        List<String> numbers = new ArrayList<>();

        // 添加花色和数字
        colors.add("♥");
        colors.add("♦");
        colors.add("♠");
        colors.add("♣");

        for (int i = 2; i <= 10; i++) {
            numbers.add(i + "");
        }
        numbers.add("J");
        numbers.add("Q");
        numbers.add("K");
        numbers.add("A");

        //花色和数字结合放进牌盒子
        for (String color : colors) {
            for (String numuber : numbers) {
                String card = color + numuber;
                pokerBox.add(card);
            }
        }
        pokerBox.add("小☺");
        pokerBox.add("大☠");

        //洗牌
        Collections.shuffle(pokerBox);

        // 创建3个人玩牌
        List<String> player1 = new ArrayList<>();
        List<String> player2 = new ArrayList<>();
        List<String> player3 = new ArrayList<>();
        ArrayList<String> dipai = new ArrayList<>();

        // 发牌
        for (int i = 0; i < pokerBox.size(); i++) {
            //获取牌的索引
            String card = pokerBox.get(i);
            if (i >= 51) {
                dipai.add(card);
            } else {
                if (i % 3 == 0) {
                    player1.add(card);
                } else if (i % 3 == 1) {
                    player2.add(card);
                } else if (i % 3 == 2) {
                    player3.add(card);
                }
            }
        }

        //看牌
        System.out.println("令狐冲:"+player1);
        System.out.println("任盈盈:"+player2);
        System.out.println("岳不群:"+player3);
        System.out.println("底牌:"+dipai);
    }
}

2、模拟斗地主(发牌有序)

package com.rookie.cards;

import java.util.*;

/**
 * Created by RookieLi on 2019/4/18.
 */
public class PlayCards3 {
    public static void main(String[] args) {

        // 创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer, String> pokerMap = new HashMap<>();

        // 存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();

        // 存储花色集合和数字集合
        ArrayList<String> colors = new ArrayList<>();
        ArrayList<String> numbers = new ArrayList<>();

        Collections.addAll(colors, "♦", "♣", "♥", "♠");
        Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");

        int index = 0;
        pokerMap.put(index, "大王");
        pokerIndex.add(index);
        index++;
        pokerMap.put(index, "小王");
        pokerIndex.add(index);
        index++;

        // 组装52张牌,存储到集合中
        for (String number : numbers) {
            for (String color : colors) {
                pokerMap.put(index, color + number);
                pokerIndex.add(index);
                index++;
            }
        }

        // 洗牌
        Collections.shuffle(pokerIndex);

        // 定义玩家,进行发牌
        ArrayList<Integer> player1 = new ArrayList<>();
        ArrayList<Integer> player2 = new ArrayList<>();
        ArrayList<Integer> player3 = new ArrayList<>();
        ArrayList<Integer> diPai = new ArrayList<>();

        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer in = pokerIndex.get(i);
            //先判断底牌
            if (i >= 51) {
                diPai.add(in);// 给底牌发牌
            } else if (i % 3 == 0) {
                player1.add(in);// 给玩家1发牌
            } else if (i % 3 == 1) {
                player2.add(in); // 给玩家2发牌
            } else if (i % 3 == 2) {
                player3.add(in); // 给玩家3发牌
            }
        }

        // 玩家手中的牌进行排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(diPai);

        // 查看玩家手中的牌
        lookPoker("刘德华",pokerMap,player1);
        lookPoker("周润发",pokerMap,player2);
        lookPoker("周星驰",pokerMap,player3);
        lookPoker("底牌",pokerMap,diPai);
    }

    // 查看玩家牌的方法
    public static void lookPoker(String name, HashMap<Integer, String> poker,ArrayList<Integer> list) {
        System.out.print(name+":");
        //遍历玩家或者底牌集合,获取牌的索引
        for (Integer key : list) {
            //使用牌的索引,去Map集合中,找到对应的牌
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();//打印完每一个玩家的牌,换行
    }
}

猜你喜欢

转载自www.cnblogs.com/miantiao312/p/10733314.html
今日推荐