Map實現鬥地主發牌有序版二

煙雨彷徨~~Xun發表於2020-11-04

在這裡插入圖片描述

package zuoye11;

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

public class DouDiZhu2 {
	//制牌

	public static void main(String[] args) {
		String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
		String[] color = {"紅桃","黑桃","梅花","方塊"};
		int m=0;
		List<Integer> list = new ArrayList<Integer>();
		Map<Integer, String> map = new HashMap<Integer, String>();
		for (String n : num) {//構建map 並存入key值 及 值對應的牌
			for (String c : color) {
				map.put(m, c+n);
				list.add(m);
				m++;
				
			}
			
		}
		map.put(m, "小王");
		list.add(m);
		map.put(m+1, "大王");
		list.add(m+1);
	
	/*	for (Map.Entry<Integer, String> en:map.entrySet()) {//遍歷map
			System.out.println(en.getKey()+" "+en.getValue());
			
		}
		*/
		
		//System.out.println(list);
		
		
		//洗牌
		Collections.shuffle(list);
		
		List<Integer> diPai = new ArrayList<Integer>();//構建底牌  玩家1 玩家2 玩家3的集合
		ArrayList<Integer> play1 = new ArrayList<Integer>();
		List<Integer> play2 = new ArrayList<Integer>();
		List<Integer> play3 = new ArrayList<Integer>();
		
		
		for (int i = 0; i < 3; i++) {
			diPai.add(list.remove(0));//給底牌發數字
			
		}
		
		System.out.println(diPai);
		
		while (true) {//給三個玩家發數字
			if (!list.isEmpty()) {
				play1.add(list.remove(0));
				
			}
			if (!list.isEmpty()) {
				play2.add(list.remove(0));
				
			}
			if (!list.isEmpty()) {
				play3.add(list.remove(0));
				
			}else {
				break;
			}
			
		}
		//排序
		Collections.sort(play1);//給三個玩家排序
		Collections.sort(play2);
		Collections.sort(play3);
		
		//呼叫方法
		System.out.println("底牌: "+fapai(map, diPai));
		System.out.println("Play1:"+fapai(map, play1));
		System.out.println("Play2:"+fapai(map, play2));
		System.out.println("Play3:"+fapai(map, play3));
		
		
		
	}
	
	
	
	public static String fapai(Map<Integer, String> map,List<Integer> list ) {
		//根據玩家的數字集合獲得map中匹配的牌
		String a ="";
		
		for(Integer s:list) {
			
			a += map.get(s)+" ";
			
		}
		
		
		return a;
		
	}

}

相關文章