使用 Groovy、JDK 集合和 Eclipse 集合的扑克牌
作者: Paul King
发布时间: 2022-09-23 10:18AM (上次更新: 2022-09-25 11:15AM)
再次,Donald Raab 发布了一篇有趣的博客文章,关于Eclipse 集合,这篇展示了一些用 Java 17 和 Eclipse 集合来建模和操作卡片的代码。强烈建议您阅读他相关的 Kata。以下是用 Groovy (这里使用的是 4.0.5) 编写的相同示例,只需稍微调整一下
enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING }
enum Suit { SPADES, DIAMONDS, HEARTS, CLUBS }
@Sortable(includes='suit,rank')
record Card(Rank rank, Suit suit) { }
var cards = Sets.cartesianProduct(EnumSet.allOf(Rank), EnumSet.allOf(Suit), Card::new)
var cardsBySuit = cards.groupBy(Card::suit)
var cardsByRank = cards.groupBy(Card::rank)
assert [cards, cardsByRank, cardsBySuit]*.size() == [52, 13 ,4]
var random = new Random(42L)
var deck = cards.toList().shuffleThis(random).shuffleThis(random).shuffleThis(random).toStack()
(1..5).collect(i -> deck.pop(5).toSortedList()).each(this::println)
以下是输出
[Card[rank=FOUR, suit=SPADES], Card[rank=FOUR, suit=DIAMONDS], Card[rank=SIX, suit=HEARTS], Card[rank=NINE, suit=CLUBS], Card[rank=JACK, suit=CLUBS]] [Card[rank=FIVE, suit=DIAMONDS], Card[rank=TWO, suit=HEARTS], Card[rank=FIVE, suit=HEARTS], Card[rank=TEN, suit=CLUBS], Card[rank=QUEEN, suit=CLUBS]] [Card[rank=FIVE, suit=SPADES], Card[rank=NINE, suit=SPADES], Card[rank=QUEEN, suit=SPADES], Card[rank=THREE, suit=DIAMONDS], Card[rank=TWO, suit=CLUBS]] [Card[rank=EIGHT, suit=SPADES], Card[rank=TWO, suit=DIAMONDS], Card[rank=EIGHT, suit=DIAMONDS], Card[rank=KING, suit=HEARTS], Card[rank=FIVE, suit=CLUBS]] [Card[rank=SIX, suit=SPADES], Card[rank=KING, suit=DIAMONDS], Card[rank=THREE, suit=HEARTS], Card[rank=TEN, suit=HEARTS], Card[rank=QUEEN, suit=HEARTS]]
我们可以用内置的 JDK 集合做类似的示例,并进行一些额外的调整以获得更漂亮的输出
enum Rank {
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
String toString() { ['A', *'2'..'9', '10', 'J', 'Q', 'K'][ordinal()] }
}
enum Suit {
SPADES, DIAMONDS, HEARTS, CLUBS
String toString() { '♠♦♥♣'[ordinal()] }
}
@Sortable(includes='suit,rank')
record Card(Rank rank, Suit suit) {
Card(List srPair) { this(srPair[1], srPair[0]) }
String toString() { "${rank}${suit}" }
}
var cards = [Suit.values(), Rank.values()].combinations().collect(Card::new)
var cardsBySuit = cards.groupBy(Card::suit)
var cardsByRank = cards.groupBy(Card::rank)
assert [cards, cardsByRank, cardsBySuit]*.size() == [52, 13, 4]
var random = new Random(42L)
3.times { cards.shuffle(random) }
var deck = cards as Stack
5.times { println "Hand ${it+1}: ${(0..5).collect{ deck.pop() }.sort()}" }
println "Remaining cards sorted:\n${deck.sort()}"
输出如下
这两个 Groovy 示例都可以在 JDK 8 到 19 版本上运行
(JDK 8 到 15 版本使用的是模拟记录)。