扑克牌 和 杨辉三角(ArrayList)

用ArrayList实现简单的扑克牌

Card

我们首先要构建一个牌堆。

class Card {
    public String suit;//花色
    public int rank;

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

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

}

接下来,进行的是洗牌和发牌的过程。

buyCard

我们首先要构建好52张扑克牌,我们可以使用

public class PuKePai {
    public static final String[] SUITS = {"♥","♠","♣","♦"};

    public static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                String suit = SUITS[i];
                int rank = j;
                Card card = new Card(suit,rank);
                cards.add(card);
            }
        }
        return cards;
    }
}

这样子直接发牌是不行的,因为顺序是一样的,所以我们要进行的是洗牌过程。

shuffle

我们直接使用random随机数进行打乱。

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

Main

最后写一个main函数,模拟发牌。

    public static void main(String[] args) {

        List<Card> cardList = buyCard();
        shuffle(cardList);

        // 现在有三个人,轮流抓牌
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }

        System.out.println("第一个 "+hand1);
        System.out.println("第二个 "+hand2);
        System.out.println("第三个 "+hand3);
    }
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {
    public String suit;//花色
    public int rank;
    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }
    @Override
    public String toString() {
        return suit+"" + rank ;
    }

}
public class PuKePai {
    public static final String[] SUITS = {"♥","♠","♣","♦"};
    public static List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                String suit = SUITS[i];
                int rank = j;
                Card card = new Card(suit,rank);
                cards.add(card);
            }
        }
        return cards;
    }
    public static void shuffle(List<Card> cards) {
        //洗牌
        Random random = new Random();
        for (int i = cards.size()-1; i > 0; i--) {
            int index = random.nextInt(i);
            Card tmp = cards.get(i);
            cards.set(i,cards.get(index));
            cards.set(index,tmp);
        }
    }
    public static void main(String[] args) {
        List<Card> cardList = buyCard();
        shuffle(cardList);
        // 现在有三个人,轮流抓牌
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        System.out.println("第一个 "+hand1);
        System.out.println("第二个 "+hand2);
        System.out.println("第三个 "+hand3);
    }
}

杨辉三角

1. 我们看到返回值是一个List<List<Integer>>,所以我们先new一个返回ret;

2. 然后就是进行每一小步的计算;

3. 观察发现,每一行的最左边和最右边都是1,我们可以将其单列出来,只需进行中间的运算;

4. 我们get到上一行的数据,根据公式可以进行运算得出左后 的结果。

    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        List<Integer> list1 = new ArrayList<>();
        list1.add(1);
        ret.add(list1);
        for(int i = 1;i < numRows;i++) {
            List<Integer> list = new ArrayList<>();
            list.add(1);
            for(int j = 1;j < i;j++){
                List<Integer> cur = ret.get(i-1);
                int val = cur.get(j) + cur.get(j-1);
                list.add(val);
            }
            list.add(1);
            ret.add(list);
        }
        return ret;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值