Last active
August 29, 2015 13:56
-
-
Save tosik/9319629 to your computer and use it in GitHub Desktop.
This file contains 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
module Poker | |
class Card | |
attr_accessor :number, :mark | |
def initialize(number, mark) | |
@number = number | |
@mark = mark | |
end | |
def number_string | |
case @number | |
when 1 | |
"A" | |
when 11 | |
"J" | |
when 12 | |
"Q" | |
when 13 | |
"K" | |
else | |
@number | |
end | |
end | |
def mark_string | |
case @mark | |
when :spade | |
"♠" | |
when :heart | |
"♥" | |
when :diamond | |
"♦" | |
when :club | |
"♣" | |
end | |
end | |
def to_s | |
"#{mark_string} #{number_string}" | |
end | |
end | |
class Hand | |
def initialize | |
@cards = [ | |
Card.new(1, :spade), | |
Card.new(2, :heart), | |
Card.new(3, :diamond), | |
Card.new(3, :heart), | |
Card.new(2, :club), | |
] | |
end | |
def to_s | |
@cards.map {|card| card.to_s }.join(",") | |
end | |
def rank | |
count = 0 | |
@cards.each do |left| | |
@cards.each do |top| | |
count += 1 if left.number == top.number | |
end | |
end | |
count | |
end | |
end | |
end | |
hand = Poker::Hand.new | |
puts hand | |
puts hand.rank | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
途中すぎる感じのコードですが、rubygems.org に poker gem があったので作るのやめた