Question

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.

Was it helpful?

Solution

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! ;-)

OTHER TIPS

Ruby is a pass-by-value language, not pass-by-reference. Therefore you cannot reassign the value of a variable passed into a method inside that method. Check out the wikipedia article on evaluation strategy for info on the different approaches to variable passing.

The fact that Ruby is pass-by-value is slightly complicated b/c Ruby actually passes a reference to the object by value. Therefore you could for example modify the contents of a hash inside a method and have those changes reflected in the external variable. Java also passes references by value, and I've always liked this write-up about it, which you may find useful.

When you do this:

review($hand1, $h1c, $h1bet)

This does not pass the global variable $h1c itself to the method, it passes (a reference to) its value.

Within the review method, the value that was passed is given the name chips, which is local to that method. When you assign to chips, you're only updating the name chips to refer to a different value.

To get the behaviour you want, you'd need to assign directly to $h1c instead of to chips. (Or, better programming practice, you could return the value of chips from the method instead of using globals.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top