My problem is that chips
is not saving as a global variable for the argument it is passed. I am passing $h1c
(which is the number of total chips player's first hand has). So if he wins or loses, chips
should then be set equal to chips
+
or -
betamount
.
The problem is that it's not being saved as the global variable. If I were to write $h1c = 150_000
, then it would equal that. If later on I write $h1c = 150_000 + 50_000
, then 200_000 would be the new value of $h1c.
For some reason this isn't working when I declare chips = chips + betamount
, which is the same as saying $h1c = $h1c + $h1bet
.
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
end
This is the where I pass the arguments to review:
def pre_review(num)
puts "Recap of that round"
puts "First Hand:"
review($hand1, $h1c, $h1bet)
muckcards(num)
end
If need be, here is the link to the full code/game to test the problem out http://labs.codecademy.com/Bmvl#:workspace Note: That I'm currently just trying to get this portion to work for $hand1, so you would select 1 for number of hands to play to reproduce this problem.
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 ofchips
, you're only changing the value ofchips
within the context of thereview
method. As soon as that method returns, the value ofchips
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:
Then, set the value of the player's chips from outside the method:
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! ;-)