Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:09:40+00:00 2026-06-05T10:09:40+00:00

Can someone help me figure out why I get the following error with line:

  • 0

Can someone help me figure out why I get the following error with line: 78- $player_total = $player_cards.inject(:+)

I also got it with line: 15 slice!- but that seems to have stopped?

How can the error be intermittent?

Unrelated question: How do you add line numbers to a post at stackoverflow?

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

@paradise: ruby -v
ruby 1.9.3p194 (2012-04-20 revision 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}"

I had to move the $rand into the hit_card function as it would intermittently point to a nil value in the array.

It runs- I think its accurate.. Don’t bet your house in vegas on it just yet. LOL

And thanks for the other tip re: the global variables. I haven’t got that far, yet…

#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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:09:43+00:00Added an answer on June 5, 2026 at 10:09 am

    I found a totally different problem in your program:

    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
    

    Yor are running the trial twice, without re-initializing the players deck. So the players hand becomes [10, 7, 8, 9]. If you run 50 trials, you will run out of cards.

    Are you runnning the start_trial only two times and getting the error?

    Here are my result of actually running your code:

    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 
    

    But if I change the $trials = 100, then I get the same failure:

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

    I hope this helps.

    Good Luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please help me figure out a way to achieve the following (see
Can someone help me figure out how to get the content of a Google
Can someone help me figure out how to translate the following curl commands into
I can't figure out how to get the results I need. Can someone help?
I'm going slidely mad over here, maybe someone can help me figure out what's
Can someone please help me figure out what I'm doing wrong? I have a
I'm hoping someone can help me figure out a memory leak that's driving me
Can someone help me with creating functions for the following processes? I want to
Can someone help me find the error when I tried to display the list
I am very new to RegEx -- so can someone please help me figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.