Created
July 1, 2012 01:06
-
-
Save fumokmm/3026343 to your computer and use it in GitHub Desktop.
ポーカーをGroovyで解いてみた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[♢A, ♠A, ♢10, ♣A, ♡A] => フォーカード(4K) | |
[♠10, ♡J, ♢J, ♣J, ♠J] => フォーカード(4K) | |
[♠10, ♡A, ♢10, ♢A, ♣10] => フルハウス(FH) | |
[♡J, ♢J, ♣3, ♠J, ♠3] => フルハウス(FH) | |
[♠3, ♠4, ♡3, ♢3, ♢A] => スリーカード(3K) | |
[♠2, ♡A, ♢K, ♣K, ♠K] => スリーカード(3K) | |
[♠A, ♠J, ♢A, ♣J, ♠10] => ツーペア(2P) | |
[♠2, ♠10, ♡10, ♡K, ♢2] => ツーペア(2P) | |
[♣K, ♡10, ♢10, ♡3, ♡J] => ワンペア(1P) | |
[♣3, ♢3, ♠10, ♠K, ♠2] => ワンペア(1P) | |
[♠3, ♠J, ♢A, ♣10, ♠Q] => 役なし(--) | |
[♣3, ♣9, ♠A, ♠10, ♢2] => 役なし(--) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for http://qiita.com/items/cbc3af152ee3f50a822f | |
enum Suit { | |
S('♤♠'), H('♡♥'), D('♢♦'), C('♧♣'), | |
def mark | |
Suit(mark){ this.mark = mark } | |
} | |
enum Rank { | |
R2, R3, R4, R5, R6, R7, R8, R9, R10, RJ, RQ, RK, RA | |
def rank(){ this.name().substring(1) } | |
} | |
class Card { | |
Suit s | |
Rank r | |
static Card valueOf(String str) { | |
new Card(s:str[0] as Suit, r:'R'+str[1..-1] as Rank) | |
} | |
String toString() { | |
switch(s) { | |
case [Suit.H, Suit.D] : return "${s.mark[0]}${r.rank()}" | |
case [Suit.S, Suit.C] : return "${s.mark[1]}${r.rank()}" | |
} | |
} | |
} | |
def patMap = [ | |
[1, 4] : [abbr: '4K', comm: 'フォーカード'], | |
[2, 3] : [abbr: 'FH', comm: 'フルハウス'], | |
[1, 1, 3] : [abbr: '3K', comm: 'スリーカード'], | |
[1, 2, 2] : [abbr: '2P', comm: 'ツーペア'], | |
[1, 1, 1, 2] : [abbr: '1P', comm: 'ワンペア'], | |
[1, 1, 1, 1, 1] : [abbr: '--', comm: '役なし'] | |
] | |
def toPat = { cardStr -> | |
def cards = cardStr.findAll( | |
"(${Suit.values().join('|')})" + | |
"(${Rank.values()*.rank().join('|')})" | |
).collect{ Card.valueOf(it) } | |
def pat = cards.countBy{ it.r }.values().sort().with{ patMap[it] } | |
println "$cards => $pat.comm($pat.abbr)" | |
pat.abbr | |
} | |
assert toPat('DASAD10CAHA') == '4K' | |
assert toPat('S10HJDJCJSJ') == '4K' | |
assert toPat('S10HAD10DAC10') == 'FH' | |
assert toPat('HJDJC3SJS3') == 'FH' | |
assert toPat('S3S4H3D3DA') == '3K' | |
assert toPat('S2HADKCKSK') == '3K' | |
assert toPat('SASJDACJS10') == '2P' | |
assert toPat('S2S10H10HKD2') == '2P' | |
assert toPat('CKH10D10H3HJ') == '1P' | |
assert toPat('C3D3S10SKS2') == '1P' | |
assert toPat('S3SJDAC10SQ') == '--' | |
assert toPat('C3C9SAS10D2') == '--' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment