洗牌算法 和 杨辉三角

目录

一、洗牌算法

1.1 Poker

1.2 PokerBox

1.3 Test

二、杨辉三角


一、洗牌算法

【业务需求】:

第一步:生成52张牌,没有大小王,一共四种花色({"♠", "♥", "♣", "♦"),每种花色共13张,J、Q、K用11、12、13来代替。

第二步:洗牌。

第三步:给三个人轮流发5张牌。

第四步:将三个人的牌 和 剩余的牌 按顺序打印出来。

【点睛】:把扑克牌box、三个人的手牌、都设置成ArrayList,对它们进行操作时直接调用ArrayList原生方法即可。

1.1 Poker

package poker;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-06
 * Time: 10:23
 */
public class Poker implements Comparable<Poker> {
    private int value;
    private String flower;

    public Poker(int value, String flower) {
        this.value = value;
        this.flower = flower;
    }

    @Override
    public String toString() {
        return "{" + this.value + " " + this.flower + "}";
    }

    @Override
    public int compareTo(Poker o) {
        return this.value - o.value;
    }
}

1.2 PokerBox

package poker;

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

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-06
 * Time: 10:26
 */
public class PokerBox {
    private ArrayList<Poker> pokers;//52张牌
    private static final String[] flowers = {"♠", "♥", "♣", "♦"};//花色

    public PokerBox() {
        this.pokers = new ArrayList<>();
        /* 生成52张排 */
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                Poker poker = new Poker(j,flowers[i]);
                this.pokers.add(poker);
            }
        }
    }

    /* 洗牌 */
    public void shuffle() {
        Random random = new Random();
        for (int i = this.pokers.size() - 1; i > 0 ; i--) {
            //生成0~i-1之间的随机值
            int j = random.nextInt(i);
            //交换pokers中下标为i和j的值
            //注意:可以发现我们在得到/设置顺表中的元素时,会直接利用方法🧐
            Poker cur = this.pokers.get(i);
            this.pokers.set(i, this.pokers.get(j));
            this.pokers.set(j,cur);
        }
    }

    /* 给三个人分配牌 */
    public void organize() {
        ArrayList<Poker> hand1 = new ArrayList<>();
        ArrayList<Poker> hand2 = new ArrayList<>();
        ArrayList<Poker> hand3 = new ArrayList<>();

        //为了方便组织三个人,于是定义了这个
        ArrayList<ArrayList<Poker>> hands = new ArrayList<>();

        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for (int i = 0; i < 5; i++) {//外层循环表示发牌的轮数
            for (int j = 0; j < 3; j++) {//内存循环表示发给哪个人
                //remove方法的返回值是,被删除元素的值
                //每次拿牌都拿最上面的那张
                Poker poker = this.pokers.remove(0);
                hands.get(j).add(poker);
            }
        }

        System.out.println("三人的牌面值:");
        System.out.println(hand1);
        System.out.println(hand2);
        System.out.println(hand3);
    }

    /* 对牌排序:根据牌面值的大小进行了排序 */
    public void mySort() {
        //Collections是操作集合的工具类,可以利用里面的sort方法对集合里面的元素进行排序
        Collections.sort(this.pokers);
    }

    @Override
    public String toString() {
        return "PokerBox{" +
                "pokers=" + pokers +
                '}';
    }
}

1.3 Test

package poker;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tangyuxiu
 * Date: 2024-08-06
 * Time: 10:29
 */
public class Test {
    public static void main(String[] args) {
        /* 生成52张牌 */
        PokerBox pokerBox = new PokerBox();
        System.out.println("新牌:");
        System.out.println(pokerBox);

        /* 洗牌 */
        pokerBox.shuffle();
        System.out.println("洗牌:");
        System.out.println(pokerBox);

        /* 给三个人分配牌 */
        pokerBox.organize();
        System.out.println("剩余牌:");
        System.out.println(pokerBox);

        /* 对牌排序 */
        pokerBox.mySort();
        System.out.println("排序:");
        System.out.println(pokerBox);

    }
}

二、杨辉三角

 public List<List<Integer>> generate(int numRows) {
        //二维数组:
        List<List<Integer>> arrayList = new ArrayList<>();
        /* 第一行 */
        List<Integer> arrayList0 = new ArrayList<>();
        arrayList0.add(1);
        //将第一行放入arrayList顺序表中
        arrayList.add(arrayList0);

        /* 其他行 */
        //我们把第一行认为是0行所以这里i从1开始,i<numRows
        for (int i = 1; i < numRows; i++) {
            List<Integer> cur = new ArrayList<>();
            cur.add(1);

            //j表示列号,从1开始是因为0列已经放了元素了
            //j<i是因为,当在i==j时,第j行一定放1,所以不算在里面
            for (int j = 1; j < i; j++) {
                Integer x1 = arrayList.get(i - 1).get(j - 1);
                Integer x2 = arrayList.get(i - 1).get(j);
                cur.add(x1 + x2);
            }

            cur.add(1);
            arrayList.add(cur);
        }

        return arrayList;
}

本篇已完结 ......

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值