2017-03-03 1 views
0

코드에 문제가있어서 변경할 필요가없는 것 같습니다. 여기에 3 개의 파일이 있으며 맨 아래에 오류가 있습니다. 정확한 코드는 18 번이고 그 중 절반은이 오류입니다. 순위, 소송을 초기화단위 테스트 에러가 정의되지 않았습니다. 메소드 'rank'for 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

: 는 필요한 는 "./Deck"을 요구 는 "./Card을"필요 "./Constants"을 "이 필요합니다./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' 
+0

많은 시스템에서 카드는 '2D', '3C', ... 'TH', 'AS'와 같이 표현됩니다. 'T'는 모두 10 개의 문자를 일관되게 유지하기 위해 10을 나타냅니다. – tadman

+0

'@ cards.map (& : suit) .uniq.length == 1' 같은 스트레이트 플러시를 테스트 할 수도 있습니다. 모두 동일한 슈트를 가지고 있음을 의미합니다. 순차적 인 카드의 경우'rank'를 정렬해야합니다. "똑 바른 플러쉬"코드가 작동하는 유일한 방법은 똑같은 카드 다섯 개가있는 경우입니다. – tadman

+0

내 코드를 수정했고 그 중 1/3은 괜찮 았지만 다른 코드는 동일한 오류가 발생했습니다 – Julian

답변

1

, 당신의 PokerHand#initialize 방법은 매우 혼란이다. 빈 배열 인 [ ] 이외의 값을 @cards에 할당하지 마십시오. 따라서 classify 메서드를 호출 할 때 @cards이 여전히 [ ]이므로 어떤 값이 x이든 @cards[0], @cards[1] 또는 실제로 @cards[x] 인 경우는 항상 nil이됩니다. PokerHand#initialize 더 다음과 같아야합니다

def initialize(cards) 
    @cards = cards 
    @hand_type = UNCLASSIFIED 
end 

둘째, 스트레이트 플러시가 잘못 구성하는 것에 대한 논리를. 현재 손에있는 모든 카드가 똑같은 경우, 즉 하트 3 개를 5 매씩 복사하는 경우에만 스트레이트 플러시를 계산합니다.

+0

은 @cards [3] .rank == @cards [4] + 1.rank 또는 @cards [3] .rank == @cards [4] .rank +1 – Julian

+0

정렬 된 카드는 @cards [3] .rank - 1' – philomory

+0

코드를 수정했는데 1/3은 괜찮 았지만 다른 코드는 똑같은 오류가 있습니다. – Julian

관련 문제