私のコードに問題があり、変更する必要があるかどうかわからないようです。ここに私の3つのファイルがあり、下には私が取得しているエラーがあります。私はまったく同じコードを18回持っており、その半分が私にこのエラーを与えています。ランク、スーツ、シンボル単体テストのエラーが未定義メソッド `rank 'が1の場合:Fixnum
def initialize(the_rank, the_suit)
@rank = the_rank
@suit = the_suit
@symbols = [nil, nil, '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
end
新しいファイルPokerhand.rb初期化
: は "./Constants" は "./Deck" は「必要必要 "./Card" を必要と必要としています。/CardSpaceship」
class PokerHand < Deck
include Constants
attr_reader :hand_type
def initialize(the_cards)
@cards = [ ]
@hand_type = UNCLASSIFIED
for card in the_cards
@cards << card
end
end
# Straight
elsif @cards[0].rank == @cards[1]+1.rank &&
@cards[1].rank == @cards[2]+1.rank &&
@cards[2].rank == @cards[3]+1.rank &&
@cards[3].rank == @cards[4]+1.rank
@hand_type = STRAIGHT
end
end
新しいファイルtest2.rb:
class PokerHand < Deck
include Constants
attr_reader :hand_type
def initialize(the_cards)
@cards = [ ]
@hand_type = UNCLASSIFIED
for card in the_cards
@cards << card
end
end
# Determine hand type of PokerHand object.
def classify
@cards.sort!
# Straight
def test_7
arr7 = [Card.new(2, "C"), Card.new(3, "S"),
Card.new(4, "H"), Card.new(5, "D"),
Card.new(6, "S")]
ph7 = PokerHand.new(arr7)
ph7.classify
assert_equal STRAIGHT , ph7.hand_type
end
取得エラー:開始のために
TestClass#test_7:
NoMethodError: undefined method `rank' for 1:Fixnum
PokerHand.rb:79:in `classify'
test2.rb:76:in `test_7'
多くのシステムでは、カードは「2D」、「3C」、...「TH」、「AS」のように表現されます。「T」は10文字を一貫して2文字の文字列にします。 – tadman
また、 '@ cards.map(&:suit).uniq.length == 1'のようにまっすぐなフラッシュをテストすることもできます。シーケンシャルカードの場合、 'rank'をソートする必要があります。あなたの "まっすぐな"コードがここで働く唯一の方法は、まったく同じカードを5枚持っている場合です。 – tadman
私のコードを修正し、そのうち1/3は問題ありませんが、他は同じエラーを返します – Julian