78- $player_total = $player_cards.inject(:+) 行で次のエラーが発生する理由を誰かが理解するのを手伝ってくれますか?
私も行でそれを得ました: 15 スライス!-しかし、それは停止したようですか?
エラーはどのように断続的に発生しますか?
無関係な質問: 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 リビジョン 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 は、断続的に配列内の nil 値を指すため、hit_card 関数に移動する必要がありました。
それは実行されます-私はその正確だと思います.. まだラスベガスの家に賭けないでください. 笑
そして、グローバル変数に関する他のヒントに感謝します。まだそこまで行ってませんが…
#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