It's not the same. Variables prefixed with $
are global, whereas unprefixed variables are local. So when you change the value of $h1c
, you are changing the value of the same $h1c
variable that you reference elsewhere in the program. However, when you change the value of chips
, you're only changing the value of chips
within the context of the review
method. As soon as that method returns, the value of chips
is lost.
So the simplest solution would be to not even bother passing the arguments to that method and just have it read the values of the global variables directly. That's probably not the best solution though in terms of style. In fact, generally the use of global variables at all is considered bad practice.
The other solution is to return the new value of the player's chips from the review method, like so:
def review(hand, chips, betamount)
abc = valueofcards(hand) #player's hand value
klm = valueofcards($handD) #dealer's hand value
if abc == klm and abc < 19
puts "You tied"
chips = chips
elsif abc > 18
puts "You lost"
chips = chips - betamount
elsif abc < 19 and klm > 18
puts "You won"
chips = chips + betamount
elsif abc < 19 and abc > klm
puts "You won"
chips = chips + betamount
elsif abc < 19 and klm < 19 and klm > abc
puts "You lost"
chips = chips - betamount
end
return chips # See here
end
Then, set the value of the player's chips from outside the method:
def pre_review(num)
puts "Recap of that round"
puts "First Hand:"
$h1c = review($hand1, $h1c, $h1bet) # Like this
muckcards(num)
end
There's probably a lot more that could be done to fix up that program in terms of code quality and programming style, but I won't get in to that right now. Keep learning! ;-)