java模拟斗地主发牌看牌


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

public class Doudizhu {
	public static void main(String[]args){
		//存放扑克牌
		//牌类+对应花色
		HashMap<Integer, String> pooker = new HashMap<Integer,String>();
		//牌的编号
		ArrayList<Integer> pookerNum = new ArrayList<Integer>();
		//牌类
		String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
		
		String[] colors = {"黑","红","梅","方"};
		int index = 2;
		for(String num:nums){
			for(String color:colors){
				pooker.put(index, color+num);
				pookerNum.add(index);
				index++;
			}
		}
		pooker.put(0, "大王");
		pookerNum.add(0);
		pooker.put(1, "小王");
		pookerNum.add(1);
		//System.out.println(pooker);
		//System.out.println(pookerNum);
		//洗牌。打乱顺序
		Collections.shuffle(pookerNum);

		
		ArrayList<Integer> player1 = new ArrayList<Integer>();
		ArrayList<Integer> player2 = new ArrayList<Integer>();
		ArrayList<Integer> player3 = new ArrayList<Integer>();
		ArrayList<Integer> bottom = new ArrayList<Integer>();
		//模拟分牌
		for(int i=0;i<pookerNum.size();i++){
			if(i>pookerNum.size()-1-3){
				bottom.add(pookerNum.get(i));
			}else
			if(i%3==0){
				player1.add(pookerNum.get(i));
			}else
			if(i%3==1){
				player2.add(pookerNum.get(i));
			}else
			if(i%3==2){
				player3.add(pookerNum.get(i));
			}
		}
		
		//整理牌
		Collections.sort(player1);
		Collections.sort(player2);
		Collections.sort(player3);
		
		//看牌
		look("player1",player1,pooker);
		look("player2",player2,pooker);
		look("player3",player3,pooker);
		look("底           牌",bottom, pooker);
	}
	
	
	public static void look(String name,ArrayList<Integer> player,HashMap<Integer, String> pooker){
		System.out.print(name+": ");
		for(Integer key:player){
			String value = pooker.get(key);
			System.out.print(value+" ");
		}
		System.out.println("\n");
	}
}
posted @ 2017-08-18 16:42  17十七  阅读(195)  评论(0编辑  收藏  举报