主要内容:这里使用collection集合,模拟香港电影中大佬们玩的扑克牌游戏。
1、游戏规则:两个玩家每人手中发两张牌,进行比较。比较每个玩家手中牌最大的点数,大小由A-2,点数大者获胜。如果点数相同,则比较花色,大小由黑(4)、红(3)、梅(2)、方(1),花色大者获胜。
2、实现步骤:
创建一副扑克牌A-2,四种花色黑(4)、红(3)、梅(2)、方(1)共52张牌;
创建两个玩家包含玩家ID和姓名、所持牌Card信息;
洗牌并向两位玩家各发两张牌;
比较玩家手中牌大小,得出获胜者;
3、程序实现
牌Card类:包含牌的数字和花色
package collectiontest.games; public class Card { private Integer id; //牌的大小 private Integer type;//牌的花色 public Card(Integer id, Integer type) { this.id = id; this.type = type; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } @Override public String toString() { return "Card [id=" + id + ", type=" + type + "]"; } }
扑克牌Poker类:包含扑克牌Card A-2
package collectiontest.games; public class Poker { private Card id2 ; private Card id3 ; private Card id4 ; private Card id5 ; private Card id6 ; private Card id7 ; private Card id8 ; private Card id9 ; private Card id10 ; private Card J ; private Card Q ; private Card K ; private Card A ; public Poker() { } //四个类型:黑--4、红--3、梅--2、方--1 public Poker(Integer type) { this.id2 = new Card(2, type); this.id3 = new Card(3, type); this.id4 = new Card(4, type); this.id5 = new Card(5, type); this.id6 = new Card(6, type); this.id7 = new Card(7, type); this.id8 = new Card(8, type); this.id9 = new Card(9, type); this.id10 = new Card(10, type); this.J = new Card(11, type); this.Q = new Card(12, type); this.K = new Card(13, type); this.A = new Card(14, type); } public Card getId2() { return id2; } public void setId2(Card id2) { this.id2 = id2; } public Card getId3() { return id3; } public void setId3(Card id3) { this.id3 = id3; } public Card getId4() { return id4; } public void setId4(Card id4) { this.id4 = id4; } public Card getId5() { return id5; } public void setId5(Card id5) { this.id5 = id5; } public Card getId6() { return id6; } public void setId6(Card id6) { this.id6 = id6; } public Card getId7() { return id7; } public void setId7(Card id7) { this.id7 = id7; } public Card getId8() { return id8; } public void setId8(Card id8) { this.id8 = id8; } public Card getId9() { return id9; } public void setId9(Card id9) { this.id9 = id9; } public Card getId10() { return id10; } public void setId10(Card id10) { this.id10 = id10; } public Card getJ() { return J; } public void setJ(Card j) { J = j; } public Card getQ() { return Q; } public void setQ(Card q) { Q = q; } public Card getK() { return K; } public void setK(Card k) { K = k; } public Card getA() { return A; } public void setA(Card a) { A = a; } }
玩家Player类:包含玩家ID和姓名、所持卡片信息