Created
November 9, 2010 10:24
-
-
Save mostlyfine/668935 to your computer and use it in GitHub Desktop.
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
| class Poker | |
| NO_PAIR = 0 | |
| ONE_PAIR = 1 | |
| TWO_PAIR = 2 | |
| THREE_OF_KIND = 3 | |
| STRAIGHT = 4 | |
| FLUSH = 5 | |
| FULL_HOUSE = 6 | |
| FOUR_CARD = 7 | |
| STRAIGHT_FLUSH = 8 | |
| FIVE_CARD = 9 | |
| ROYAL_STRAIGHT_FLUSH = 10 | |
| WILD_CARD = "*" | |
| ROYAL_NUMBERS = %w(A T J Q K) | |
| NUMBERS = %w(A 2 3 4 5 6 7 8 9 T J Q K) | |
| SUITS = %w(1 2 3 4) | |
| attr_accessor :numbers, :suits | |
| # ex) ["1A","2T","22","4Q","36"] | |
| def initialize(cards) | |
| @numbers = [] | |
| @suits = [] | |
| cards.each do |card| | |
| suit, num = card.split(//) | |
| @suits << suit | |
| @numbers << num | |
| end | |
| end | |
| def wild_card_match | |
| if wild = numbers.index(WILD_CARD) | |
| result = [] | |
| SUITS.each do |suit| | |
| NUMBERS.each do |number| | |
| suits[wild] = suit | |
| numbers[wild] = number | |
| result << match | |
| end | |
| end | |
| result.max | |
| else | |
| match | |
| end | |
| end | |
| def match | |
| if royal_straight_flush? | |
| ROYAL_STRAIGHT_FLUSH | |
| elsif five_card? | |
| FIVE_CARD | |
| elsif straight_flush? | |
| STRAIGHT_FLUSH | |
| elsif four_card? | |
| FOUR_CARD | |
| elsif full_house? | |
| FULL_HOUSE | |
| elsif flush? | |
| FLUSH | |
| elsif straight? | |
| STRAIGHT | |
| elsif three_of_kind? | |
| THREE_OF_KIND | |
| elsif two_pair? | |
| TWO_PAIR | |
| else | |
| # one_pairもブタ扱い | |
| NO_PAIR | |
| end | |
| end | |
| def one_pair? | |
| how_include_cards?(numbers, 2) | |
| end | |
| def two_pair? | |
| numbers.uniq.size == 3 and not three_of_kind? | |
| end | |
| def three_of_kind? | |
| how_include_cards?(numbers, 3) | |
| end | |
| def straight? | |
| check = "A23456789TJQKA" | |
| reverse_numbers = %w(2 3 4 5 6 7 8 9 T J Q K A) | |
| sorted = numbers.sort{|a,b| NUMBERS.index(a) <=> NUMBERS.index(b)}.to_s | |
| reversed = numbers.sort{|a,b| reverse_numbers.index(a) <=> reverse_numbers.index(b)}.to_s | |
| !!check.index(sorted) or !!check.index(reversed) | |
| end | |
| def flush? | |
| suits.uniq.size == 1 | |
| end | |
| def full_house? | |
| three_of_kind? and one_pair? | |
| end | |
| def four_card? | |
| how_include_cards?(numbers, 4) | |
| end | |
| def straight_flush? | |
| straight? and flush? | |
| end | |
| def five_card? | |
| how_include_cards?(numbers, 5) | |
| end | |
| def royal_straight_flush? | |
| straight_flush? and royal? | |
| end | |
| private | |
| def royal? | |
| (numbers - ROYAL_NUMBERS).size == 0 | |
| end | |
| def how_include_cards?(numbers, any) | |
| numbers.each do |number| | |
| return true if numbers.size - (numbers - [number]).size == any | |
| end | |
| false | |
| end | |
| end |
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
| describe Poker do | |
| it "役成立パターンチェック" do | |
| card_pattern.each do |card, pattern| | |
| Poker.new(card).wild_card_match.should == pattern | |
| end | |
| end | |
| private | |
| def card_pattern | |
| { | |
| ["1Q","1J","1A","1K","1T"] => Poker::ROYAL_STRAIGHT_FLUSH, | |
| ["1Q","1J","1A","1K","**"] => Poker::ROYAL_STRAIGHT_FLUSH, | |
| ["1Q","2J","1A","1K","1T"] => Poker::STRAIGHT, | |
| ["43","24","12","1A","15"] => Poker::STRAIGHT, | |
| ["49","28","16","17","1T"] => Poker::STRAIGHT, | |
| ["**","2J","1A","1K","1T"] => Poker::STRAIGHT, | |
| ["**","24","12","1A","15"] => Poker::STRAIGHT, | |
| ["24","47","35","16","43"] => Poker::STRAIGHT, | |
| ["4Q","**","3Q","1Q","2Q"] => Poker::FIVE_CARD, | |
| ["39","37","36","35","38"] => Poker::STRAIGHT_FLUSH, | |
| ["**","37","36","35","38"] => Poker::STRAIGHT_FLUSH, | |
| ["22","32","12","43","42"] => Poker::FOUR_CARD, | |
| ["22","32","12","43","**"] => Poker::FOUR_CARD, | |
| ["22","33","12","43","42"] => Poker::FULL_HOUSE, | |
| ["16","4Q","**","3Q","26"] => Poker::FULL_HOUSE, | |
| ["19","14","18","1T","1J"] => Poker::FLUSH, | |
| ["19","**","1A","1T","1J"] => Poker::FLUSH, | |
| ["16","46","25","4Q","26"] => Poker::THREE_OF_KIND, | |
| ["16","**","25","4Q","26"] => Poker::THREE_OF_KIND, | |
| ["16","4Q","25","**","26"] => Poker::THREE_OF_KIND, | |
| ["16","4Q","25","3Q","26"] => Poker::TWO_PAIR, | |
| ["16","4Q","25","3Q","2T"] => Poker::NO_PAIR, | |
| ["16","**","25","3Q","2T"] => Poker::NO_PAIR, | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment