Java使用ArrayList實現撲克牌的示例代碼

前言

學習瞭關於集合類的知識,我們可以做一個小項目來加深對集合類知識的學習!

一、項目要求

代碼實現,一副撲克牌(不包括大小王)的購買、打亂、發牌。

二、具體實現

2.1 Card類

class Card {
    private int rank;//數字
    private String suit;//花色

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

    @Override
    public String toString() {
        return "[ " + this.suit + ":"+this.rank+" ]";
    }
}

2.2 生成撲克牌

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

    //假設沒有大小王:1 2 3............. 11 12 13
    public static List<Card> buyCard() {
        ArrayList<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(rank, suit);
//                cards.add(card);
                cards.add(new Card(j,suits[i]));
            }
        }
        return cards;
    }

2.3 打亂順序

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

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

2.4 發牌

		System.out.println("揭牌:3個人每個人輪流揭牌5張牌");

        ArrayList<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 < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);
                hand.get(j).add(card);
            }
        }

三、Test.java

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

class Card {
    private int rank;//數字
    private String suit;//花色

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

    @Override
    public String toString() {
        return "[ " + this.suit + ":"+this.rank+" ]";
    }
}
public class Test1 {
    private static final String[] suits = {"♥", "♠", "♣", "♦"};

    //假設沒有大小王:1 2 3............. 11 12 13
    public static List<Card> buyCard() {
        ArrayList<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(rank, suit);
//                cards.add(card);
                cards.add(new Card(j,suits[i]));
            }
        }
        return cards;
    }

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

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

    public static void main(String[] args) {
        List<Card> cards = buyCard();
        System.out.println("買牌:" + cards);
        shuffle(cards);
        System.out.println("洗牌:" + cards);
        System.out.println("揭牌:3個人每個人輪流揭牌5張牌");

        ArrayList<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 < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);
                hand.get(j).add(card);
            }
        }
        System.out.println("第一個人的牌:"+ hand1);
        System.out.println("第二個人的牌:"+ hand2);
        System.out.println("第三個人的牌:"+ hand3);
        System.out.println("剩下的牌:"+cards);
    }

    public static void main1(String[] args) {
        // 1. 構造一副撲克牌
        // 2. 揭牌
        Card card = new Card(3,"♥");
        System.out.println(card);

    }
}

到此這篇關於Java使用ArrayList實現撲克牌的示例代碼的文章就介紹到這瞭,更多相關Java ArrayList撲克牌內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: