案例介绍

具体规则:
- 组装54张扑克牌
- 将54张牌顺序打乱
- 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌
- 手中扑克牌从大到小的摆放顺序:大王, 小王, 2, A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class Poker {
public static void main(String[] args){
// 获取扑克牌的映射关系
HashMap<Integer, String> pokerMap = getPoker();
// 获取牌
ArrayList<Integer> cards = getCards();
// 洗牌
Collections.shuffle(cards);
// 发牌
ArrayList<ArrayList<Integer>> poker = givePoker(cards);
// 看牌
lookCards(poker, pokerMap);
}
// 获取扑克牌的映射关系
public static HashMap<Integer, String> getPoker(){
// 定义花色
ArrayList<String> color = new ArrayList<String>();
color.add("♠");
color.add("♣");
color.add("♥");
color.add("♦");
// 定义数
ArrayList<String> number = new ArrayList<String>();
Collections.addAll(number, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
HashMap<Integer, String> pokerMap = new HashMap<Integer, String>();
// 添加大、小王
pokerMap.put(0, "大王 ");
pokerMap.put(1, "小王 ");
int index = 2;
for (String theNumber: number){
for (String theColor: color){
pokerMap.put(index, theColor + theNumber + " ");
index++;
}
}
return pokerMap;
}
// 获取牌
public static ArrayList<Integer> getCards(){
ArrayList<Integer> cards = new ArrayList<Integer>();
for(int i = 0; i < 54; i++){
cards.add(i);
}
return cards;
}
// 发牌
public static ArrayList<ArrayList<Integer>> givePoker(ArrayList<Integer> cards) {
ArrayList<ArrayList<Integer>> poker = new ArrayList<ArrayList<Integer>>();
// 创建三个玩家和底牌
ArrayList<Integer> player = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> buttonCards = new ArrayList<Integer>();
//遍历这副洗好的牌,遍历过程中,将牌发到三个玩家和底牌中
for (int i = 0; i < cards.size(); i++){
if(i <= 50){
if (i % 3 == 0){
player.add(cards.get(i));
}else if(i % 3 == 1){
player2.add(cards.get(i));
}else{
player3.add(cards.get(i));
}
}else{
buttonCards.add(cards.get(i));
}
}
// 对每个人的手中的牌排序
Collections.sort(player);
Collections.sort(player2);
Collections.sort(player3);
poker.add(player);
poker.add(player2);
poker.add(player3);
poker.add(buttonCards);
return poker;
}
// 看牌
public static void lookCards(ArrayList<ArrayList<Integer>> poker, HashMap<Integer, String> pokerMap){
ArrayList<Integer> player = poker.get(0);
ArrayList<Integer> player2 = poker.get(1);
ArrayList<Integer> player3 = poker.get(2);
ArrayList<Integer> buttonCards = poker.get(3);
// 玩家1
System.out.print("玩家1: ");
for(Integer key: player){
String value = pokerMap.get(key);
System.out.print(value + " ");
}
System.out.println();
// 玩家2
System.out.print("玩家2: ");
for(Integer key: player2){
String value = pokerMap.get(key);
System.out.print(value + " ");
}
System.out.println();
// 玩家3
System.out.print("玩家3: ");
for(Integer key: player3){
String value = pokerMap.get(key);
System.out.print(value + " ");
}
System.out.println();
// 底牌
System.out.print("底牌");
for(Integer key: buttonCards){
String value = pokerMap.get(key);
System.out.print(value + " ");
}
System.out.println();
}
}

网友评论