Java基礎(chǔ)高級(jí)綜合練習(xí)題撲克牌的創(chuàng)建
最近學(xué)了很多的知識(shí),腦容量小,記不清,還是得做做練習(xí)!
今天就做了一個(gè)撲克牌的練習(xí)
首先呢..這個(gè)邏輯一定要非常清楚,我們要想做出一副撲克牌,必定要弄清楚每一張牌和整的一副牌

首先分析 一張撲克
一張牌里面有什么?相信大家看圖(圖不是我寫(xiě)的)就應(yīng)該懂了,一張撲克有屬于它自己的花色(紅桃,黑桃,梅花,方塊) 以及自己的點(diǎn)數(shù)(A,2,3…..J,Q,K)就這兩種屬性,對(duì)吧!
那么花色符號(hào),點(diǎn)數(shù)符號(hào)是個(gè)啥? 花色符號(hào)就是來(lái)代替我們的花色的,我們不可能拿著“紅桃”這種文字寫(xiě)進(jìn)程序吧!所以我們可以用數(shù)字來(lái)代替
我們就按照下面的,一 一對(duì)應(yīng)
/** * 王 ♥ ♠ ♣ ♦ * 1 2 3 4 5 * A J Q K 小王 大王 * 1 11 12 13 14 15 **/
好了,我們已經(jīng)把每張?zhí)厥庖稽c(diǎn)的撲克給對(duì)應(yīng)好了!我們可以開(kāi)始寫(xiě)代碼了
我的代碼文件:
- APoker.java————–一張撲克
- Poker.java—————-一副撲克
- Test.java——————測(cè)試
APoker.java先給大家展示
public class APoker {
//implements Comparable<APoker>
//花色
private int color;
//點(diǎn)數(shù)
private int count;
//花色符號(hào)
private String colorText;
//點(diǎn)數(shù)符號(hào)
private String countText;
//寫(xiě)構(gòu)造方法
public APoker(int color, int count, String colorText, String countText) {
super();
this.color = color;
this.count = count;
this.colorText = colorText;
this.countText = countText;
}
//GET SET 方法,進(jìn)行封裝
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getColorText() {
return colorText;
}
public void setColorText(String colorText) {
this.colorText = colorText;
}
public String getCountText() {
return countText;
}
public void setCountText(String countText) {
this.countText = countText;
}
//重寫(xiě) toString 方法,因?yàn)槲覀冃枰@示每張牌的具體情況
@Override
public String toString() {
return "APoker [color=" + color + ", count=" + count + ", colorText=" + colorText + ", countText=" + countText
+ "]\n";
}
}
這里還是非常容易理解的,無(wú)非就是進(jìn)行了封裝和重寫(xiě)toString方法。
OK,一張撲克寫(xiě)完了,我們接下來(lái)寫(xiě)一副撲克牌
一副撲克牌
我再把那個(gè)圖拿下來(lái)

我們發(fā)現(xiàn)一副撲克牌里面有花色數(shù)量,撲克牌的數(shù)量,以及所有牌(所有牌也就是一個(gè)集合)。另外這里面還有著幾個(gè)方法,這里我就寫(xiě)創(chuàng)建撲克(),洗牌()抽取一張() 吧。
現(xiàn)在看下
Poker.java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Poker {
//花色數(shù)量
private int colorcount;
//牌的數(shù)量
private int pokercount;
//牌的集合
private List<APoker> mList;
//進(jìn)行封裝
public int getColorcount() {
return colorcount;
}
public void setColorcount(int colorcount) {
this.colorcount = colorcount;
}
public int getPokercount() {
return pokercount;
}
public void setPokercount(int pokercount) {
this.pokercount = pokercount;
}
public List<APoker> getmList() {
return mList;
}
public void setmList(List<APoker> mList) {
this.mList = mList;
}
/**
* 王 ♥ ♠ ♣ ♦
* 1 2 3 4 5
* A J Q K 小王 大王
* 1 11 12 13 14 15
**/
//創(chuàng)建一副撲克牌
public List<APoker> creatPoker() {
//初始化colorcount pokercount
colorcount=5;//一副撲克有 王 ♥ ♠ ♣ ♦這五種花色
pokercount=54;//一副撲克共有54張牌
mList=new ArrayList<APoker>();
// ♥ ♠ ♣ ♦----------先分析這四種,因?yàn)檫@四種里面才含有A-K的值,小王大王后面處理
for (int i = 2; i <=5; i++) {
//得到每種花色里面的牌
for (int j = 1; j <= 13; j++) {
String colorText=null;
String countText=null;
switch (i) {
case 2:
colorText="♥";
break;
case 3:
colorText="♠";
break;
case 4:
colorText="♣";
break;
case 5:
colorText="♦";
break;
}
switch (j) {
case 1:
countText="A";
break;
case 11:
countText="J";
break;
case 12:
countText="Q";
break;
case 13:
countText="K";
break;
default:
countText=j+""; //除了A,J,Q,K,都直接使用數(shù)字,這里是將j轉(zhuǎn)化為字符
break;
}
APoker aPoker1=new APoker(i, j, colorText, countText);
mList.add(aPoker1); //把♥ ♠ ♣ ♦這四種花色塞進(jìn)一副撲克里面
}
}
APoker aPoker2=new APoker(1, 14, "王", "小王");//寫(xiě)小王
APoker aPoker3=new APoker(1, 14, "王", "大王");//寫(xiě)大王
mList.add(aPoker2);//把小王塞進(jìn)一副撲克里面去
mList.add(aPoker3);//把大王塞進(jìn)一副撲克里面去
return mList;
}
/**
*洗牌方法
**/
public List<APoker> shufflePoker() {
Collections.shuffle(mList); //這是Collections的一個(gè)把集合打亂的方法
return mList;
}
/**
* 隨機(jī)抽牌
**/
public APoker getRandomPoker() {
Random random=new Random();//獲取一個(gè)隨機(jī)數(shù)
int index=random.nextInt(54);
return mList.get(index);
}
}
這里慢慢看也很容易的,我已經(jīng)全部把每一步解釋了,大家根據(jù)那個(gè)對(duì)應(yīng)關(guān)系應(yīng)該很容易理解。
兩個(gè)寫(xiě)好了,我們可以進(jìn)行使用了
Test.java就是我們用來(lái)測(cè)試我們之前寫(xiě)好的代碼!
創(chuàng)建一副撲克
import java.util.List;
public class Test {
public static void main(String[] args) {
Poker poker=new Poker();//創(chuàng)建一副撲克對(duì)象
List<APoker> mList=poker.creatPoker(); 調(diào)用creatPoker()方法,創(chuàng)建一副撲克
System.out.println(mList);打印出來(lái)!
}
}
我們來(lái)看結(jié)果

OK 54張撲克被創(chuàng)建了!
洗牌
我們修改一下Test.java的內(nèi)容
import java.util.List;
public class Test {
public static void main(String[] args) {
Poker poker=new Poker();
List<APoker> mList=poker.creatPoker();
List<APoker> mList2=poker.shufflePoker();
System.out.println(mList2);
}
}
打印一下

果然,,牌的順序已經(jīng)亂了,我們進(jìn)行了洗牌
隨機(jī)抽牌
我們繼續(xù)重寫(xiě)一下Test.java
import java.util.List;
public class Test {
public static void main(String[] args) {
Poker poker=new Poker();
List<APoker> mList=poker.creatPoker();
APoker ap=poker.getRandomPoker();
System.out.println(ap);
}
}
打印一下

果然它隨機(jī)抽取了一張,每次打印抽取的牌都是不同的,這里就不展示了!
OK,大家繼續(xù)學(xué)習(xí)吧,come on!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
SpringBoot如何導(dǎo)出Jar包并測(cè)試(使用IDEA)
這篇文章主要介紹了SpringBoot如何導(dǎo)出Jar包并測(cè)試(使用IDEA),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
詳解基于java的Socket聊天程序——客戶端(附demo)
這篇文章主要介紹了詳解基于java的Socket聊天程序——客戶端(附demo),客戶端設(shè)計(jì)主要分成兩個(gè)部分,分別是socket通訊模塊設(shè)計(jì)和UI相關(guān)設(shè)計(jì)。有興趣的可以了解一下。2016-12-12
SpringBoot集成Hutool防止XSS攻擊的兩種解決方法
XSS漏洞是生產(chǎn)上比較常見(jiàn)的問(wèn)題,本文主要介紹了SpringBoot集成Hutool防止XSS攻擊的兩種解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
關(guān)于java String中intern的深入講解
這篇文章主要給大家介紹了關(guān)于java String中intern的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
springboot引入druid解析sql的過(guò)程
在開(kāi)發(fā)中,有時(shí)我們可能會(huì)需要獲取SQL中的表名,那么因?yàn)椴煌臄?shù)據(jù)源類型SQL會(huì)存在部分差異,那么我們就可以使用alibaba 的druid包實(shí)現(xiàn)不同的數(shù)據(jù)源類型的sql解析,需要的朋友可以參考下2023-08-08
Java中的分布式系統(tǒng)開(kāi)發(fā)基于?Zookeeper?與?Dubbo?的應(yīng)用案例解析
本文將通過(guò)實(shí)際案例,帶你走進(jìn)基于Zookeeper與Dubbo的分布式系統(tǒng)開(kāi)發(fā),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-09-09

