zoukankan      html  css  js  c++  java
  • Java模拟斗地主

    模拟斗地主:

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.Map;
    
    public class DouDiZhu {
    
        public static void main(String[] args) {
            //创建装有扑克牌的Map集合
            Map<Integer,String> pooker=new HashMap<Integer,String>();
            //创建牌号集合
            ArrayList<Integer> pookerNumber=new ArrayList<Integer>();
            //封装数据
            String[] color={"","","",""};
            String[] number={"2","1","K","Q","J","10","9","8","7","6","5","4","3",};
            //下标
            int index=2;
            for(String num:number){
                for(String c:color){
                    pooker.put(index, c+num);
                    pookerNumber.add(index);
                    index++;
                }
            }
            //封装大小王
            pooker.put(0, "大王");
            pookerNumber.add(0);
            pooker.put(1, "小王");
            pookerNumber.add(1);
            //洗牌
            Collections.shuffle(pookerNumber);
            System.out.println(pookerNumber);
            //发牌
            ArrayList<Integer> bottom=new ArrayList<Integer>();
            ArrayList<Integer> player1=new ArrayList<Integer>();
            ArrayList<Integer> player2=new ArrayList<Integer>();
            ArrayList<Integer> player3=new ArrayList<Integer>();
            //遍历发牌
            for(int i=0;i<pookerNumber.size();i++){
                //先发底牌
                if(i<3){
                    bottom.add(pookerNumber.get(i));
                }else if(i%3==0){
                    player1.add(pookerNumber.get(i));
                }else if(i%3==1){
                    player2.add(pookerNumber.get(i));
                }else if(i%3==2){
                    player3.add(pookerNumber.get(i));
                }
            }
            //排序
            Collections.sort(bottom);
            Collections.sort(player1);
            Collections.sort(player2);
            Collections.sort(player3);
            //看牌
            look("A",pooker,player1);
            look("B",pooker,player2);
            look("C",pooker,player3);
            look("底牌",pooker,bottom);
        
        }
       public static void look(String name,Map<Integer,String> pooker,ArrayList<Integer> player){
            System.out.print(name+":");
            for(int i:player){
                System.out.print(pooker.get(i)+"  ");
            }
            System.out.println();
        }
    }
  • 相关阅读:
    【转】XP下安装IIS6.0的办法
    设计模式学习笔记十:单例模式(Singleton Pattern)
    linq学习笔记(2):DataContext
    设计模式学习笔记十二:桥接模式(Bridge Pattern)
    英文版c#数据结构
    linq学习笔记(3):Where
    一步步学习WCF(1):Hello
    linq学习笔记(5):Count/Sum/Min/Max/Avg
    [转]Asp.Net 面试题目收集
    从谷歌公司发现的十个至理名言
  • 原文地址:https://www.cnblogs.com/heitaitou/p/12882317.html
Copyright © 2011-2022 走看看