斗地主逻辑业务实现(List,Collection的学习)

Card.java

  1 /** 斗地主业务中的 扑克牌 类型 */
2 public class Card {
3 public static final int THREE = 0;
4 public static final int FOUR = 1;
5 public static final int FIVE = 2;
6 public static final int SIX = 3;
7 public static final int SEVEN = 4;
8 public static final int EIGHT = 5;
9 public static final int NINE = 6;
10 public static final int TEEN = 7;
11 public static final int JACK = 8;
12 public static final int QUEEN = 9;
13 public static final int KING = 10;
14 /** A */
15 public static final int ACE = 11;
16 public static final int DEUCE = 12;
17 public static final int BLACK = 13;
18 public static final int COLOR = 14;
19 /** 方块 */
20 public static final int DIAMOND = 0;
21 /** 梅花 */
22 public static final int CLUB = 1;
23 /** 红桃 */
24 public static final int HEART = 2;
25 /** 黑桃 */
26 public static final int SPADE = 3;
27 /***/
28 public static final int JOKER = 4;
29
30 private static final String[] SUIT_NAMES=
31 {"方块","梅花","红桃","黑桃",""};
32 private static final String[] RANK_NAMES=
33 {"3","4","5","6","7","8","9","10","J",
34 "Q","K","A","2","",""};
35
36
37 /** 点数
38 * 0~15, 0代表3,1代表3...
39 * 11代表A 12代表2 13:小,14:大*/
40 private int rank;
41 /** 花色, 0:方块,1:梅花,2:红桃 3:黑桃 4:王 */
42 private int suit;
43
44 public Card() {
45 }
46 public Card(int suit, int rank) {
47 //this.rank = rank;
48 //this.suit = suit;
49 setRank(rank);
50 setSuit(suit);
51 }
52 /**
53 * 构造器
54 * @param cardName 如:黑桃10, 大王,小王
55 */
56 public Card(String cardName){
57 //要处理大小王问题
58 System.out.println("yiyogn!");
59 String suitName;//黑桃
60 String rankName;//10
61 if(cardName.endsWith("")){
62 suitName = cardName.substring(1);
63 rankName = cardName.substring(0,1);
64 }else{
65 suitName = cardName.substring(0,2);
66 rankName = cardName.substring(2);
67 }
68 int rank = parseRank(rankName);//黑桃->3
69 int suit = parseSuit(suitName);//10->7
70 setRank(rank);
71 setSuit(suit);
72 System.out.println("用到了!");
73 }
74
75 private int parseRank(String rankName) {
76 for (int i = 0; i < RANK_NAMES.length; i++) {
77 String name = RANK_NAMES[i];
78 //if(rankName!=null && rankName.equals(name)){
79 if(name.equals(rankName)){
80 return i;
81 }
82 }
83 throw new IllegalArgumentException("错了!");
84 }
85 private int parseSuit(String suitName) {
86 for (int i = 0; i < SUIT_NAMES.length; i++) {
87 String name = SUIT_NAMES[i];
88 if(name.equals(suitName)){
89 return i;
90 }
91 }
92 throw new IllegalArgumentException("错了!");
93 }
94
95 public void setSuit(int suit) {
96 if(suit<DIAMOND || suit >JOKER)
97 throw new IllegalArgumentException("超!");
98 this.suit = suit;
99 }
100 public int getSuit() {
101 return suit;
102 }
103 public void setRank(int rank) {
104 if(rank<THREE || rank>COLOR)
105 throw new IllegalArgumentException("超了");
106 this.rank = rank;
107 }
108 public int getRank() {
109 return rank;
110 }
111
112
113 public String toString() {
114 return SUIT_NAMES[suit] + RANK_NAMES[rank];
115 }
116 public boolean equals(Object obj) {
117 if(obj==null)
118 return false;
119 if(this==obj)
120 return true;
121 if (obj instanceof Card) {
122 Card other = (Card) obj;
123 return rank==other.rank &&
124 suit==other.suit;
125 }
126 return false;
127 }
128 public int hashCode() {
129 return suit*100+rank;
130 }
131
132 }

Player.java

 1 import java.util.ArrayList;
2 import java.util.List;
3
4 public class Player {
5 private int id;// 编号
6 private String name; // 登录名
7 private String pwd;// 密码
8 private List<Card> cards = // 人手中的牌
9 new ArrayList<Card>();
10
11 public Player() {
12 }
13
14 public Player(int id, String name) {
15 this.id = id;
16 this.name = name;
17 }
18
19 public void add(Card card) {
20 this.cards.add(card);
21 }
22
23 public List<Card> getCards() {
24 return cards;
25 }
26
27 public void setCards(List<Card> cards) {
28 this.cards = cards;
29 }
30
31 public int getId() {
32 return id;
33 }
34
35 public void setId(int id) {
36 this.id = id;
37 }
38
39 public String getName() {
40 return name;
41 }
42
43 public void setName(String name) {
44 this.name = name;
45 }
46
47 public String getPwd() {
48 return pwd;
49 }
50
51 public void setPwd(String pwd) {
52 this.pwd = pwd;
53 }
54
55 @Override
56 public String toString() {
57 return name + ":" + cards;
58 }
59
60 @Override
61 public boolean equals(Object obj) {
62 if (obj == null)
63 return false;
64 if (this == obj)
65 return true;
66 if (obj instanceof Player) {
67 Player other = (Player) obj;
68 return this.id == other.id;
69 }
70 return false;
71 }
72
73 @Override
74 public int hashCode() {
75 return id;
76 }
77
78 }

CardDemo.java

 1 import java.util.ArrayList;
2 import java.util.Iterator;
3 import java.util.List;
4 import java.util.Random;
5
6 public class CardDemo {
7 public static void main(String[] args) {
8 List<Card> cards = new ArrayList<Card>();
9 for (int rank = Card.THREE; rank <= Card.DEUCE; rank++) {
10 cards.add(new Card(Card.DIAMOND, rank));
11 cards.add(new Card(Card.SPADE, rank));
12 cards.add(new Card(Card.HEART, rank));
13 cards.add(new Card(Card.CLUB, rank));
14
15 }
16 cards.add(new Card(Card.JOKER, Card.BLACK));
17 cards.add(new Card(Card.JOKER, Card.COLOR));
18
19 System.out.println(cards);
20 cards = shuffle(cards);
21 System.out.println(cards);
22
23 // Card c = new Card("黑桃3");
24 // 已经有了一副扑克牌了
25 Player mo = new Player(1, "莫小贝");
26 Player bai = new Player(2, "白展堂");
27 Player zhang = new Player(3, "张静初");
28
29 // 0 1 2 0 1 2 0 1 2
30 Player[] players = { mo, bai, zhang };
31 // 0 1 2
32 // 发牌
33 int idx = 0;
34 Iterator<Card> ite = cards.iterator();
35 while (ite.hasNext()) {
36 Card card = ite.next();
37 players[idx++ % players.length].add(card);
38 ite.remove();// 删除一副牌中,刚刚发过的牌
39 // cards.remove(card);//
40 if (cards.size() == 3)// 剩下三张不发了
41 break;
42 }
43 System.out.println(mo);
44 System.out.println(bai);
45 System.out.println(zhang);
46 System.out.println(cards);
47 }
48
49 /**
50 * @param pokers
51 * 有序扑克牌数组
52 * @return 乱序后的扑克牌数组
53 */
54 public static List<Card> shuffle(List<Card> pokers) {
55 Random r = new Random();
56 for (int i = pokers.size() - 1; i > 0; i--) {
57 int j = r.nextInt(i);
58 Card cj = pokers.get(j);
59 Card ci = pokers.set(i, cj);// 置換
60 pokers.set(j, ci);
61 }
62 return pokers;
63 }
64 }

结果:

posted @ 2011-07-29 09:16  牧涛  阅读(1868)  评论(0编辑  收藏  举报