扑克牌之斗地主的简单代码

斗地主的代码:

package demo8;

import java.util.*;

public class Test8 {
    public static void main(String[] args) {
        // 生成扑克
        HashMap map=new HashMap<>();      
        ArrayList picket=new ArrayList<>();    //  用来存键值,也是存扑克的张数
        String[] type=new String[]{"♦","♣","♥","♠"};
        String[] nums=new String[]{"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        int index=0;
        for (int i=0;i player1=new TreeSet<>(); // 玩家1
        TreeSet player2=new TreeSet<>(); // 玩家2
        TreeSet player3=new TreeSet<>(); // 玩家3
        TreeSet dipai=new TreeSet<>();   // 底牌
        // 发牌 ,发索引,根据索引找值,map
        for (int i=0;i=picket.size()-3){
                dipai.add(picket.get(i));
            }else if (i%3==0){
                player1.add(picket.get(i));
            }else if (i%3==1){
                player2.add(picket.get(i));
            }else if(i%3==2){
                player3.add(picket.get(i));
            }
        }
        //System.out.println(player1);//  输出一个玩家查看是否为17张牌
        seeCards("小明",player1,map);
        seeCards("小饭",player2,map);
        seeCards("小扣",player3,map);
        seeCards("底牌",dipai,map);

    }

    // 封装看牌方法,需要的形参为玩家名字,玩家的牌号(索引),牌的值(map)
     public static void seeCards(String name,TreeSet players, HashMap map){
         System.out.print(name+"的牌为:");
         Iterator iter=players.iterator();//  玩家的牌开始遍历
         while (iter.hasNext()){
             System.out.print(map.get(iter.next())+" ");// 得到玩家的牌值
         }
         System.out.println();
     }


}

灵活运用HashMap的键值对,键存张数,value存花色。

你可能感兴趣的:(斗地主)