Java集合框架(模拟斗地主洗牌发牌)

案例介绍

image.png

具体规则:

  • 组装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 pokerMap = getPoker();
        
        // 获取牌
        ArrayList cards = getCards();
        
        // 洗牌
        Collections.shuffle(cards);

        // 发牌
        ArrayList> poker = givePoker(cards);

        // 看牌
        lookCards(poker, pokerMap);
            
    }
    

    // 获取扑克牌的映射关系
    public static HashMap getPoker(){
        // 定义花色
        ArrayList color = new ArrayList();
        color.add("♠");
        color.add("♣");
        color.add("♥");
        color.add("♦");
        
        // 定义数
        ArrayList number = new ArrayList();
        Collections.addAll(number, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        
        HashMap pokerMap = new HashMap();
        
        // 添加大、小王
        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 getCards(){
        ArrayList cards = new ArrayList();
        for(int i = 0; i < 54; i++){
            cards.add(i);
        }
        return cards;
    }
    
    
    // 发牌
    public static ArrayList> givePoker(ArrayList cards) {
        ArrayList> poker = new ArrayList>();
        
        // 创建三个玩家和底牌
        ArrayList player = new ArrayList();
        ArrayList player2 = new ArrayList();
        ArrayList player3 = new ArrayList();
        ArrayList buttonCards = new ArrayList();
        
        //遍历这副洗好的牌,遍历过程中,将牌发到三个玩家和底牌中
        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> poker, HashMap pokerMap){
        
        ArrayList player = poker.get(0);
        ArrayList player2 = poker.get(1);
        ArrayList player3 = poker.get(2);
        ArrayList 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();
    }
}
image.png

你可能感兴趣的:(Java集合框架(模拟斗地主洗牌发牌))