diff --git a/card.rb b/card.rb index c556be9..0a66a06 100644 --- a/card.rb +++ b/card.rb @@ -1,65 +1,71 @@ -#!/usr/bin/env ruby - -class Card - - attr_reader :rank, :suit - - def initialize(str) - r = str[0,1].downcase - s = str[1,1].downcase - @suit = case s - when 's' then :spades - when 'h' then :hearts - when 'd' then :diamonds - when 'c' then :clubs - end - @rank = case r - when '2'..'9' then r.to_i - when 't' then 10 - when 'j' then 11 - when 'q' then 12 - when 'k' then 13 - when 'a' then 14 - end - end - - def rank_s - case @rank - when 2..10 then @rank.to_s - when 11 then "Jack" - when 12 then "Queen" - when 13 then "King" - when 14 then "Ace" - end - end - - def suit_s - @suit.to_s.capitalize - end - - def to_s - rank_s + "-" + suit_s - end - - def ==(other) - @rank == other.rank && @suit == other.suit - end - - def <=>(other) - if @rank != other.rank - @rank <=> other.rank - else - # order is Spades, Hearts, Diamonds, Clubs (conveniently reverse alphabetical) - other.suit_s <=> suit_s - end - end - - def eql?(other) - self == other - end - - def hash - to_s.hash - end -end - +#!/usr/bin/env ruby + +class Card + + attr_reader :rank, :suit + + def initialize(str) + r = str[0,1].downcase + s = str[1,1].downcase + @suit = case s + when 's' then :spades + when 'h' then :hearts + when 'd' then :diamonds + when 'c' then :clubs + end + @rank = case r + when '2'..'9' then r.to_i + when 't' then 10 + when 'j' then 11 + when 'q' then 12 + when 'k' then 13 + when 'a' then 14 + end + end + + def rank_long_s + ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace'][@rank-2] + end + + def suit_long_s + @suit.to_s.capitalize + end + + def rank_s + ['2','3','4','5','6','7','8','9','t','j','q','k','a'][@rank-2] + end + + def suit_s + {:spades => 's', :hearts => 'h', :diamonds => 'd', :clubs => 'c'}[@suit] + end + + def to_s + rank_s + suit_s + end + + def to_long_s + rank_long_s + '-' + suit_long_s + end + + def ==(other) + @rank == other.rank && @suit == other.suit + end + + def <=>(other) + if @rank != other.rank + @rank <=> other.rank + else + # order is Spades, Hearts, Diamonds, Clubs (conveniently reverse alphabetical) + other.suit_s <=> suit_s + end + end + + def eql?(other) + self == other + end + + def hash + to_s.hash + end +end + diff --git a/combinations.rb b/combinations.rb index 29b5f1e..a057e50 100644 --- a/combinations.rb +++ b/combinations.rb @@ -1,16 +1,16 @@ -#!/usr/bin/env ruby - -class Array - def combinations n - result = Array.new - if n > 0 - (0 .. self.length - n).each do |i| - combs = self[i + 1 .. -1].combinations(n - 1) - combs = [[]] if combs.empty? - combs.collect {|comb| [self[i]] + comb}.each {|x| result << x} - end - end - return result - end -end - +#!/usr/bin/env ruby + +class Array + def combinations n + result = Array.new + if n > 0 + (0 .. self.length - n).each do |i| + combs = self[i + 1 .. -1].combinations(n - 1) + combs = [[]] if combs.empty? + combs.collect {|comb| [self[i]] + comb}.each {|x| result << x} + end + end + return result + end +end + diff --git a/css/tripoli.simple.css b/css/tripoli.simple.css new file mode 100644 index 0000000..a83db68 --- /dev/null +++ b/css/tripoli.simple.css @@ -0,0 +1,29 @@ +/* + * Tripoli is a generic CSS standard for HTML rendering. + * Copyright (C) 2007-2008 David Hellsing + * + * http://devkick.com/lab/tripoli/ + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +** +_______________________________ +RESET */ +*{text-decoration:none;font-size:1em;outline:none;margin:0;padding:0}code,kbd,samp,pre,tt,var,textarea,input,select,isindex,listing,xmp,plaintext{font:inherit;white-space:normal}a,img,a img,iframe,form,abbr,acronym,object,applet,table,a abbr,a acronym{border-width:0}dfn,i,cite,var,address,em{font-style:normal}th,b,strong,h1,h2,h3,h4,h5,h6,dt{font-weight:400}caption,th,td{text-align:left}html{background:#FFF;color:#000;line-height:1;font-family:arial, sans-serif}/* \*/html{font-family:sans-serif}/* */q{quotes:"\201C""\201D""\2018""\2019"}ul,ol,dir,menu{list-style:none}sub,sup{vertical-align:baseline}a{color:inherit}/* +_______________________________ +DISABLE DEPRECATED HTML */ +font,basefont{color:inherit;font:inherit;font-size:100%}center,*[align]{text-align:inherit}s,strike,u{text-decoration:inherit}img{border:none;margin:0}ol{list-style-type:decimal}body{background-color:transparent}tr,th,td{width:auto;height:auto;background-color:transparent;vertical-align:inherit;border:none}table[border],.content table[border]{border-collapse:separate;border-spacing:0}nobr{white-space:normal}marquee{overflow:visible;-moz-binding:none}blink{text-decoration:none}/* +_______________________________ +GENERAL */ +html{font-size:125%}body{font-size:50%}a{text-decoration:underline}strong,th,thead td,h1,h2,h3,h4,h5,h6,dt{font-weight:700}cite,em,dfn{font-style:italic}code,kbd,samp,pre,tt,var,input[type='text'],input[type='password'],textarea{font-size:100%;font-family:mono-space,monospace}pre{white-space:pre}pre *{font-size:100%;white-space:pre}del{text-decoration:line-through}ins,dfn{border-bottom:1px solid #000}small,sup,sub{font-size:85%}big{font-size:125%;line-height:80%}abbr,acronym{text-transform:uppercase;font-size:85%;letter-spacing:.1em}abbr[title],acronym[title],dfn[title]{cursor:help;border-bottom:1px dotted #000}sup{vertical-align:super}sub{vertical-align:sub}blockquote{padding-left:2.2em}hr{display:none/* We will re-reset it later for content */}:lang(af),:lang(nl),:lang(pl){quotes:'\201E' '\201D' '\201A' '\2019'}:lang(bg),:lang(cs),:lang(de),:lang(is),:lang(lt),:lang(sk),:lang(sr),:lang(ro){quotes:'\201E' '\201C' '\201A' '\2018'}:lang(da),:lang(hr){quotes:'\00BB' '\00AB' '\203A' '\2039'}:lang(el),:lang(es),:lang(sq),:lang(tr){quotes:'\00AB' '\00BB' '\2039' '\203A'}:lang(en-GB){quotes:'\2018' '\2019' '\201C' '\201D'}:lang(fi),:lang(sv){quotes:'\201D' '\201D' '\2019' '\2019'}:lang(fr){quotes:'\ab\2005' '\2005\bb' '\2039\2005' '\2005\203a'}*[lang|='en'] q:before{content:'\201C'}*[lang|='en'] q:after{content:'\201D'}*[lang|='en'] q q:before{content:'\2018'}*[lang|='en'] q q:after{content:'\2019'}input,select,button{cursor:pointer}input[type='text'],input[type='password']{cursor:text}input[type='hidden']{display:none}/* +_______________________________ +CONTENT */ +.content{font-size:1.2em;line-height:1.6em}.content h1{font-size:1.6em;line-height:1;margin:1em 0 .5em}.content h2{font-size:1.5em;line-height:1;margin:1.07em 0 .535em}.content h3{font-size:1.4em;line-height:1;margin:1.14em 0 .57em}.content h4{font-size:1.3em;line-height:1;margin:1.23em 0 .615em}.content h5{font-size:1.2em;line-height:1;margin:1.33em 0 .67em}.content h6{font-size:1em;line-height:1;margin:1.6em 0 .8em}.content hr{display:block;background:#000;color:#000;width:100%;height:1px;border:none}.content ul{list-style:disc outside}.content ol{list-style:decimal outside}.content table{border-collapse:collapse}.content hr,.content p,.content ul,.content ol,.content dl,.content pre,.content address,.content table,.content form{margin-bottom:1.6em}.content p+p{margin-top:-.8em}.content fieldset{margin:1.6em 0;padding:1.6em}/* \*/.content legend{padding-left:.8em;padding-right:.8em}/* *//* for Opera 8 */@media all and min-width 0px{.content legend{margin-bottom:1.6em}.content fieldset{margin-top:0}.content[class^='content'] fieldset{margin-top:1.6em}}.content fieldset>*:first-child{margin-top:0}.content textarea,.content input[type='text']{padding:.1em .2em}.content input{padding:.2em .1em}.content select{padding:.2em .1em 0}.content select[multiple]{margin-bottom:.8em}.content option{padding:0 .4em .1em}.content button{padding:.3em .5em}.content input[type='radio']{position:relative;bottom:-.2em}.content dt{margin-top:.8em;margin-bottom:.4em}.content ul,.content ol{margin-left:2.2em}.content caption,.content form div{padding-bottom:.8em}.content ul ul,content ol ul,.content ul ol,content ol ol{margin-bottom:0}/* +_______________________________ +VISUAL PLUG */ +blockquote{color:#666}blockquote > *:first-child:before /* Tripoli bonus: pure CSS blockquote */{content:"\201C";font-size:2.5em;margin-left:-.62em;font-family:georgia,serif;padding-right:.2em;color:#aaa;line-height:0}abbr[title],acronym[title],dfn[title]{border-bottom:1px solid #ccc}ins,dfn{border-bottom-color:#666}del{color:#666}fieldset{border-color:#ccc}textarea,input[type='text'],input[type='password'],select{border:1px solid #ccc;background:#fff}fieldset{border:1px solid #ccc}textarea:hover,input[type='text']:hover,input[type='password']:hover,select:hover{border-color:#aaa}textarea:focus,input[type='text']:focus,input[type='password']:focus,select:focus{outline:2px solid #e4e4e4;border-color:#888}.content hr{background:#aaa;color:#aaa}.content table{border-top:1px solid #ccc;border-left:1px solid #ccc}.content th,.content td{border-bottom:1px solid #ddd;border-right:1px solid #ccc}.content th,.content td{padding:.8em}a:link{color:#36c}a:visited{color:#99c}a:hover,code,pre{color:#c33}a:active,.a:focus{color:#000}/* +_______________________________ +TYPE PLUG */ +.alt{font-family:"baskerville italic","Warnock Pro","Goudy Old Style","Palatino","palatino linotype","Book Antiqua",Georgia, serif;font-style:italic;font-weight:400}.dquo{margin-left:-.55em}/* +_______________________________ +END */ \ No newline at end of file diff --git a/css/tripoli.simple.ie.css b/css/tripoli.simple.ie.css new file mode 100644 index 0000000..8ae1f6d --- /dev/null +++ b/css/tripoli.simple.ie.css @@ -0,0 +1,14 @@ +/* + * Tripoli is a generic CSS standard for HTML rendering. + * Copyright (C) 2007-2008 David Hellsing + * + * http://devkick.com/lab/tripoli/ + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +** +_______________________________ +BASE.IE */ +textarea{white-space:pre}.content legend{margin-bottom:1.6em}.content fieldset{padding-top:0}sup,sub{font-size:100%}.content legend:first-child{margin-top:0}a abbr,a acronym{text-decoration:underline}* html .content blockquote *{margin:.8em 0}* html input{cursor:expression(this.type=='text' || this.type=='password' ? 'text' : 'pointer');display:expression(this.type=='hidden' ? 'none' : 'inline')}* html .content textarea,* html .content input,.content input[type='submit'],.content input[type='button']{padding:0}/* +_______________________________ +END */ \ No newline at end of file diff --git a/hand.rb b/hand.rb index 5ba8635..59e9ca7 100644 --- a/hand.rb +++ b/hand.rb @@ -1,93 +1,93 @@ -#!/usr/bin/env ruby - -require 'card' - -class Hand - def initialize(hand_string) - @cards = hand_string.scan(/../).collect { |s| Card.new(s) }.sort - end - - def legal? - @cards.size == @cards.sort.uniq.size && [5,10].include?(@cards.size) - end - - def illegal? - !(legal?) - end - - def rank - return nil if illegal? - - # array of suits, ranks - suits = @cards.collect { |c| c.suit_s } - ranks = @cards.collect { |c| c.rank } - - # count of suits - suit_counts = Hash.new(0) - suits.each { |s| suit_counts[s] += 1 } - - # count of ranks - rank_counts = Hash.new(0) - ranks.each { |s| rank_counts[s] += 1 } - - # test for straight & flush - straight = (ranks.sort == (ranks.min .. ranks.max).to_a) - flush = (suits.sort.uniq.size == 1) - - # rank hand - return :royal_flush if straight && flush && ranks.min == 10 - return :straight_flush if straight && flush - return :four_of_a_kind if [1,4] == rank_counts.values.sort - return :full_house if [2,3] == rank_counts.values.sort - return :flush if flush - return :straight if straight - - # nothing - nil - end - - - def score - r = rank - - return 0 if r.nil? - - # ten card hands - return 80 if @cards.size == 10 && r == :straight_flush - return 40 if @cards.size == 10 && r == :flush - return 20 if @cards.size == 10 && r == :straight - - # five card hands - return 21 if r == :royal_flush - return 20 if r == :straight_flush - return 15 if r == :four_of_a_kind - return 10 if r == :full_house - return 7 if r == :flush - return 5 if r == :straight - - # nothing - return 0 - end - - def score_name - rank.to_s.gsub(/_/,' ') if rank - end - - def to_s - @cards.collect { |c| c.to_s }.join(', ') - end - - def hash - to_s.hash - end - - def <=>(other) - to_s <=> other.to_s - end - -end - - - - - +#!/usr/bin/env ruby + +require 'card' + +class Hand + def initialize(hand_string) + @cards = hand_string.scan(/../).collect { |s| Card.new(s) }.sort + end + + def legal? + @cards.size == @cards.sort.uniq.size && [5,10].include?(@cards.size) + end + + def illegal? + !(legal?) + end + + def rank + return nil if illegal? + + # array of suits, ranks + suits = @cards.collect { |c| c.suit_s } + ranks = @cards.collect { |c| c.rank } + + # count of suits + suit_counts = Hash.new(0) + suits.each { |s| suit_counts[s] += 1 } + + # count of ranks + rank_counts = Hash.new(0) + ranks.each { |s| rank_counts[s] += 1 } + + # test for straight & flush + straight = (ranks.sort == (ranks.min .. ranks.max).to_a) + flush = (suits.sort.uniq.size == 1) + + # rank hand + return :royal_flush if straight && flush && ranks.min == 10 + return :straight_flush if straight && flush + return :four_of_a_kind if [1,4] == rank_counts.values.sort + return :full_house if [2,3] == rank_counts.values.sort + return :flush if flush + return :straight if straight + + # nothing + nil + end + + + def score + r = rank + + return 0 if r.nil? + + # ten card hands + return 80 if @cards.size == 10 && r == :straight_flush + return 40 if @cards.size == 10 && r == :flush + return 20 if @cards.size == 10 && r == :straight + + # five card hands + return 21 if r == :royal_flush + return 20 if r == :straight_flush + return 15 if r == :four_of_a_kind + return 10 if r == :full_house + return 7 if r == :flush + return 5 if r == :straight + + # nothing + return 0 + end + + def score_name + rank.to_s.gsub(/_/,' ') if rank + end + + def to_s + @cards.collect { |c| c.to_s }.join(', ') + end + + def hash + to_s.hash + end + + def <=>(other) + to_s <=> other.to_s + end + +end + + + + + diff --git a/hands.txt b/hands.txt new file mode 100644 index 0000000..ac3f348 --- /dev/null +++ b/hands.txt @@ -0,0 +1,9 @@ +tj,tsjsqsksastcjcqckcac +tj,tsjsqsksastcjcqckcac +cathy,tsjsqsksastcjcqckcac +cathy,3d8c6s7d8h9s9dkdqd5d +cathy,2s3s4s5s6s7s8s9stsjs +eileen,2c3s4c5d6h7s8d9sthjs +randy,as3s4s5s6s7s8s9stsjs +jen,asadacah6s7s8s9stsjs +jen,asadackskd7s8s9stsjs diff --git a/img/2c.gif b/img/2c.gif index fee499c..779be38 100644 Binary files a/img/2c.gif and b/img/2c.gif differ diff --git a/img/2d.gif b/img/2d.gif index 13c04f1..7325844 100644 Binary files a/img/2d.gif and b/img/2d.gif differ diff --git a/img/2h.gif b/img/2h.gif index d51c1ca..5c2f055 100644 Binary files a/img/2h.gif and b/img/2h.gif differ diff --git a/img/2s.gif b/img/2s.gif index a78b663..a056885 100644 Binary files a/img/2s.gif and b/img/2s.gif differ diff --git a/img/3c.gif b/img/3c.gif index ec7f3a6..0ed59ec 100644 Binary files a/img/3c.gif and b/img/3c.gif differ diff --git a/img/3d.gif b/img/3d.gif index 1f7051e..db15763 100644 Binary files a/img/3d.gif and b/img/3d.gif differ diff --git a/img/3h.gif b/img/3h.gif index a5945d6..96e83dc 100644 Binary files a/img/3h.gif and b/img/3h.gif differ diff --git a/img/3s.gif b/img/3s.gif index a8b82b0..b78109b 100644 Binary files a/img/3s.gif and b/img/3s.gif differ diff --git a/img/4c.gif b/img/4c.gif index 4200ce0..c3087f3 100644 Binary files a/img/4c.gif and b/img/4c.gif differ diff --git a/img/4d.gif b/img/4d.gif index d0c045f..c8cbe5e 100644 Binary files a/img/4d.gif and b/img/4d.gif differ diff --git a/img/4h.gif b/img/4h.gif index d698954..296c848 100644 Binary files a/img/4h.gif and b/img/4h.gif differ diff --git a/img/4s.gif b/img/4s.gif index 4e7ca42..1706c62 100644 Binary files a/img/4s.gif and b/img/4s.gif differ diff --git a/img/5c.gif b/img/5c.gif index 806d512..1d8210b 100644 Binary files a/img/5c.gif and b/img/5c.gif differ diff --git a/img/5d.gif b/img/5d.gif index 48e9d6a..ac65c8e 100644 Binary files a/img/5d.gif and b/img/5d.gif differ diff --git a/img/5h.gif b/img/5h.gif index b5e2cc5..e956619 100644 Binary files a/img/5h.gif and b/img/5h.gif differ diff --git a/img/5s.gif b/img/5s.gif index 9a7d653..7f92c13 100644 Binary files a/img/5s.gif and b/img/5s.gif differ diff --git a/img/6c.gif b/img/6c.gif index fe20aa9..86f9e27 100644 Binary files a/img/6c.gif and b/img/6c.gif differ diff --git a/img/6d.gif b/img/6d.gif index 965b1df..0c2460d 100644 Binary files a/img/6d.gif and b/img/6d.gif differ diff --git a/img/6h.gif b/img/6h.gif index a796aac..84fe2b5 100644 Binary files a/img/6h.gif and b/img/6h.gif differ diff --git a/img/6s.gif b/img/6s.gif index aeabece..10b85bf 100644 Binary files a/img/6s.gif and b/img/6s.gif differ diff --git a/img/7c.gif b/img/7c.gif index f2ba71e..3334637 100644 Binary files a/img/7c.gif and b/img/7c.gif differ diff --git a/img/7d.gif b/img/7d.gif index c14c7d3..f3292f5 100644 Binary files a/img/7d.gif and b/img/7d.gif differ diff --git a/img/7h.gif b/img/7h.gif index 4cde873..7445906 100644 Binary files a/img/7h.gif and b/img/7h.gif differ diff --git a/img/7s.gif b/img/7s.gif index 2e2acbb..c6e8272 100644 Binary files a/img/7s.gif and b/img/7s.gif differ diff --git a/img/8c.gif b/img/8c.gif index efae72e..0c9c235 100644 Binary files a/img/8c.gif and b/img/8c.gif differ diff --git a/img/8d.gif b/img/8d.gif index 5c7d407..db1c388 100644 Binary files a/img/8d.gif and b/img/8d.gif differ diff --git a/img/8h.gif b/img/8h.gif index 8979af5..4eb9e23 100644 Binary files a/img/8h.gif and b/img/8h.gif differ diff --git a/img/8s.gif b/img/8s.gif index 0e67a58..28d12e8 100644 Binary files a/img/8s.gif and b/img/8s.gif differ diff --git a/img/9c.gif b/img/9c.gif index 5a3470e..aad6762 100644 Binary files a/img/9c.gif and b/img/9c.gif differ diff --git a/img/9d.gif b/img/9d.gif index d947458..7faeb03 100644 Binary files a/img/9d.gif and b/img/9d.gif differ diff --git a/img/9h.gif b/img/9h.gif index 8ba2a6e..70d7baf 100644 Binary files a/img/9h.gif and b/img/9h.gif differ diff --git a/img/9s.gif b/img/9s.gif index 6b694a5..4289967 100644 Binary files a/img/9s.gif and b/img/9s.gif differ diff --git a/img/ac.gif b/img/ac.gif index 36d2278..7907a1b 100644 Binary files a/img/ac.gif and b/img/ac.gif differ diff --git a/img/ad.gif b/img/ad.gif index f0fb5b1..66a2d22 100644 Binary files a/img/ad.gif and b/img/ad.gif differ diff --git a/img/ah.gif b/img/ah.gif index fc69abf..beb48ea 100644 Binary files a/img/ah.gif and b/img/ah.gif differ diff --git a/img/as.gif b/img/as.gif index 1312930..8188392 100644 Binary files a/img/as.gif and b/img/as.gif differ diff --git a/img/jc.gif b/img/jc.gif index c030ab2..704084e 100644 Binary files a/img/jc.gif and b/img/jc.gif differ diff --git a/img/jd.gif b/img/jd.gif index afc1233..a4be54a 100644 Binary files a/img/jd.gif and b/img/jd.gif differ diff --git a/img/jh.gif b/img/jh.gif index 9a8782b..8f409c9 100644 Binary files a/img/jh.gif and b/img/jh.gif differ diff --git a/img/js.gif b/img/js.gif index b9b421c..c719730 100644 Binary files a/img/js.gif and b/img/js.gif differ diff --git a/img/kc.gif b/img/kc.gif index 9bb1117..6c1581b 100644 Binary files a/img/kc.gif and b/img/kc.gif differ diff --git a/img/kd.gif b/img/kd.gif index 9f2b69b..a5652e0 100644 Binary files a/img/kd.gif and b/img/kd.gif differ diff --git a/img/kh.gif b/img/kh.gif index f44ccbf..21e8f1c 100644 Binary files a/img/kh.gif and b/img/kh.gif differ diff --git a/img/ks.gif b/img/ks.gif index 504dd7a..5e236bd 100644 Binary files a/img/ks.gif and b/img/ks.gif differ diff --git a/img/qc.gif b/img/qc.gif index bf997b6..a2afb00 100644 Binary files a/img/qc.gif and b/img/qc.gif differ diff --git a/img/qd.gif b/img/qd.gif index d1eb8aa..7b5a513 100644 Binary files a/img/qd.gif and b/img/qd.gif differ diff --git a/img/qh.gif b/img/qh.gif index 7e6d7b3..6561cb0 100644 Binary files a/img/qh.gif and b/img/qh.gif differ diff --git a/img/qs.gif b/img/qs.gif index a7ad367..6d78eb9 100644 Binary files a/img/qs.gif and b/img/qs.gif differ diff --git a/img/tc.gif b/img/tc.gif index f9b1c84..3425cde 100644 Binary files a/img/tc.gif and b/img/tc.gif differ diff --git a/img/td.gif b/img/td.gif index 66af3be..f671084 100644 Binary files a/img/td.gif and b/img/td.gif differ diff --git a/img/th.gif b/img/th.gif index 9aaf5b9..5e35783 100644 Binary files a/img/th.gif and b/img/th.gif differ diff --git a/img/ts.gif b/img/ts.gif index 5a89a9c..82c0a40 100644 Binary files a/img/ts.gif and b/img/ts.gif differ diff --git a/rank.rb b/rank.rb index 2420119..d2d643a 100755 --- a/rank.rb +++ b/rank.rb @@ -1,64 +1,123 @@ -#!/usr/bin/env ruby - -require 'hand' -require 'combinations' - -def score(hand_string, verbose=false) - # split into an array of two character strings (each representing a card) - cards = hand_string.scan(/../) - - # a place to store all of our scores - best_hands = nil - best_score = -1 - - # create and score all permutations of two 5 card hands - cards.combinations(5).each do |c| - # create each hand from two 10 character strings - hand_a = Hand.new(c.join) - hand_b = Hand.new((cards-c).join) - score = hand_a.score + hand_b.score - - # save the hand if it's the best - best_hands,best_score = [hand_a,hand_b],score if score > best_score - end - - # now create and score the 10 card hand - ten_card_hand = Hand.new(cards.join) - best_hands,best_score = [ten_card_hand],ten_card_hand.score if ten_card_hand.score > best_score - - # DEBUG - if verbose then - best_hands.each do |h| - puts "#{h.score} points for a #{h.score_name}" - puts h - puts - end - puts "#{best_score} TOTAL POINTS" - end - - # return score - best_score -end - -score('tsjsqsksastcjcqckcac',true) -puts -puts -score('3d8c6s7d8h9s9dkdqd5d',true) -puts -puts -score('2s3s4s5s6s7s8s9stsjs',true) -puts -puts -score('2c3s4c5d6h7s8d9sthjs',true) -puts -puts -score('as3s4s5s6s7s8s9stsjs',true) -puts -puts -score('asadacah6s7s8s9stsjs',true) -puts -puts -score('asadackskd7s8s9stsjs',true) -puts -puts - +#!/usr/bin/env ruby + +require 'hand' +require 'combinations' + +def score(hand_string, verbose=false) + # split into an array of two character strings (each representing a card) + cards = hand_string.scan(/../) + + # a place to store all of our scores + best_hands = nil + best_score = -1 + + # create and score all permutations of two 5 card hands + cards.combinations(5).each do |c| + # create each hand from two 10 character strings + hand_a = Hand.new(c.join) + hand_b = Hand.new((cards-c).join) + score = hand_a.score + hand_b.score + + # save the hand if it's the best + best_hands,best_score = [hand_a,hand_b],score if score > best_score + end + + # now create and score the 10 card hand + ten_card_hand = Hand.new(cards.join) + best_hands,best_score = [ten_card_hand],ten_card_hand.score if ten_card_hand.score > best_score + + # DEBUG + if verbose then + best_hands.each do |h| + puts "#{h.score} points for a #{h.score_name}" + puts h + puts + end + puts "#{best_score} TOTAL POINTS" + end + + # return score + [best_score, best_hands] +end + +PlayerNames = { + 'aaron' => 'Aaron Dickinson', + 'adam' => 'Adam Dickinson', + 'angel' => 'Angel Madigan', + 'cathy' => 'Cathy Waits', + 'dundee' => 'Dundee Reyno', + 'eddy' => 'Eddy Cramm', + 'eileen' => 'Eileen Gher', + 'eryn' => 'Eryn Roston', + 'francois' => 'François Bertrand', + 'fred' => 'Fred Thompson', + 'george' => 'George Rothrock', + 'jeff' => 'Jeff Curley', + 'jen' => 'Jen Gratz', + 'john' => 'John Yuill', + 'jonathan' => 'Jonathan Leghart', + 'josh' => 'Josh Hart', + 'lonna' => 'Lonna Leghart', + 'mike' => 'Mike Skinner', + 'peter' => 'Peter Heino', + 'ramana' => 'Ramana Prakash', + 'randy' => 'Randy Yumul', + 'rob' => 'Rob Whyte', + 'ryan' => 'Ryan Mack', + 'shaelagh' => 'Shaelagh Skinner', + 'stephanie' => 'Stéphanie Plante', + 'steven' => 'Steven Gher', + 'swaits' => 'Stephen Waits', + 'tim' => 'Tim Flier', + 'tj' => 'TJ Madigan', +} + + +total = Hash.new(0) + +puts '' +puts 'Super Babel Scoring' +puts '' +puts '' +puts ' ' +puts '' +puts '
' +puts '

Hand Log

' +puts "" +puts "" +even = false +IO.readlines('hands.txt').each do |line| + player,cards = line.chomp.split(/,/) + score,hands = score(cards) + total[player.downcase] += score + evenorodd = even ? "even" : "odd" + even = !even + puts "" + puts " " + colspan = 3 - hands.size + hands.each do |h| + puts " " + end + puts " " + puts " " + puts "" +end +puts "
PlayerHandsScoreTotal
#{PlayerNames[player]} [#{player}]" + h.to_s.gsub(/ /,'').split(',').each { |card| puts "" } + puts "
#{h.score_name} (#{h.score})
#{score}#{total[player]}
" + + +puts '

Total Scores

' +total.sort { |a,b| a[1] <=> b[1] }.reverse.each { |s| puts "#{PlayerNames[s[0]]} [#{s[0]}] - #{s[1]}
" } + +puts '
' +puts '' +puts '' +