Java模拟实现扑克牌洗牌和发牌的示例代码

一. 需求

设计一副新的的扑克牌, 4个花色(♥, ♠, ♦, ♣)对应 1 到 13 , 不算大小王一共52张牌 ; 然后将扑克牌随机打乱顺序 , 最后实现三个人进行摸牌 , 三个人轮流进行摸牌(每次摸一张牌) , 最终每个人手里有五张牌

二. 全局代码

poker.java

public class poker {
    private String suit;//花色
    private int num;//数字

    public poker(String suit, int num) {
        this.suit = suit;
        this.num = num;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

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

pokers.java

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

public class pokers {
    public static final String[] SUITS = {"♠","♥","♣","♦"};
    //买一副扑克牌
    public static List<poker> buypokers() {
        List<poker> pokerList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                pokerList.add(new poker(SUITS[i],j));
            }
        }
        return pokerList;
    }

    //洗牌
    public static void shuffle(List<poker> pokerList) {
        Random random = new Random();
        for (int i = pokerList.size()-1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(pokerList, i, index);
        }
    }
    //交换
    public static void swap (List<poker> pokerList, int i, int index) {
        poker tmp = pokerList.get(i);
        pokerList.set(i, pokerList.get(index));
        pokerList.set(index, tmp);
    }

    public static void main(String[] args) {
        List<poker> pokerList = buypokers();
        System.out.println("新牌:" + pokerList);
        shuffle(pokerList);
        System.out.println("洗牌:" + pokerList);

        //揭牌 3个人 每个人轮流揭5张牌

        //用来存放三个人揭起来的牌
        List<poker> hand1 = new ArrayList<>();
        List<poker> hand2 = new ArrayList<>();
        List<poker> hand3 = new ArrayList<>();

        List<List<poker>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //确定是谁在摸牌
                List<poker> tmpHand = hand.get(j);
                tmpHand.add(pokerList.remove(0));
            }
        }

        //输出每个人的牌
        for (int i = 0; i < hand.size(); i++) {
            System.out.println("第"+(i+1)+"个人的牌是"+hand.get(i));
        }

        System.out.println("剩余的牌有"+pokerList);
    }
}

执行结果 :

三. 设计分析

1. 设计一张扑克牌

定义一个类 , 类中字段包含一张扑克牌的 花色 和 数字 ,并给出构造方法和其他相关访问字段的方法

public class poker {
    private String suit;//花色
    private int num;//数字

    public poker(String suit, int num) {
        this.suit = suit;
        this.num = num;
    }

    public String getSuit() {
        return suit;
    }

    public void setSuit(String suit) {
        this.suit = suit;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

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

2. 得到一副新牌

定义一个存放4种花色的数组 , 创建一个顺序表来存放获取到的扑克牌 , 通过两层循环得到52张扑克牌 , 外层循环4次每次得到一种花色 , 内层循环13次得到每种花色的13个值

public static final String[] SUITS = {"♠","♥","♣","♦"};
    //买一副扑克牌
public static List<poker> buypokers() {
    List<poker> pokerList = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        for (int j = 1; j <= 13; j++) {
            pokerList.add(new poker(SUITS[i],j));
        }
    }
    return pokerList;
}

3. 洗牌

顺序表中有52张牌 , 也就是52个元素 , 从最后一个元素开始循环 , 利用 Random 这个类中的方法生成1到元素下标之间的随机数 , 将生成随机数位置的元素和循环中的那个元素进行交换 .

    //洗牌
    public static void shuffle(List<poker> pokerList) {
        Random random = new Random();
        for (int i = pokerList.size()-1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(pokerList, i, index);
        }
    }
    //交换
    public static void swap (List<poker> pokerList, int i, int index) {
        poker tmp = pokerList.get(i);
        pokerList.set(i, pokerList.get(index));
        pokerList.set(index, tmp);
    }

4. 发牌

定义三个顺序表分存放三个人摸起来的牌 , 将这三个顺表再作为元素放入另一个新的顺序表中 , 好方便执行循环摸牌的操作 , 然后还是通过两层循环去摸牌 , 三个人每人摸一张 , 摸5轮 , 所以外层循环执行5次 ; 内层循环3三次 , 每次表示一个人摸去一张牌 ;

其实每次摸牌就就是从扑克牌所在顺序表中删除第一个元素 , 所以每次摸牌访问的都是顺序表中的第一个元素 , remove方法返回的是删除的元素

//用来存放三个人揭起来的牌
List<poker> hand1 = new ArrayList<>();
List<poker> hand2 = new ArrayList<>();
List<poker> hand3 = new ArrayList<>();

List<List<poker>> hand = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);

for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 3; j++) {
        //确定是谁在摸牌
        List<poker> tmpHand = hand.get(j);
        tmpHand.add(pokerList.remove(0));
    }
}

以上就是Java模拟实现扑克牌洗牌和发牌的示例代码的详细内容,更多关于Java扑克牌洗牌发牌的资料请关注其它相关文章!

若文章对您有帮助,帮忙点个赞!

0
-5
发布时间 2022-09-24 12:07:18
0 条回复(回复会通过微信通知作者)
点击加载更多评论
登录 后再进行评论
(微信扫码即可登录,无需注册)