public class Card {
2. public static final int DIAMOND = 0; // 方块(钻石)
3. public final static int CLUB = 1; // 梅花
4. public static final int HEART = 2; // 红桃(红心)
5. public static final int SPADE = 3; // 黑桃(花锄)
6. public static final int JOKER = 4; // 王
7.
8. public final static int THREE = 0;
9. public final static int FOUR = 1;
10. public final static int FIVE = 2;
11. public final static int SIX = 3;
12. public final static int SEVEN = 4;
13. public final static int EIGHT = 5;
14. public final static int NINE = 6;
15. public final static int TEN = 7;
16. public final static int JACK = 8;// J
17. public final static int QUEEN = 9;// Q
18. public final static int KING = 10;// K
19. public final static int ACE = 11;// A
20. public final static int DEUCE = 12; // 2
21. public final static int BLACK = 13; // 小王
22. public final static int COLOR = 14;// 大王
23.
24. /** 花色 0代表方块, 1代表梅花, 2代表红桃, 3代表黑桃,4:王 */
25. private int suit;
26. /** 点数 规定: 0代表3, 1代表4, 2代表5,... */
27. private int rank;
28.
29. public Card() {
30. }
31.
32. public Card(int suit, int rank) {
33. // this.rank = rank;
34. // this.suit = suit;
35. setRank(rank);
36. setSuit(suit);
37. }
38.
39. public int getSuit() {
40. return suit;
41. }
42.
43. public void setSuit(int suit) {
44. if (suit < DIAMOND || suit > JOKER)
45. throw new RuntimeException("花色超过范围!");
46. this.suit = suit;
47. }
48.
49. public int getRank() {
50. return rank;
51. }
52.
53. public void setRank(int rank) {
54. if (rank < THREE || rank > COLOR) {
55. throw new RuntimeException("点数超过范围!");
56. }
57. this.rank = rank;
58. }
59.
60. private static final String[] RANK_NAMES = { "3", "4", "5", "6", "7", "8",
61. "9", "10", "J", "Q", "K", "A", "2", "小王", "大王" };
62. private static final String[] SUIT_NAMES = { "方块", "梅花", "红桃", "黑桃", "" };
63.
64. // 覆盖Object 类的toStirng() 方法. 实现对象的文本描述
65. public String toString() {
66. return SUIT_NAMES[suit] + RANK_NAMES[rank];
67. }
68.
69. public boolean equals(Object obj) {
70. if (obj == null) {
71. return false;
72. }
73. if (this == obj) {
74. return true;
75. }
76. if (obj instanceof Card) {
77. Card other = (Card) obj;
78. return this.rank == other.rank && this.suit == other.suit;
79. }
80. return false;
81. }
82.
83. public int hashCode() {
84. // return suit*100+rank;
85. // suit=3= 00000000 00000000 00000000 00000011
86. // rank=10=00000000 00000000 00000000 00000011
87. // suit<<16=00000000 00000011 00000000 00000000
88. // 00000000 00000011 00000000 00000011
89. return (suit << 16) + rank;// (x<<16)+y
90. }
91.
92.}
Java代码 复制代码 收藏代码
1.import java.util.ArrayList;
2.import java.util.List;
3.import java.util.Random;
4.
5.import org.junit.Test;
6.
7.public class ShuffleCard {
8.
9. public static void swap(List<?> list, int i, int j) {
10. final List l = list;
11. l.set(i, l.set(j, l.get(i)));
12. }
13.
14. //随机洗牌
15. @Test
16. public void testShuffle2() {
17. List<Card> cards = new ArrayList<Card>();
18. // 生成一副牌
19. for (int rank = Card.THREE; rank <= Card.DEUCE; rank++) {
20. cards.add(new Card(Card.DIAMOND, rank));
21. cards.add(new Card(Card.CLUB, rank));
22. cards.add(new Card(Card.HEART, rank));
23. cards.add(new Card(Card.SPADE, rank));
24. }
25. cards.add(new Card(Card.JOKER, Card.BLACK));
26. cards.add(new Card(Card.JOKER, Card.COLOR));
27. System.out.println(cards.toString());
28.
29. Random random = new Random();
30. for (int i = cards.size(); i > 1; i--) {
31. int m = random.nextInt(i);
32. swap(cards, i - 1, m);
33. }
34. System.out.println(cards.toString());
35. }
36.
37.}
生成结果:
[方块3, 梅花3, 红桃3, 黑桃3, 方块4, 梅花4, 红桃4, 黑桃4, 方块5, 梅花5, 红桃5, 黑桃5, 方块6, 梅花6, 红桃6, 黑桃6, 方块7, 梅花7, 红桃7, 黑桃7, 方块8, 梅花8, 红桃8, 黑桃8, 方块9, 梅花9, 红桃9, 黑桃9, 方块10, 梅花10, 红桃10, 黑桃10, 方块J, 梅花J, 红桃J, 黑桃J, 方块Q, 梅花Q, 红桃Q, 黑桃Q, 方块K, 梅花K, 红桃K, 黑桃K, 方块A, 梅花A, 红桃A, 黑桃A, 方块2, 梅花2, 红桃2, 黑桃2, 小王, 大王]
[红桃8, 方块5, 梅花2, 方块4, 小王, 梅花5, 黑桃2, 方块Q, 黑桃A, 黑桃J, 红桃K, 梅花4, 方块J, 梅花A, 黑桃5, 方块3, 红桃9, 红桃Q, 黑桃K, 方块7, 梅花Q, 梅花6, 红桃J, 方块9, 方块6, 方块8, 黑桃Q, 梅花K, 梅花9, 方块K, 红桃5, 红桃3, 黑桃8, 方块10, 大王, 红桃2, 梅花10, 黑桃10, 黑桃3, 黑桃6, 红桃4, 黑桃9, 红桃6, 梅花8, 梅花7, 红桃10, 黑桃7, 方块A, 黑桃4, 梅花J, 红桃A, 梅花3, 方块2, 红桃7]
java的随机的算法的实例
猜你喜欢
转载自weitao1026.iteye.com/blog/2266126
今日推荐
周排行