New to Classes

Hello. I’m new to making and defining your own classes (well really to
Ruby in general)and need some of your help with areas of my code. Here
is the code for the game:
class Die

def roll
@numberShowing = 1 + rand(6)
end

def showing
@numberShowing
end

end

dice = [Die.new]
play=true
money= 100
while play==true
puts ‘You have $’ + money.to_s + ‘. What is your bid?’
bid= gets.chomp
puts ‘$’ + money.to_s + ‘-’ + ‘$’ + bid.to_s
moneytest= money.to_i - bid.to_i
while moneytest<=0
puts ‘Please enter a valid bid’
bid=gets.chomp
moneytest= money.to_i - bid.to_i
end
money=(money.to_i - bid.to_i)
puts ‘$’ + money.to_s + ’ remaining’
puts ‘What number would you like to place your bet of ’ + ‘$’ + bid + ’
on?’
number= gets.chomp
puts ‘Dice roll was…’
die = Die.new
die.roll
puts die.showing
if @numberShowing.to_i==number.to_i
puts 'Congrats! You made ’ + ‘$’ + bid.to_s
else
puts 'Sorry. You lost ’ + ‘$’ + bid.to_s
end
if (money==0 or money<=0)
puts ‘Sorry you lose’
play==false
end
end

The problems I’m having with it (as you will see if you test it) is that
you cannont win money, which I assume is because of my handling of
@numberShowing, and that you can’t go ‘all in’. Any help would be
greatly appreciated.

-Scott

Scott Andrechek wrote:

@numberShowing

puts ‘$’ + money.to_s + ‘-’ + ‘$’ + bid.to_s
number= gets.chomp
puts ‘Dice roll was…’
die = Die.new
die.roll
puts die.showing
if @numberShowing.to_i==number.to_i

Why are you using @numberShowing instead of die.showing? If you print
@numberShowing here I think you will see your problem.

Michael W. Ryder wrote:

Scott Andrechek wrote:

@numberShowing

puts ‘$’ + money.to_s + ‘-’ + ‘$’ + bid.to_s
number= gets.chomp
puts ‘Dice roll was…’
die = Die.new
die.roll
puts die.showing
if @numberShowing.to_i==number.to_i

Why are you using @numberShowing instead of die.showing? If you print
@numberShowing here I think you will see your problem.

Thanks. You can now win money. Unfortanatley you still can’t go ‘all
in’. Hopefully a solution will come up…

-Scott

On Fri, Aug 21, 2009 at 2:57 AM, Scott
Andrechek[email protected] wrote:

Unfortanatley you still can’t go ‘all
in’. Hopefully a solution will come up…

Try changing

while moneytest<=0

to

while moneytest<0


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs