Java使用ArrayList實(shí)現(xiàn)撲克牌的示例代碼
前言
學(xué)習(xí)了關(guān)于集合類的知識(shí),我們可以做一個(gè)小項(xiàng)目來加深對(duì)集合類知識(shí)的學(xué)習(xí)!
一、項(xiàng)目要求
代碼實(shí)現(xiàn),一副撲克牌(不包括大小王)的購買、打亂、發(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è)沒有大小王: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è)沒有大小王: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的使用方法詳解
ApplicationContextAware?通過它Spring容器會(huì)自動(dòng)把上下文環(huán)境對(duì)象調(diào)用ApplicationContextAware接口中的setApplicationContext方法,這篇文章主要介紹了Spring中ApplicationContextAware的作用,需要的朋友可以參考下2023-03-03
Spring框架 引入@Resource注解報(bào)空指針的解決
這篇文章主要介紹了Spring框架 引入@Resource注解報(bào)空指針的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案
這篇文章主要介紹了feignclient?https?接口調(diào)用報(bào)證書錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java 開發(fā)的幾個(gè)注意點(diǎn)總結(jié)
這篇文章主要介紹了Java開發(fā)的幾個(gè)注意點(diǎn)的相關(guān)資料,需要的朋友可以參考下2016-09-09
SpringBoot+RabbitMq具體使用的幾種姿勢(shì)
這篇文章主要介紹了SpringBoot+RabbitMq具體使用的幾種姿勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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不一致異常問題,具有很好的參考價(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 支持,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

