斗地主(有序版本)

1.准备牌
特殊牌:大王,小王
52张牌:循环嵌套遍历两个集合/数组,组装52张牌

 String[] colors={"♠","♥","♣","♦"};
 String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
  • 创建一个map集合,存储所有牌
  • 创建一个List集合,存储牌的索引

2.洗牌
使用Collections中的shuffle(List)方法

3.发牌
一人一张轮流发牌,没人17张
集合索引%3
剩余3张给底牌

4.排序
使用Collections中的sort方法
sort(List)

5.看牌
遍历一个集合,获取到另一个集合的Key
遍历key查找到value
遍历玩家和底牌的List集合,获取到map集合的可以,通过key找到value值

public class DouDiZhu {
    public static void main(String[] args) {
        //1.准备牌
        //创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer,String> poker=new HashMap<>();
        ArrayList<Integer> pokerIndex=new ArrayList<>();
        String[] colors={"♠","♥","♣","♦"};
        String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
        int index=0;
        poker.put(index,"大王");
        pokerIndex.add(index++);
        poker.put(index,"小王");
        pokerIndex.add(index++);
        for(String number:numbers){
            for(String color:colors){
                poker.put(index,color+number);
                pokerIndex.add(index++);
            }
        }
        //2.洗牌
        Collections.shuffle(pokerIndex);
        //3.发牌
        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);
            }else if(i%3==1){
                player2.add(in);
            }else {
                player3.add(in);
            }

        }
        //4.排序
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
        Collections.sort(dipai);

        lookpoker("玩家一",poker,player1);
    }
    //5.看牌
    public static void lookpoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        System.out.print(name+":");
        for(Integer key:list){
            String value=poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();
    }
}

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

猜你喜欢

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