Home  >  Article  >  Java  >  How to implement simple shuffling and dealing functions in Java

How to implement simple shuffling and dealing functions in Java

WBOY
WBOYforward
2023-05-10 20:22:04980browse

Code Idea

To deal and shuffle the cards, we first need to have playing cards. Each different card corresponds to a different suit and number. Here we need to abstract a pair of playing card objects, and then To shuffle the cards, we randomly shuffle the order of the playing cards, and then deal the cards. Everyone distributes the same number of playing cards equally.

Code implementation

We create a Poker package here to wrap our poker-related classes. Create three Java files in the Poker package: the Poker file is used to generate playing cards, including suits and points; the Game file is used to implement the functions of creating a set of playing cards, shuffling and dealing cards; and the Test file is used by us experimental.

Poker's Java file abstracts a playing 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 implements the functions of shuffling and dealing cards

Create member variables that store the string array of suits

These colors cannot be changed and can only be used in the current class, so we directly use
private static final modification to ensure safety.

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

Create a deck of playing cards

We use an array to store the 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. We must develop edge when writing code Have the habit of testing while writing so that errors can be discovered in time.

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

How to implement simple shuffling and dealing functions in Java

Shuffle

	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);
    }

Similarly, let’s do a test.

How to implement simple shuffling and dealing functions in Java

We can see: Our shuffling function has been implemented, and the order of the playing cards generated is different each time.

Dealing

Here we create a two-dimensional array to store the hand cards divided by 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 implementation

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) {
        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 pokers,int i,int j) {
        Poker tmp = pokers.get(i);
        pokers.set(i,pokers.get(j));
        pokers.set(j,tmp);
    }

    public List> game(List pokers) {
        List> hand = new ArrayList<>();
        List hand1 = new ArrayList<>();
        List hand2 = new ArrayList<>();
        List 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);
    }
}

How to implement simple shuffling and dealing functions in Java

The above is the detailed content of How to implement simple shuffling and dealing functions in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete