Java 斗地主综合案例:有序版--小记

浮夸小生。
2021-09-08 / 0 评论 / 307 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年09月08日,已超过960天没有更新,若内容或图片失效,请留言反馈。

结果:
周润发:小王2♣A♦Q♦J♣10♠10♥9♥9♦8♠7♥6♦5♦5♣4♦3♥3♦
刘德华:大王2♠2♥2♦A♥K♠K♥K♦K♣J♦10♣8♦6♠6♣4♠3♠3♣
周星驰:A♠Q♠Q♥J♠J♥10♦9♠9♣8♥8♣7♦7♣6♥5♠5♥4♥4♣
底牌:A♣Q♣7♠

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

/*
1.准备牌
2.洗牌
3.发牌
4.排序
5.看牌
 */
public class Doudizhu {
    public static void main(String[] args) {
     //准备牌两个集合,一个存储牌一个存储牌的引索序列号。
        //创建一个Map集合,存储拍的引索和组装好的牌
        HashMap<Integer,String> poker = new HashMap<>();
        //创建一个List 集合 存储牌的引索序列号
        ArrayList<Integer> pokerIndex = new ArrayList<>();
        List<String> colors = List.of("♠", "♥", "♦", "♣");
        List<String> number = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");

        //首先添加大王,小王
        int index = 0;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;

        System.out.println(poker);
        System.out.println(pokerIndex);

        for (String numbers : number) {
            for (String color : colors) {
                poker.put(index,numbers + color);
                pokerIndex.add(index);
                index++;
            }
        }


        //2.洗牌 Collections shuffle(list) 方法 就行随机置放

        Collections.shuffle(pokerIndex);
        System.out.println(pokerIndex);

        //3.发牌 创建玩家 以及底牌

        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = 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){
                //给玩家一发牌
                player01.add(in);
            }else if (i % 3 ==1){
                //给玩家一发牌
                player02.add(in);
            }else if (i % 3 ==2){
                //给玩家一发牌
                player03.add(in);
            }
        }

        //4.排序 利用 Collections。sort(list) 方法 给牌排序

        Collections.sort(player01);
        Collections.sort(player02);
        Collections.sort(player03);
        Collections.sort(diPai);

        getLook("周润发",poker,player01);
        getLook("刘德华",poker,player02);
        getLook("周星驰",poker,player03);
        getLook("底牌",poker,diPai);

    }

    public static void getLook(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();
    }
}
0

评论 (0)

取消