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();
    }
}

 

posted on 2020-05-14 11:45  嘿抬头i  阅读(234)  评论(0编辑  收藏  举报