最近學了很多的知識,腦容量小,記不清,還是得做做練習!
今天就做了一個撲克牌的練習
首先呢..這個邏輯一定要非常清楚,我們要想做出一副撲克牌,必定要弄清楚每一張牌和整的一副牌

首先分析 一張撲克
一張牌里面有什么?相信大家看圖(圖不是我寫的)就應該懂了,一張撲克有屬于它自己的花色(紅桃,黑桃,梅花,方塊) 以及自己的點數(A,2,3…..J,Q,K)就這兩種屬性,對吧!
那么花色符號,點數符號是個啥? 花色符號就是來代替我們的花色的,我們不可能拿著“紅桃”這種文字寫進程序吧!所以我們可以用數字來代替
我們就按照下面的,一 一對應
/** * 王 ♥ ♠ ♣ ♦ * 1 2 3 4 5 * A J Q K 小王 大王 * 1 11 12 13 14 15 **/
好了,我們已經把每張特殊一點的撲克給對應好了!我們可以開始寫代碼了
我的代碼文件:
APoker.java先給大家展示
public class APoker {
//implements Comparable<APoker>
//花色
private int color;
//點數
private int count;
//花色符號
private String colorText;
//點數符號
private String countText;
//寫構造方法
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 方法,進行封裝
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;
}
//重寫 toString 方法,因為我們需要顯示每張牌的具體情況
@Override
public String toString() {
return "APoker [color=" + color + ", count=" + count + ", colorText=" + colorText + ", countText=" + countText
+ "]\n";
}
}
這里還是非常容易理解的,無非就是進行了封裝和重寫toString方法。
OK,一張撲克寫完了,我們接下來寫一副撲克牌
一副撲克牌
我再把那個圖拿下來

我們發現一副撲克牌里面有花色數量,撲克牌的數量,以及所有牌(所有牌也就是一個集合)。另外這里面還有著幾個方法,這里我就寫創建撲克(),洗牌()抽取一張() 吧。
現在看下
Poker.java:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Poker {
//花色數量
private int colorcount;
//牌的數量
private int pokercount;
//牌的集合
private List<APoker> mList;
//進行封裝
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
**/
//創建一副撲克牌
public List<APoker> creatPoker() {
//初始化colorcount pokercount
colorcount=5;//一副撲克有 王 ♥ ♠ ♣ ♦這五種花色
pokercount=54;//一副撲克共有54張牌
mList=new ArrayList<APoker>();
// ♥ ♠ ♣ ♦----------先分析這四種,因為這四種里面才含有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,都直接使用數字,這里是將j轉化為字符
break;
}
APoker aPoker1=new APoker(i, j, colorText, countText);
mList.add(aPoker1); //把♥ ♠ ♣ ♦這四種花色塞進一副撲克里面
}
}
APoker aPoker2=new APoker(1, 14, "王", "小王");//寫小王
APoker aPoker3=new APoker(1, 14, "王", "大王");//寫大王
mList.add(aPoker2);//把小王塞進一副撲克里面去
mList.add(aPoker3);//把大王塞進一副撲克里面去
return mList;
}
/**
*洗牌方法
**/
public List<APoker> shufflePoker() {
Collections.shuffle(mList); //這是Collections的一個把集合打亂的方法
return mList;
}
/**
* 隨機抽牌
**/
public APoker getRandomPoker() {
Random random=new Random();//獲取一個隨機數
int index=random.nextInt(54);
return mList.get(index);
}
}
這里慢慢看也很容易的,我已經全部把每一步解釋了,大家根據那個對應關系應該很容易理解。
兩個寫好了,我們可以進行使用了
Test.java就是我們用來測試我們之前寫好的代碼!
創建一副撲克
import java.util.List;
public class Test {
public static void main(String[] args) {
Poker poker=new Poker();//創建一副撲克對象
List<APoker> mList=poker.creatPoker(); 調用creatPoker()方法,創建一副撲克
System.out.println(mList);打印出來!
}
}
我們來看結果

OK 54張撲克被創建了!
洗牌
我們修改一下Test.java的內容
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);
}
}
打印一下

果然,,牌的順序已經亂了,我們進行了洗牌
隨機抽牌
我們繼續重寫一下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);
}
}
打印一下

果然它隨機抽取了一張,每次打印抽取的牌都是不同的,這里就不展示了!
OK,大家繼續學習吧,come on!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。如果你想了解更多相關內容請查看下面相關鏈接
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。