Java 簡單實現鬥地主建立牌組與隨機發牌、看牌

 今天閒着無聊,用基礎的隨機數,寫一個簡單的鬥地主發牌,給須要用到的同窗作一個參考。java

package com.linlx.Individual.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class GameTest {

	public static void main(String[] args) {
		GameTest a = new GameTest();
		a.createCards();
	}
	
	/**
	 * createCards(建立牌池) 
	 * void
	 * @exception 
	 * @since  1.0.0
	*/
	public void createCards(){
		//建立牌池集合
		List<String> cardsList = new ArrayList<>();
		
		//生成各個花色牌組
		for (int i = 0; i <= 4; i++) {
			switch (i) {
				case 0:
					addCardsList(":spades:",13,cardsList);
					break;
				case 1:
					addCardsList(":clubs:",13,cardsList);
					break;
				case 2:
					addCardsList(":hearts:",13,cardsList);
					break;
				case 3:
					addCardsList("◇",13,cardsList);
					break;
				default:
					cardsList.add("大王");
					cardsList.add("小王");
					break;
			}
		}
		
		/**
		 * 如下部分爲顯示牌組,可要可不要
		 * */
		System.out.println("牌組長度:"+cardsList.size());
		Iterator<String> it = cardsList.iterator();
		System.out.print("牌池:");
		while (it.hasNext()) {
			System.out.print(it.next()+" ");
		}
		
		//調用發牌方法
		giveOutCards(cardsList);
	}
	
	/**
	 * addCardsList(生成各個花色牌組)
	 * @param key
	 * @param len
	 * @param cardsList 
	 * void
	 * @exception 
	 * @since  1.0.0
	*/
	public void addCardsList(String key,int len,List<String> cardsList){
		//開始生成花色牌組,並判斷生成A,J,Q,K等特殊牌組
		for (int j = 0; j < len; j++) {
			switch (j) {
			case 0:
				cardsList.add(key+"A");
				break;
			case 10:
				cardsList.add(key+"J");
				break;
			case 11:
				cardsList.add(key+"Q");
				break;
			case 12:
				cardsList.add(key+"K");
				break;
			default:
				cardsList.add(key+(j+1));
				break;
			}
		}
	}
	
	/**
	 * giveOutCards(發牌)
	 * @param cardsList 
	 * void
	 * @exception 
	 * @since  1.0.0
	*/
	public void giveOutCards(List<String> cardsList){
		//建立玩家牌池集合
		List<Set<String>> gamePlayerCards = new ArrayList<>();
		
		//循環出三個玩家牌組
		for (int i = 0; i < 3; i++) {
			//建立玩家牌組
			Set<String> setCar = new HashSet<>();
			//開始循環無法發牌十七張
			for (int j = 0; j < 17; j++) {
				boolean flag = false;
				while (flag == false) {
					//生成一個從54-1的隨機數,並減1,稍後做爲獲取牌池集合的下標
					int index = (int)Math.round(Math.random()*cardsList.size()+1)-1;
					//判斷該次想要獲取的牌是不是是已被髮出
					if(index < cardsList.size()&&cardsList.get(index)!=null){
						//將拿到的牌加入至玩家牌組
						setCar.add(cardsList.get(index));
						//刪除牌池中的牌
						cardsList.remove(index);
						//賦值,跳出循環
						flag = true;
					}
				}
			}
			
			//添加至玩家牌池
			gamePlayerCards.add(setCar);
		}
		
		/**
		 * 如下部分爲顯示牌組,可要可不要
		 * */
		System.out.print("\n地主牌:");
		for (String card : cardsList) {
			System.out.print(card+" ");
		}
		System.out.println();
		for (Set<String> cards : gamePlayerCards) {
			Iterator<String> it = cards.iterator();
			while (it.hasNext()) {
				System.out.print(it.next()+" ");
			}
			System.out.println(" 牌組長度:"+cards.size());
		}
	}

}

輸出:dom

牌池::spades:A :spades:2 :spades:3 :spades:4 :spades:5 :spades:6 :spades:7 :spades:8 :spades:9 :spades:10 :spades:J :spades:Q :spades:K :clubs:A :clubs:2 :clubs:3 :clubs:4 :clubs:5 :clubs:6 :clubs:7 :clubs:8 :clubs:9 :clubs:10 :clubs:J :clubs:Q :clubs:K :hearts:A :hearts:2 :hearts:3 :hearts:4 :hearts:5 :hearts:6 :hearts:7 :hearts:8 :hearts:9 :hearts:10 :hearts:J :hearts:Q :hearts:K ◇A ◇2 ◇3 ◇4 ◇5 ◇6 ◇7 ◇8 ◇9 ◇10 ◇J ◇Q ◇K 大王 小王 
地主牌::spades:Q :clubs:5 ◇10 
:hearts:J ◇4 :clubs:2 :clubs:K ◇Q :spades:J :spades:10 ◇2 :hearts:9 :clubs:8 :spades:6 ◇7 :hearts:5 :hearts:7 :spades:3 :hearts:A :spades:8  牌組長度:17
:clubs:J :spades:A ◇J :hearts:Q :hearts:2 :clubs:Q :hearts:3 ◇6 :spades:K :spades:4 :clubs:7 ◇8 :clubs:4 :spades:2 :clubs:6 :clubs:A :spades:9  牌組長度:17
◇K :hearts:K ◇3 ◇5 :hearts:4 大王 :spades:5 :clubs:9 :clubs:10 :spades:7 :hearts:10 :clubs:3 :hearts:6 ◇9 :hearts:8 小王 ◇A  牌組長度:17spa

相關文章
相關標籤/搜索