Java實現(xiàn)撲克牌的創(chuàng)建以及發(fā)放
題目:
創(chuàng)建一個撲克牌(不需要包含大小王),分別分發(fā)給3個人,一個人發(fā)5張牌,輸出結(jié)果要求包含全套牌(52張牌),以及3個人各自的牌的花色以及數(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+" ]";
}
}
//沒有大小王: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個人每個人輪流接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個人的牌: "+hand1);
System.out.println("第2個人的牌: "+hand2);
System.out.println("第3個人的牌: "+hand3);
System.out.println("剩下的牌: "+cards);
}
2.撲克牌運行結(jié)果

3.撲克牌代碼創(chuàng)建原理講解
3.1創(chuàng)建撲克牌前的準備工作
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個人每個人輪流接5張牌");
System.out.println("第1個人的牌: "+hand1);
System.out.println("第2個人的牌: "+hand2);
System.out.println("第3個人的牌: "+hand3);
System.out.println("剩下的牌: "+cards);
}
}
這里我們寫好了我們撲克牌的基本菜單邏輯,即買牌(52張),再洗牌(打亂牌),最后將所有牌分發(fā)給玩游戲的三個人(每人五張)。
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方法來創(chuàng)建我們的52張牌,cards是我們存放卡牌的位置(包含數(shù)字以及花色),通過雙循環(huán)來創(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);
}
}
在寫我們的shuffle洗牌方法前我們寫了swap交換方法,其中swap使用的是空瓶交換原理通過List中g(shù)et方法來交換數(shù)值以及相對應(yīng)的花色。
而shuffle方法我們則用了size來代表我們卡牌的數(shù)量,通過random隨機方法來生成隨機數(shù)傳給我們swap交換方法當作形參從而完成我們撲克牌的洗牌。
3.4發(fā)牌
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("第1個人的牌: "+hand1);
System.out.println("第2個人的牌: "+hand2);
System.out.println("第3個人的牌: "+hand3);
System.out.println("剩下的牌: "+cards);
在創(chuàng)建好牌和洗完牌之后當然就是我們的發(fā)牌了,我們發(fā)牌采取了創(chuàng)建hand的動態(tài)數(shù)組來儲存我們的牌,然后我們創(chuàng)建了hand1、hand2以及hand3的三個動態(tài)數(shù)組來分別表示我們的三個人,通過兩次循環(huán)達到3個人每個人輪流接5張牌的目的。
最后完成的效果圖如下:

總結(jié)
到此這篇關(guān)于Java實現(xiàn)撲克牌的創(chuàng)建以及發(fā)放的文章就介紹到這了,更多相關(guān)Java撲克牌創(chuàng)建及發(fā)放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Spring bean的注解注入之@Autowired的原理及使用
之前講過bean注入是什么,也使用了xml的配置文件進行bean注入,這也是Spring的最原始的注入方式(xml注入).本文主要講解的注解有以下幾個:@Autowired、 @Service、@Repository、@Controller 、@Component、@Bean、@Configuration、@Resource ,需要的朋友可以參考下2021-06-06
JAVA實現(xiàn)連接本地打印機并打印文件的實現(xiàn)代碼
這篇文章主要介紹了JAVA實現(xiàn)連接本地打印機并打印文件的實現(xiàn)代碼,需要的朋友可以參考下2019-10-10
SpringCloud 搭建企業(yè)級開發(fā)框架之實現(xiàn)多租戶多平臺短信通知服務(wù)(微服務(wù)實戰(zhàn))
這篇文章主要介紹了SpringCloud 搭建企業(yè)級開發(fā)框架之實現(xiàn)多租戶多平臺短信通知服務(wù),系統(tǒng)可以支持多家云平臺提供的短信服務(wù)。這里以阿里云和騰訊云為例,集成短信通知服務(wù),需要的朋友可以參考下2021-11-11
SpringBoot 項目中的圖片處理策略之本地存儲與路徑映射
在SpringBoot項目中,靜態(tài)資源存放在static目錄下,使得前端可以通過URL來訪問這些資源,我們就需要將文件系統(tǒng)的文件路徑與URL建立一個映射關(guān)系,把文件系統(tǒng)中的文件當成我們的靜態(tài)資源即可,本文給大家介紹SpringBoot本地存儲與路徑映射的相關(guān)知識,感興趣的朋友一起看看吧2023-12-12

