Use ArrayList to realize the case of the landlord (disordered version)

Case Introduction

According to the rules of Doudizhu, complete the action of shuffling the cards. Specific rules:

Use 54 cards to shuffle the order. Three players participate in the game. The three draw cards alternately, each with 17 cards, and the last three are reserved as hole cards.

case study

1. Prepare cards:

Each poker card consists of two parts: suit and number. The assembling of playing cards can be completed by nesting iterative set of suit and set of numbers.

2. Licensing

After the poker group is transferred, the shuffle method of the Collections class is used to shuffle and rearrange. The last 3 cards are used as the hole cards, and the remaining cards are dealt sequentially by modulating the 3.

3. Look at the cards

Print the collection.

Code demo

import java.util.ArrayList;
import java.util.Collections;

/**
 * @author layman
 */
public class Poker {
    
    
    //牌堆
    private static ArrayList<String> pokerBox = new ArrayList<>();
    //花色集合
    private static ArrayList<String> colors = new ArrayList<>();
    //创建数字集合
    private static ArrayList<String> numbers = new ArrayList<>();
    //三名玩家
    private static ArrayList<String> playerOne = new ArrayList<>();
    private static ArrayList<String> playerTwo = new ArrayList<>();
    private static ArrayList<String> playerThree = new ArrayList<>();
    private static ArrayList<String> diPai = new ArrayList<>();

    public static void main(String[] args) {
    
    
        createPoker();
        faPai();
        showCards();
    }
    /**
    * 创建扑克牌并洗牌
    */
    public static void createPoker(){
    
    
        //4种花色
        colors.add("♥");
        colors.add("♦");
        colors.add("♠");
        colors.add("♣");
        //13个数字
        for(int i = 2;i <= 10;i++){
    
    
            numbers.add(i+"");
        }
        numbers.add("J");
        numbers.add("Q");
        numbers.add("K");
        numbers.add("A");
        //生成扑克牌
        for (String color : colors) {
    
    
            for(String number : numbers){
    
    
                String card = color+number;
                pokerBox.add(card);
            }
        }
        pokerBox.add("小王");
        pokerBox.add("大王");
        //洗牌(随机打乱扑克牌顺序)
        Collections.shuffle(pokerBox);
    }
    /**
     * 发牌
     */
    public static void faPai(){
    
    
        //发牌
        for(int i = 0;i < pokerBox.size();i++){
    
    
            String card = pokerBox.get(i);
            if(i >= 51){
    
    
                //最后三张作为底牌
                diPai.add(card);
            } else {
    
    
                if(i%3 == 0){
    
    
                    playerOne.add(card);
                }else if(i%3 == 1){
    
    
                    playerTwo.add(card);
                }else{
    
    
                    playerThree.add(card);
                }
            }
        }
    }
    /**
     * 看牌
     */
    public static void showCards(){
    
    
        System.out.println("赌圣:" + playerOne);
        System.out.println("赌侠:" + playerTwo);
        System.out.println("赌王:" + playerThree);
        System.out.println("底牌:" + diPai);
    }
}

Guess you like

Origin blog.csdn.net/single_0910/article/details/114155762