Java實(shí)現(xiàn)撲克牌的創(chuàng)建以及發(fā)放
題目:
創(chuàng)建一個(gè)撲克牌(不需要包含大小王),分別分發(fā)給3個(gè)人,一個(gè)人發(fā)5張牌,輸出結(jié)果要求包含全套牌(52張牌),以及3個(gè)人各自的牌的花色以及數(shù)字。
1.撲克牌的源代碼
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created with IntelliJ IDEA.
* Description:
* User: 曉星航
* Date: 2023-02-27
* Time: 18:09
*/
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+" ]";
}
}
//沒(méi)有大小王:1,2,3,4,5,6,7,8,9,10,11,12,13
public class TestDemo {
private static final String[] suits = {"?","?","?","?"};
public static List<Card> buyCard() {
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {/*
int rank = j;
String suit = suits[i];
Card card = new Card(j,suits[i]);
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[i];
Card tmp = cards.get(i);
// cards[i] = cards[j];
cards.set(i,cards.get(j));
// cards[j] = tmp;
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);
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("第1個(gè)人的牌: "+hand1);
System.out.println("第2個(gè)人的牌: "+hand2);
System.out.println("第3個(gè)人的牌: "+hand3);
System.out.println("剩下的牌: "+cards);
}
2.撲克牌運(yùn)行結(jié)果

3.撲克牌代碼創(chuàng)建原理講解
3.1創(chuàng)建撲克牌前的準(zhǔn)備工作
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+" ]";
}
}
這為后續(xù)撲克牌的花色以及數(shù)字大小創(chuàng)建了類。
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張牌");
System.out.println("第1個(gè)人的牌: "+hand1);
System.out.println("第2個(gè)人的牌: "+hand2);
System.out.println("第3個(gè)人的牌: "+hand3);
System.out.println("剩下的牌: "+cards);
}
}
這里我們寫(xiě)好了我們撲克牌的基本菜單邏輯,即買牌(52張),再洗牌(打亂牌),最后將所有牌分發(fā)給玩游戲的三個(gè)人(每人五張)。
3.2 52張撲克牌的創(chuàng)建(除去大小王)
public static List<Card> buyCard() {
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {/*
int rank = j;
String suit = suits[i];
Card card = new Card(j,suits[i]);
cards.add(card);*/
cards.add(new Card(j,suits[i]));
}
}
return cards;
}
這里我們定義了buyCard方法來(lái)創(chuàng)建我們的52張牌,cards是我們存放卡牌的位置(包含數(shù)字以及花色),通過(guò)雙循環(huán)來(lái)創(chuàng)建我們梅花♣、方片♦、紅桃♥和黑桃♠這四種牌。
3.3洗牌
private static void swap(List<Card> cards,int i,int j) {
// Card tmp = cards[i];
Card tmp = cards.get(i);
// cards[i] = cards[j];
cards.set(i,cards.get(j));
// cards[j] = tmp;
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);
}
}
在寫(xiě)我們的shuffle洗牌方法前我們寫(xiě)了swap交換方法,其中swap使用的是空瓶交換原理通過(guò)List中g(shù)et方法來(lái)交換數(shù)值以及相對(duì)應(yīng)的花色。
而shuffle方法我們則用了size來(lái)代表我們卡牌的數(shù)量,通過(guò)random隨機(jī)方法來(lái)生成隨機(jī)數(shù)傳給我們swap交換方法當(dāng)作形參從而完成我們撲克牌的洗牌。
3.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);
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("第1個(gè)人的牌: "+hand1);
System.out.println("第2個(gè)人的牌: "+hand2);
System.out.println("第3個(gè)人的牌: "+hand3);
System.out.println("剩下的牌: "+cards);
在創(chuàng)建好牌和洗完牌之后當(dāng)然就是我們的發(fā)牌了,我們發(fā)牌采取了創(chuàng)建hand的動(dòng)態(tài)數(shù)組來(lái)儲(chǔ)存我們的牌,然后我們創(chuàng)建了hand1、hand2以及hand3的三個(gè)動(dòng)態(tài)數(shù)組來(lái)分別表示我們的三個(gè)人,通過(guò)兩次循環(huán)達(dá)到3個(gè)人每個(gè)人輪流接5張牌的目的。
最后完成的效果圖如下:

總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)撲克牌的創(chuàng)建以及發(fā)放的文章就介紹到這了,更多相關(guān)Java撲克牌創(chuàng)建及發(fā)放內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring bean的注解注入之@Autowired的原理及使用
之前講過(guò)bean注入是什么,也使用了xml的配置文件進(jìn)行bean注入,這也是Spring的最原始的注入方式(xml注入).本文主要講解的注解有以下幾個(gè):@Autowired、 @Service、@Repository、@Controller 、@Component、@Bean、@Configuration、@Resource ,需要的朋友可以參考下2021-06-06
JAVA實(shí)現(xiàn)連接本地打印機(jī)并打印文件的實(shí)現(xiàn)代碼
這篇文章主要介紹了JAVA實(shí)現(xiàn)連接本地打印機(jī)并打印文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下2019-10-10
SpringCloud 搭建企業(yè)級(jí)開(kāi)發(fā)框架之實(shí)現(xiàn)多租戶多平臺(tái)短信通知服務(wù)(微服務(wù)實(shí)戰(zhàn))
這篇文章主要介紹了SpringCloud 搭建企業(yè)級(jí)開(kāi)發(fā)框架之實(shí)現(xiàn)多租戶多平臺(tái)短信通知服務(wù),系統(tǒng)可以支持多家云平臺(tái)提供的短信服務(wù)。這里以阿里云和騰訊云為例,集成短信通知服務(wù),需要的朋友可以參考下2021-11-11
SpringBoot 項(xiàng)目中的圖片處理策略之本地存儲(chǔ)與路徑映射
在SpringBoot項(xiàng)目中,靜態(tài)資源存放在static目錄下,使得前端可以通過(guò)URL來(lái)訪問(wèn)這些資源,我們就需要將文件系統(tǒng)的文件路徑與URL建立一個(gè)映射關(guān)系,把文件系統(tǒng)中的文件當(dāng)成我們的靜態(tài)資源即可,本文給大家介紹SpringBoot本地存儲(chǔ)與路徑映射的相關(guān)知識(shí),感興趣的朋友一起看看吧2023-12-12
java使用EditText控件時(shí)不自動(dòng)彈出輸入法的方法
這篇文章主要介紹了java使用EditText控件時(shí)不自動(dòng)彈出輸入法的方法,需要的朋友可以參考下2015-03-03
Java實(shí)現(xiàn)拓?fù)渑判虻氖纠a
這篇文章我們要講的是拓?fù)渑判?,這是一個(gè)針對(duì)有向無(wú)環(huán)圖的算法,主要是為了解決前驅(qū)后繼的關(guān)系,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05

