欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼

 更新時(shí)間:2022年10月04日 09:27:25   作者:摸魚王胖嘟嘟  
學(xué)習(xí)了關(guān)于集合類的知識(shí),我們可以做一個(gè)小項(xiàng)目來(lái)加深對(duì)集合類知識(shí)的學(xué)習(xí)!本文就來(lái)利用ArrayList實(shí)現(xiàn)撲克牌發(fā)牌洗牌效果,需要的可以參考一下

前言

學(xué)習(xí)了關(guān)于集合類的知識(shí),我們可以做一個(gè)小項(xiàng)目來(lái)加深對(duì)集合類知識(shí)的學(xué)習(xí)!

一、項(xiàng)目要求

代碼實(shí)現(xiàn),一副撲克牌(不包括大小王)的購(gòu)買、打亂、發(fā)牌。

二、具體實(shí)現(xiàn)

2.1 Card類

class Card {
    private int rank;//數(shù)字
    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 = {"?", "?", "?", "?"};

    //假設(shè)沒(méi)有大小王: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 發(fā)牌

		System.out.println("揭牌:3個(gè)人每個(gè)人輪流揭牌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);

        //每個(gè)人,輪流揭牌
        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;//數(shù)字
    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 = {"?", "?", "?", "?"};

    //假設(shè)沒(méi)有大小王: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個(gè)人每個(gè)人輪流揭牌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);

        //每個(gè)人,輪流揭牌
        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("第一個(gè)人的牌:"+ hand1);
        System.out.println("第二個(gè)人的牌:"+ hand2);
        System.out.println("第三個(gè)人的牌:"+ hand3);
        System.out.println("剩下的牌:"+cards);
    }

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

    }
}

到此這篇關(guān)于Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼的文章就介紹到這了,更多相關(guān)Java ArrayList撲克牌內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring中ApplicationContextAware的使用方法詳解

    Spring中ApplicationContextAware的使用方法詳解

    ApplicationContextAware?通過(guò)它Spring容器會(huì)自動(dòng)把上下文環(huán)境對(duì)象調(diào)用ApplicationContextAware接口中的setApplicationContext方法,這篇文章主要介紹了Spring中ApplicationContextAware的作用,需要的朋友可以參考下
    2023-03-03
  • Spring框架 引入@Resource注解報(bào)空指針的解決

    Spring框架 引入@Resource注解報(bào)空指針的解決

    這篇文章主要介紹了Spring框架 引入@Resource注解報(bào)空指針的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Springboot2 配置AOP日志的方法步驟

    Springboot2 配置AOP日志的方法步驟

    這篇文章主要介紹了Springboot2 配置AOP日志的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案

    feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案

    這篇文章主要介紹了feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java線程之死鎖

    java線程之死鎖

    這篇文章主要介紹了Java線程之死鎖,死鎖是這樣一種情形-多個(gè)線程同時(shí)被阻塞,它們中的一個(gè)或者全部都在等待某個(gè)資源被釋放。由于線程被無(wú)限期地阻塞,因此程序不可能正常終止
    2022-05-05
  • Java?不同版本的?Switch語(yǔ)句

    Java?不同版本的?Switch語(yǔ)句

    本文主要介紹了Java不同版本的Switch語(yǔ)句,自Java13以來(lái),Switch表達(dá)式就被添加到Java核心庫(kù)中,下面我們將介紹舊的Java?Switch語(yǔ)句和新的Switch語(yǔ)句的區(qū)別,需要的朋友可以參考一下
    2022-06-06
  • Java 開發(fā)的幾個(gè)注意點(diǎn)總結(jié)

    Java 開發(fā)的幾個(gè)注意點(diǎn)總結(jié)

    這篇文章主要介紹了Java開發(fā)的幾個(gè)注意點(diǎn)的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • SpringBoot+RabbitMq具體使用的幾種姿勢(shì)

    SpringBoot+RabbitMq具體使用的幾種姿勢(shì)

    這篇文章主要介紹了SpringBoot+RabbitMq具體使用的幾種姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • JVM默認(rèn)時(shí)區(qū)為:Asia/Shanghai與java程序中GMT+08不一致異常

    JVM默認(rèn)時(shí)區(qū)為:Asia/Shanghai與java程序中GMT+08不一致異常

    這篇文章主要介紹了JVM默認(rèn)時(shí)區(qū)為:Asia/Shanghai與java程序中GMT+08不一致異常問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整Git commit template 支持

    IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整Git commit template 支持

    這篇文章主要介紹了IntelliJ IDEA 2021.1 EAP 4 發(fā)布:字體粗細(xì)可調(diào)整,Git commit template 支持,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02

最新評(píng)論