簡單鬥地主

/*
   * 一副撲克
   * 洗牌
   * 發牌
   *看牌
   */
	public static void fightLandlord(){
		//一副撲克
		String[] num={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
		String[] color={"梅花","紅桃","方片","黑桃"};
		HashMap<Integer, String> hm=new HashMap<Integer, String>();     //存儲索引和撲克
		ArrayList<Integer> list=new ArrayList<Integer>();                      //存儲索引
		int index=0;
		for(String n:num){
			for(String c:color){
				String p=c.concat(n);
				hm.put(index, p);
				list.add(index);
				index++;	
			}
		}
		list.add(index);
		hm.put(index, "小王");
		index++;
		list.add(index);
		hm.put(index, "大王");
      //洗牌
	    Collections.shuffle(list);
	    TreeSet<Integer> landlord=new TreeSet<Integer>();
	    TreeSet<Integer> peasant1=new TreeSet<Integer>();
	    TreeSet<Integer> peasant2=new TreeSet<Integer>();
	    TreeSet<Integer> extra=new TreeSet<Integer>();
	   //發牌
	    for(int i=0;i<list.size();i++){
	    	if(i>=list.size()-3){
	    		extra.add(list.get(i));
	    	}else if(i % 3 ==0){
	    		landlord.add(list.get(i));
	    	}else if(i%3==1){
	    		peasant1.add(list.get(i));
	    	}else{
	    		peasant2.add(list.get(i));
	    	}
	    }
	    //看牌
	    seePoker(hm,landlord,"地主");
	    seePoker(hm,peasant1,"農民1");
	    seePoker(hm,peasant2,"農民2");
	    seePoker(hm,extra,"底牌");
	    
	}
     public static void seePoker(HashMap<Integer, String> hm,TreeSet<Integer> p,String name){
    	 System.out.print(name+"的牌是:");
    	 for(Integer i:p){
    		 System.out.print(hm.get(i)+" ");
    	 }
    	 System.out.println();
     }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章