Java語言模拟鬥地主發牌
文章目錄
- Java語言模拟鬥地主發牌
-
- 1. 程式意義
- 2. 需求
- 3. 分析
- 4. 程式編寫過程
- 5. 總結
1. 程式意義
幫助了解和掌握與集合相關的知識,包含的知識點有:數組、List集合、Map集合、、泛型、接口、多态、疊代器等基礎知識。
2. 需求
模拟撲克牌鬥地主的洗牌、發牌和看牌操作。
3. 分析
1. 普通牌52張,每個面值有4種花色。
2. 特殊牌:王牌2張,大王和小王。
3. 涉及到的知識
* Map集合:用于存放牌組,key 為每張牌的編号,value 為具體的牌(由面值和花色組成)
* List集合:用于存放每張牌的編号,便于洗牌和發牌操作。
* 疊代器:用于将建立的牌放入集合當中
4. 主要涉及到的方法
* put(K key, V value):将牌的編号和映射牌的屬性存入 Map 集合。
* add(E e):将牌的編号存入List集合中。
* shuffle(List<?> list):随機排列List清單,模拟洗牌。
* get(int index):擷取指定元素,輔助 add(E e) 模拟發牌操作。
* sort(List list):排序,模拟 調順序操作。
4. 程式編寫過程
import java.util.*;
/**
* 模拟鬥地主發牌,看牌
*/
public class Test {
public static void main(String[] args) {
/**
* 建立牌庫
*/
//撲克牌的面值和花色分别用兩個 String 型的數組存放。
String[] colors_poker = {"♥", "♠", "♦", "♣"};
String[] value_poker = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
// 牌庫用一個 Map 集合表示,key為牌的編号,value為牌的面值和花色。
Map<Integer, String> pokers = new HashMap<>();
// 定義牌的鍵 key_poker,用于聯系編号和具體的一張牌,以key:value的方式存入Map集合
int key_poker = 1;
// 牌的序号用一個List 集合表來表示。
List<Integer> id_poker = new ArrayList<>();
// 将牌的序号周遊存入List 集合,将鍵和牌對應存入Map
for (String value : value_poker) {
for (String color : colors_poker) {
//組合一張具體的牌,面值+花色
String poker = value + color;
//将牌的編号id 放入 List集合
id_poker.add(key_poker);
//組合好的牌放入 牌庫 Map
pokers.put(key_poker, poker);
//讓序号值自增1,進入下次存牌操作
key_poker++;
}
}
// 手動添加大小王到牌庫
pokers.put(key_poker, "小王");
id_poker.add(key_poker++);
pokers.put(key_poker, "大王");
id_poker.add(key_poker);
// 檢視牌庫情況
System.out.println("牌庫:" + pokers);
System.out.println("牌的編号:" + id_poker);
System.out.println("牌的數量:" + key_poker);
System.out.println("------------------分割線------------------");
/**
* 洗牌
*/
// 用shuffle()方法将牌的編号數組 id_poker 随機排序,模仿洗牌操作。
Collections.shuffle(id_poker);
//檢視洗牌過後的牌庫編号
System.out.println(id_poker);
System.out.println("------------------分割線------------------");
/**
* 發牌
*/
//定義 3個List用于存放3個玩家配置設定到的牌。
List<Integer> p1 = new ArrayList<>();
List<Integer> p2 = new ArrayList<>();
List<Integer> p3 = new ArrayList<>();
//定義 1個List集合,用于存放最後的三張底牌。
List<Integer> bottom_poker = new ArrayList<>();
//配置設定牌到3個集合中
for (int i = 0; i < id_poker.size(); i++) {
//周遊方式擷取牌的編号
Integer num_poker = id_poker.get(i);
if (i >= id_poker.size() - 3) {//配置設定底牌,剩下的三張
bottom_poker.add(num_poker);
} else if (i % 3 == 0) {//配置設定玩家1
p1.add(num_poker);
} else if (i % 3 == 1) {//配置設定玩家2
p2.add(num_poker);
} else if (i % 3 == 2) {//配置設定玩家3
p3.add(num_poker);
}
}
// 檢視4個集合中的配置設定情況
System.out.println("P1:" + p1);
System.out.println("P2:" + p2);
System.out.println("P3:" + p3);
System.out.println("bottom_poker:" + bottom_poker);
System.out.println("------------------分割線------------------");
//定義一個方法将4個集合中排的編号變成具體的牌
//列印最終結果
System.out.println("P1:" + alterPoker(p1, pokers));
System.out.println("P2:" + alterPoker(p2, pokers));
System.out.println("P3:" + alterPoker(p3, pokers));
System.out.println("bottom_poker:" + alterPoker(bottom_poker, pokers));
}
/**
* 根據編号ID擷取對應的牌
*
* @param id_poker :牌編号集合
* @param pokers :牌庫集合
* @return id_alter :将編号id_poker 替換成 對應的牌 的 List 集合
*/
public static List<String> alterPoker(List<Integer> id_poker, Map<Integer, String> pokers) {
//1.對傳入的編号進行升序排序
Collections.sort(id_poker);
//2.建立儲存換牌結果的List集合
List<String> id_alter = new ArrayList<>();
//3.周遊傳入的牌編号集合,儲存到id_alter集合
for (Integer id : id_poker) {
//3.1 根據周遊得到的id 編号擷取牌庫pokers中對應的牌
String poker = pokers.get(id);
//3.2 将換好的牌存入id_alter集合
id_alter.add(poker + " ");
}
//4. 傳回換好牌的集合 id_alter
return id_alter;
}
}
運作結果:
牌庫:{1=3♥, 2=3♠, 3=3♦, 4=3♣, 5=4♥, 6=4♠, 7=4♦, 8=4♣, 9=5♥, 10=5♠, 11=5♦, 12=5♣, 13=6♥, 14=6♠, 15=6♦, 16=6♣, 17=7♥, 18=7♠, 19=7♦, 20=7♣, 21=8♥, 22=8♠, 23=8♦, 24=8♣, 25=9♥, 26=9♠, 27=9♦, 28=9♣, 29=10♥, 30=10♠, 31=10♦, 32=10♣, 33=J♥, 34=J♠, 35=J♦, 36=J♣, 37=Q♥, 38=Q♠, 39=Q♦, 40=Q♣, 41=K♥, 42=K♠, 43=K♦, 44=K♣, 45=A♥, 46=A♠, 47=A♦, 48=A♣, 49=2♥, 50=2♠, 51=2♦, 52=2♣, 53=小王, 54=大王}
牌的編号:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
牌的數量:54
------------------分割線------------------
洗過牌後的編号庫:[7, 44, 14, 20, 41, 30, 49, 53, 32, 2, 54, 5, 21, 3, 18, 15, 52, 51, 11, 29, 50, 35, 10, 34, 22, 16, 13, 43, 42, 28, 19, 24, 39, 48, 6, 4, 40, 33, 12, 26, 36, 27, 23, 31, 38, 8, 47, 1, 37, 9, 25, 17, 46, 45]
------------------分割線------------------
P1:[7, 20, 49, 2, 21, 15, 11, 35, 22, 43, 19, 48, 40, 26, 23, 8, 37]
P2:[44, 41, 53, 54, 3, 52, 29, 10, 16, 42, 24, 6, 33, 36, 31, 47, 9]
P3:[14, 30, 32, 5, 18, 51, 50, 34, 13, 28, 39, 4, 12, 27, 38, 1, 25]
bottom_poker:[17, 46, 45]
------------------分割線------------------
P1:[3♠ , 4♦ , 4♣ , 5♦ , 6♦ , 7♦ , 7♣ , 8♥ , 8♠ , 8♦ , 9♠ , J♦ , Q♥ , Q♣ , K♦ , A♣ , 2♥ ]
P2:[3♦ , 4♠ , 5♥ , 5♠ , 6♣ , 8♣ , 10♥ , 10♦ , J♥ , J♣ , K♥ , K♠ , K♣ , A♦ , 2♣ , 小王 , 大王 ]
P3:[3♥ , 3♣ , 4♥ , 5♣ , 6♥ , 6♠ , 7♠ , 9♥ , 9♦ , 9♣ , 10♠ , 10♣ , J♠ , Q♠ , Q♦ , 2♠ , 2♦ ]
bottom_poker:[7♥ , A♥ , A♠ ]
5. 總結
- 這個程式中旨在加深對List、Map集合的了解。
- 程式中的 id_poker 和 key_poker 是一一對應的關系,就相當于資料庫中兩張表的外鍵限制一般,一個 id_poker 可以唯一确定 一張牌,一個 key_poker 也可以唯一确定一張牌,但是可以通過 id_poker 來通路 key_poker 對應的 value。
時間:2019年10月28日00:18:48