最近學了很多的知識,腦容量小,記不清,還是得做做練習!
今天就做了一個撲克牌的練習
首先呢..這個邏輯一定要非常清楚,我們要想做出一副撲克牌,必定要弄清楚每一張牌和整的一副牌
首先分析 一張撲克
一張牌里面有什么?相信大家看圖(圖不是我寫的)就應該懂了,一張撲克有屬于它自己的花色(紅桃,黑桃,梅花,方塊) 以及自己的點數(a,2,3…..j,q,k)就這兩種屬性,對吧!
那么花色符號,點數符號是個啥? 花色符號就是來代替我們的花色的,我們不可能拿著“紅桃”這種文字寫進程序吧!所以我們可以用數字來代替
我們就按照下面的,一 一對應
1
2
3
4
5
6
|
/** * 王 ♥ ♠ ♣ ♦ * 1 2 3 4 5 * a j q k 小王 大王 * 1 11 12 13 14 15 **/ |
好了,我們已經把每張特殊一點的撲克給對應好了!我們可以開始寫代碼了
我的代碼文件:
- apoker.java————–一張撲克
- poker.java—————-一副撲克
- test.java——————測試
apoker.java先給大家展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
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就是我們用來測試我們之前寫好的代碼!
創建一副撲克
1
2
3
4
5
6
7
8
|
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的內容
1
2
3
4
5
6
7
8
9
|
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
1
2
3
4
5
6
7
8
9
|
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!
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/qq_36547531/article/details/81582632