1

有人可以帮我弄清楚为什么我会收到以下错误:78- $player_total = $player_cards.inject(:+)

我也得到了它:15 slice!-但这似乎已经停止了?

错误怎么可能是间歇性的?

不相关的问题:如何在 stackoverflow 的帖子中添加行号?

bjtrial.rb:78:in +': nil can't be coerced into Fixnum (TypeError) from bjtrial.rb:78:ineach' from bjtrial.rb:78:in inject' from bjtrial.rb:78:inblock in start_trial' from bjtrial.rb:76:in each' from bjtrial.rb:76:instart_trial' from bjtrial.rb:90:in `'

@天堂:ruby -v ruby​​ 1.9.3p194(2012-04-20 修订版 35410)[x86_64-darwin11.3.0]

#blackjack trial player hits 10-7 once vs. 11, 2
$deck = (((2..11).to_a+[10]*3)*4)
$rand= rand($deck.length)
$dealer_win = 0
$player_win = 0
$tie = 0

$p1 = 10 #players cards
$p2 = 7  

$d1 = 11 #dealers cards
$d2 = 2 

#deal & remove dealt cards deck
$dealer_cards = Array([$deck.slice!($deck.index($d1)),$deck.slice!($deck.index($d2))])
$player_cards = Array([$deck.slice!($deck.index($p1)),$deck.slice!($deck.index($p2))])

$trials = 2
$dealer_win = 0
$playerWin = 0

def hit_card
    #remove hit card from the deck
    $hit_card = $deck.slice!($rand)
    $hit_card
end

def show_dealer_cards
    total = $dealer_cards.inject(:+)
    puts
    puts "Dealer's cards are: #{$dealer_cards.inspect}"
    puts "Dealer's total is: #{$dealer_cards.inject(:+)}"
    sleep 1
    def check_dealer_total
        total = $dealer_cards.inject(:+)

        if total > 21 && $dealer_cards.count(11) < 1
            puts "Dealer Busted- Player wins!"
            $player_win += 1
        elsif total > 21 && $dealer_cards.count(11) >= 1
            puts "Over 21- with an ace to factor:"
            $dealer_cards[$dealer_cards.index(11)] -= 10
            puts "Now the dealer has:"
            show_dealer_cards
        elsif total <= 16 || total == 17 && $dealer_cards.count(11) == 1
            hit_card
            puts "Dealer draws a:#{$hit_card}"
            $dealer_cards.push($hit_card)
            show_dealer_cards
        else
            player_total = $player_cards.inject(:+)
            dealer_total = $dealer_cards.inject(:+)

            puts "Results:"
            puts "Player has #{player_total}"
            puts "Dealer has #{dealer_total}"

            if player_total < dealer_total
                puts "Dealer Wins!"
                $dealer_win += 1
            elsif player_total == dealer_total
                puts "It's a tie! Nobody Wins"
                $tie += 1
            else
                puts "Player Wins!"
                $player_win += 1
            end

        end
    end
    check_dealer_total
end

def start_trial

    for i in (1..$trials).to_a
        $player_cards.push(hit_card) #player hits once and stays or busts
        $player_total = $player_cards.inject(:+) #this is line 78

        if $player_total > 21
            puts "Player Busted"
            $dealer_win += 1
        else
        show_dealer_cards
        end

    end
end

start_trial

puts 
puts "Player Cards: #{$p1}, #{$p2} (alwalys hits once)"
puts "Dealers Cards: #{$d1}, #{$d2} (plays normally)"
puts "Number of trials: #{$trials}"
puts "Ties: #{$tie}"
puts "Player Wins: #{$player_win}"
puts "Dealer Wins: #{$dealer_win}"

我不得不将 $rand 移动到 hit_card 函数中,因为它会间歇性地指向数组中的 nil 值。

它运行-我认为它是准确的.. 不要把你在维加斯的房子押在它上面。哈哈

并感谢其他提示:全局变量。我还没到那一步,但...

#blackjack trial player hits 10-7 once vs. 11, x
$dealer_win = 0.0
$player_win = 0.0
$tie = 0.0

$trials = 500000
#Hit Once to hit...
$strategy = "Hit Once"

def hit_card
    $rand= rand($deck.length)
    #remove hit card from the deck
    $hit_card = $deck.slice!($rand)
    $hit_card
end

def show_dealer_cards
    total = $dealer_cards.inject(:+)
    #puts
    #puts "Dealer's cards are: #{$dealer_cards.inspect}"
    #puts "Dealer's total is: #{$dealer_cards.inject(:+)}"
    #sleep 1
    def check_dealer_total
        total = $dealer_cards.inject(:+)

        if total > 21 && $dealer_cards.count(11) < 1
            #puts "Dealer Busted- Player wins!"
            $player_win += 1.0
        elsif total > 21 && $dealer_cards.count(11) >= 1
            #puts "Over 21- with an ace to factor:"
            $dealer_cards[$dealer_cards.index(11)] -= 10
            #puts "Now the dealer has:"
            show_dealer_cards
        elsif total <= 16 || total == 17 && $dealer_cards.count(11) == 1
            hit_card
            #puts "Dealer draws a:#{$hit_card}"
            $dealer_cards.push($hit_card)
            show_dealer_cards
        else
            player_total = $player_cards.inject(:+)
            dealer_total = $dealer_cards.inject(:+)

            #puts "Results:"
            #puts "Player has #{player_total}"
            #puts "Dealer has #{dealer_total}"

            if player_total < dealer_total
                #puts "Dealer Wins!"
                $dealer_win += 1.0
            elsif player_total == dealer_total
                #puts "It's a tie! Nobody Wins"
                $tie += 1.0
            else
                #puts "Player Wins!"
                $player_win += 1.0
            end

        end
    end
    check_dealer_total
end

def start_trial

    for i in (1..$trials).to_a

        $deck = (((2..11).to_a+[10]*3)*4).shuffle

        $p1 = 10 #players cards
        $p2 = 7  

        $d1 = 11 #dealers cards
        $d2 = hit_card #just a random card

        #deal & remove dealt cards deck
        $dealer_cards = Array([$deck.slice!($deck.index($d1)),$deck.slice!($deck.index($d2))])
        $player_cards = Array([$deck.slice!($deck.index($p1)),$deck.slice!($deck.index($p2))])

        if $strategy == "Hit Once"
            $player_cards.push(hit_card) #player hits once and stays or busts
        else
            $strategy = "Stand"
        end

        $player_total = $player_cards.inject(:+) #this is line 78

        if $player_total > 21
            #puts "Player Busted"
            $dealer_win += 1.0
        else
        show_dealer_cards
        end

    end
end

start_trial

puts 
puts '*' * 80
puts "Player Cards: #{$p1}, #{$p2}"
puts "Strategy: #{$strategy}"
puts "Dealers Cards: #{$d1}, x"
puts "Number of trials: #{$trials}"
puts "Ties: #{$tie}"
puts "Player Wins: #{$player_win}"
puts "Dealer Wins: #{$dealer_win}"
puts "Win Percentage: %.2f" % (($player_win / $trials) * 100)
puts "Loss Percentage: %.2f" % (($dealer_win / $trials) * 100)
puts "Tie Percentage: %.2f" % (($tie / $trials) * 100)
puts '*' * 80
4

1 回答 1

0

我在您的程序中发现了一个完全不同的问题:

def start_trial

    for i in (1..$trials).to_a
        $player_cards.push(hit_card) #player hits once and stays or busts
        $player_total = $player_cards.inject(:+) #this is line 78

        if $player_total > 21
            puts "Player Busted"
            $dealer_win += 1
        else
        show_dealer_cards
        end

    end
end

你正在运行两次试验,没有重新初始化玩家牌组。所以玩家的手就变成了[10, 7, 8, 9]。如果您进行 50 次试验,您将用完卡。

您是否只运行了两次 start_trial 并收到错误消息?

这是我实际运行代码的结果:

load './bj.rb'
Player Busted
Player Busted

Player Cards: 10, 7 (alwalys hits once)
Dealers Cards: 11, 2 (plays normally)
Number of trials: 2
Ties: 0
Player Wins: 0
Dealer Wins: 2
 => true 

但是,如果我更改$trials = 100,那么我会遇到同样的失败:

 load './bj.rb'
Player Busted
TypeError: nil can't be coerced into Fixnum
    from /rails/Stack/bj.rb:78:in `+'

我希望这有帮助。

祝你好运!

于 2012-06-07T04:20:24.343 回答