Using Java to Realize Simple Shuffle and Card Dealing Functions

In daily life, everyone must have played or heard of Doudizhu. Doudizhu is roughly divided into three processes: shuffling cards, dealing cards, and players finishing their cards according to the rules. just win. It sounds simple, but there are many factors that affect your victory: shuffling, player level. In casinos, many people cheat during the process of shuffling and dealing cards, so can we use code to realize the functions of shuffling and dealing cards to achieve relative fairness?

code idea

To deal and shuffle the cards, we first need to have poker cards. Each different card corresponds to a different suit and number. Here we need to abstract a deck of poker objects, and then shuffle the cards. We randomly shuffle the cards The order of the playing cards, and then the dealing of the cards, everyone distributes the same number of playing cards equally.

Code

Here we create a Poker package to package our poker-related classes. Create three Java files in the Poker package: the Poker file is used to generate poker cards, including suits and points; the Game file is used to implement the functions of creating a set of poker cards, shuffling and dealing cards; and the Test file is what we use experimental.

Poker's Java file abstracts a poker card

package poker;

public class Poker {
    
    

//扑克牌的点数
    private int rank;
//扑克牌的花色
    private String suit;

//创建构造方法
    public Poker(int rank, String suit) {
    
    
        this.rank = rank;
        this.suit = suit;
    }

//以下这些方法我们今天暂时用不上
    public int getRank() {
    
    
        return rank;
    }

    public void setRank(int rank) {
    
    
        this.rank = rank;
    }

    public String getSuit() {
    
    
        return suit;
    }

//重写toString方法
    @Override
    public String toString() {
    
    
        return "{ "+rank+" "+suit+"}";
    }
}

Game's Java file realizes the functions of shuffling and dealing cards

Create a member variable that stores a string array of suits

These suits cannot be changed and can only be used in the current class, so we directly
decorate them with private static final to ensure security.

private static final String[] suit = {
    
    "♥","♣","♦","♠"};

Create a deck of cards

We use arrays to store playing cards

public List<Poker> buyPoker() {
    
    
        List<Poker> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
    
    
            for (int j = 1; j <=13 ; j++) {
    
    
                Poker poker = new Poker(j,suit[i]);
                pokers.add(poker);
            }
        }
        return pokers;
    }

Use Test to see whether the creation is successful. When writing code, we must develop the habit of testing while writing, so that errors can be found in time.

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Game game = new Game();
        List<Poker> poker = game.buyPoker();
    }
}

insert image description here

shuffle

The operation principle of shuffling is to exchange random pairs of poker cards. Since it is random, we need to use Random to generate random numbers. We start from the back of the array and exchange the numbers corresponding to the front random positions, because it is easy for us to Generate random numbers.

	public void shuffle(List<Poker> poker) {
    
    
        for (int i = poker.size()-1; i > 0 ; i--) {
    
    
            Random random = new Random();
            int index = random.nextInt(i);
            swap(poker,i,index);
        }
    }
    
//创建私有方法来实现交换
	private void swap(List<Poker> pokers,int i,int j) {
    
    
        Poker tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));
        pokers.set(j,tmp);
    }

Again, let's do a test.
insert image description here
We can see that our shuffling function has been implemented, and the order of the playing cards generated each time is different.

Licensing

Here we create a two-dimensional array to store the hands of the three players.

public List<List<Poker>> game(List<Poker> pokers) {
    
    
        List<List<Poker>> hand = new ArrayList<>();
        List<Poker> hand1 = new ArrayList<>();
        List<Poker> hand2 = new ArrayList<>();
        List<Poker> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 3; i++) {
    
    
        //我们这里测试每人分发五张
            for (int j = 0; j < 5; j++) {
    
    
//我们默认每次从下标为0的位置分发,并且分发一次就移除这个下表为0的扑克牌,
//移除之后,它后面的数字也会自动补齐到0位置
                Poker tmp = pokers.remove(0);
                hand.get(i).add(tmp);
            }
        }
        return hand;
    }

Overall function realization

Poker.java

public class Poker {
    
    
    private int rank;
    private String suit;

    public Poker(int rank, String suit) {
    
    
        this.rank = rank;
        this.suit = suit;
    }

    public int getRank() {
    
    
        return rank;
    }

    public void setRank(int rank) {
    
    
        this.rank = rank;
    }

    public String getSuit() {
    
    
        return suit;
    }

    @Override
    public String toString() {
    
    
        return "{ "+rank+" "+suit+"}";
    }
}

Game.java

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Game {
    
    
    private static final String[] suit = {
    
    "♥","♣","♦","♠"};
    public List<Poker> buyPoker() {
    
    
        List<Poker> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
    
    
            for (int j = 1; j <=13 ; j++) {
    
    
                Poker poker = new Poker(j,suit[i]);
                pokers.add(poker);
            }
        }
        return pokers;
    }

    //洗牌
    public void shuffle(List<Poker> poker) {
    
    
        for (int i = poker.size()-1; i > 0 ; i--) {
    
    
            Random random = new Random();
            int index = random.nextInt(i);
            swap(poker,i,index);
        }
    }

    private void swap(List<Poker> pokers,int i,int j) {
    
    
        Poker tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));
        pokers.set(j,tmp);
    }

    public List<List<Poker>> game(List<Poker> pokers) {
    
    
        List<List<Poker>> hand = new ArrayList<>();
        List<Poker> hand1 = new ArrayList<>();
        List<Poker> hand2 = new ArrayList<>();
        List<Poker> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 3; i++) {
    
    
            for (int j = 0; j < 5; j++) {
    
    
                Poker tmp = pokers.remove(0);
                hand.get(i).add(tmp);
            }
        }
        return hand;
    }
}

Test.java

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Game game = new Game();
        List<Poker> poker = game.buyPoker();
        System.out.println(poker);
        game.shuffle(poker);
        System.out.println(poker);
        List<List<Poker>> hand = game.game(poker);
        for (int i = 0; i < hand.size(); i++) {
    
    
            System.out.println("第"+(i+1)+"个人的牌"+hand.get(i));
        }
        System.out.println("剩下的牌");
        System.out.println(poker);
    }
}

insert image description here

Guess you like

Origin blog.csdn.net/m0_73888323/article/details/130000389